From 3d241a10393faf5dad67a9ce44c6d67eb81b28ae Mon Sep 17 00:00:00 2001 From: fge1087 Date: Wed, 29 Oct 2014 12:07:01 -0500 Subject: [PATCH] added functions to Order model added functions to the Order model to complete the tests in tests.py, and uncommented those tests so they run --- orders/models.py | 42 +++++++++++++++++++++++++++++++++++++++++- orders/tests.py | 8 ++++---- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/orders/models.py b/orders/models.py index aeb40f6..cf14b17 100644 --- a/orders/models.py +++ b/orders/models.py @@ -1,4 +1,5 @@ -from django.db import models +from django.db import models, connection +from django.db.models import Count, Q class Order(models.Model): @@ -10,6 +11,45 @@ class Order(models.Model): ) shipping_method = models.CharField(max_length=100, choices=SHIPPING_CHOICES) date_completed = models.DateTimeField() + + @classmethod + def split_by_shipping_method(cls): + """ Return two lists of primary keys: one for orders done by First Class Mail, and one for + orders done by Priority Mail.""" + fcm = list(cls.objects.filter(shipping_method='FCM').values_list('pk', flat=True)) + pri = list(cls.objects.filter(shipping_method='PRI').values_list('pk', flat=True)) + return fcm, pri + + @classmethod + def split_by_single_and_multiple(cls): + """ Return two lists of primary keys: one for orders that have only one item, and one for + orders that have multiple items.""" + singles = list(cls.objects.annotate(Count('items')).filter(items__count=1).values_list('pk', flat=True)) + multiples = list(cls.objects.annotate(Count('items')).filter(items__count__gte=2).values_list('pk', flat=True)) + return singles, multiples + + @classmethod + def single_orders_are_sorted(cls): + """ Return list of orders with only one item, sorted by the priority denoted in the priority attribute of the OrderItem + class (XS -> XXL).""" + cursor = connection.cursor() + cursor.execute("""SELECT innerQuery.id, orders_orderitem.product from (SELECT orders_order.id FROM orders_order LEFT OUTER JOIN orders_orderitem ON ( orders_order.id = orders_orderitem.order_id ) + GROUP BY orders_order.id, orders_order.shipping_method, orders_order.date_completed + HAVING COUNT(orders_orderitem.id) = 1) innerQuery LEFT OUTER JOIN orders_orderitem ON (innerQuery.id = orders_orderitem.order_id)""") + + single_orders = cursor.fetchall() + single_sorted_orders = [pk for (pk, product) in sorted(single_orders, key=lambda tup: OrderItem.priority[tup[1]])] + return single_sorted_orders + + @classmethod + def orders_split_by_xxl_and_not(cls): + """ Return two lists of primary keys: one for multiple orders that include at least one + XXL shirt, and one for multiple orders that have no XXL shirts.""" + base_query = cls.objects.annotate(Count('items')).filter(items__count__gte=2) + xxl = list(base_query.filter(items__product='XXL').values_list('pk', flat=True)) + not_xxl = list(base_query.filter(~Q(items__product = 'XXL')).values_list('pk', flat=True)) + return xxl, not_xxl + class OrderItem(models.Model): diff --git a/orders/tests.py b/orders/tests.py index a3ea841..3c01807 100644 --- a/orders/tests.py +++ b/orders/tests.py @@ -9,20 +9,20 @@ class OrderOrderingTestCase(TestCase): fixtures = ['test_orders.json'] def test_orders_are_split_by_shipping_method(self): - #fcm, pri = Order.split_by_shipping_method() + fcm, pri = Order.split_by_shipping_method() self.assertEqual(results.fcm, fcm) self.assertEqual(results.pri, pri) def test_orders_are_split_by_single_and_multiple(self): - #singles, multiples = Order.split_by_single_and_multiple() + singles, multiples = Order.split_by_single_and_multiple() self.assertEqual(results.singles, singles) self.assertEqual(results.multiples, multiples) def test_single_orders_are_sorted(self): - #single_sorted_orders = Order.single_orders_are_sorted() + single_sorted_orders = Order.single_orders_are_sorted() self.assertEqual(results.single_sorted_orders, single_sorted_orders) def test_multiple_orders_are_split_by_xxl_and_not(self): - #xxl, not_xxl = Order.orders_split_by_xxl_and_not() + xxl, not_xxl = Order.orders_split_by_xxl_and_not() self.assertEqual(results.xxl, xxl) self.assertEqual(results.not_xxl, not_xxl) \ No newline at end of file