Skip to content

Commit

Permalink
ZDL-83: On cart page, when increase items of different product, it so…
Browse files Browse the repository at this point in the history
…metimes switches the order of the items
  • Loading branch information
RyanAquino committed Sep 18, 2021
1 parent 8a0de01 commit 725c17c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
21 changes: 21 additions & 0 deletions orders/migrations/0002_auto_20210917_2012.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.0.8 on 2021-09-17 12:12

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
("orders", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="orderitem",
name="date_added",
field=models.DateTimeField(
default=django.utils.timezone.now, editable=False
),
),
]
3 changes: 2 additions & 1 deletion orders/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils import timezone
from django.db import models
from authentication.models import User
from products.models import Product
Expand Down Expand Up @@ -46,7 +47,7 @@ class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, null=True)
date_added = models.DateTimeField(auto_now_add=True)
date_added = models.DateTimeField(default=timezone.now, editable=False)

def __str__(self):
return str(self.id)
Expand Down
78 changes: 77 additions & 1 deletion orders/tests/test_orders_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from orders.tests.factories.order_item_factory import OrderItemFactory
from orders.tests.factories.order_factory import OrderFactory
from orders.models import OrderItem, Order
Expand All @@ -16,13 +18,47 @@ def test_list_all_orders_with_empty_database(logged_in_client):
assert response.json() == {"total_items": 0, "total_amount": 0, "products": []}


@pytest.mark.django_db
def test_list_all_orders_sorted_by_date_added(logged_in_client, logged_in_user):
customer_order = OrderFactory(customer=logged_in_user)
date_format = "%Y-%m-%d"
first_order_date = "2021-11-20"
second_order_date = "2021-11-25"
third_order_date = "2021-11-26"

OrderItemFactory(
order=customer_order,
date_added=datetime.strptime(first_order_date, date_format),
)
OrderItemFactory(
order=customer_order,
date_added=datetime.strptime(second_order_date, date_format),
)
OrderItemFactory(
order=customer_order,
date_added=datetime.strptime(third_order_date, date_format),
)

response = logged_in_client.get("/v1/orders/")
response_data = response.json()
sorted_dates = [
order_item["date_added"] for order_item in response_data["products"]
]
assert sorted_dates == [
"2021-11-26T00:00:00Z",
"2021-11-25T00:00:00Z",
"2021-11-20T00:00:00Z",
]


@pytest.mark.django_db
def test_list_all_orders(logged_in_client, logged_in_user):
"""
Test list all orders
"""
customer_order = OrderFactory(id=1, customer=logged_in_user)
order_item = OrderItemFactory(order=customer_order)
product = ProductFactory(name="Product 0")
order_item = OrderItemFactory(id=1, order=customer_order, product=product)

response = logged_in_client.get("/v1/orders/")
response_data = response.json()
Expand Down Expand Up @@ -160,3 +196,43 @@ def test_multiple_order_items_should_not_delete_main_order(logged_in_user):
OrderItemFactory.create_batch(5)
OrderItem.objects.first().delete()
assert OrderItem.objects.count() == 4 and Order.objects.count() == 4


@pytest.mark.django_db
def test_update_cart_response_sorted_by_date_added(logged_in_client, logged_in_user):
order = OrderFactory(customer=logged_in_user)
date_format = "%Y-%m-%d"
first_order_date = "2021-11-20"
second_order_date = "2021-11-25"
third_order_date = "2021-11-26"

OrderItemFactory(
date_added=datetime.strptime(second_order_date, date_format),
order=order,
quantity=1,
)
OrderItemFactory(
date_added=datetime.strptime(third_order_date, date_format),
order=order,
quantity=1,
)
first_order = OrderItemFactory(
date_added=datetime.strptime(first_order_date, date_format),
order=order,
quantity=1,
)
product_id = first_order.product.id

data = {"productId": product_id, "action": "add"}

response = logged_in_client.post(f"/v1/orders/update-cart/", data=data)
response_data = response.json()
sorted_dates = [
order_item["date_added"] for order_item in response_data["products"]
]

assert sorted_dates == [
"2021-11-26T00:00:00Z",
"2021-11-25T00:00:00Z",
"2021-11-20T00:00:00Z",
]
8 changes: 6 additions & 2 deletions orders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def list(self, request, *args, **kwargs):
resp = {
"total_items": order.get_cart_items,
"total_amount": order.get_cart_total,
"products": serializer.data,
"products": sorted(
serializer.data, key=lambda x: x["date_added"], reverse=True
),
}

return Response(resp)
Expand Down Expand Up @@ -85,7 +87,9 @@ def update_cart(self, request):
resp = {
"total_items": order.get_cart_items,
"total_amount": order.get_cart_total,
"products": serializer.data,
"products": sorted(
serializer.data, key=lambda x: x["date_added"], reverse=True
),
}

return Response(data=resp, status=200)
Expand Down

0 comments on commit 725c17c

Please sign in to comment.