diff --git a/orders/migrations/0002_auto_20210917_2012.py b/orders/migrations/0002_auto_20210917_2012.py new file mode 100644 index 0000000..02e10b1 --- /dev/null +++ b/orders/migrations/0002_auto_20210917_2012.py @@ -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 + ), + ), + ] diff --git a/orders/models.py b/orders/models.py index 4fc1195..d739b44 100644 --- a/orders/models.py +++ b/orders/models.py @@ -1,3 +1,4 @@ +from django.utils import timezone from django.db import models from authentication.models import User from products.models import Product @@ -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) diff --git a/orders/tests/test_orders_api.py b/orders/tests/test_orders_api.py index 33fd956..9f6f244 100644 --- a/orders/tests/test_orders_api.py +++ b/orders/tests/test_orders_api.py @@ -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 @@ -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() @@ -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", + ] diff --git a/orders/views.py b/orders/views.py index 42f076e..3d57b53 100644 --- a/orders/views.py +++ b/orders/views.py @@ -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) @@ -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)