Просмотр исходного кода

Rework partner tests for readability

master
David Winterbottom 13 лет назад
Родитель
Сommit
372d44b336

+ 6
- 3
oscar/apps/partner/wrappers.py Просмотреть файл

@@ -7,6 +7,9 @@ class DefaultWrapper(object):
7 7
     """
8 8
     Default stockrecord wrapper
9 9
     """
10
+    CODE_IN_STOCK = 'instock'
11
+    CODE_AVAILABLE = 'available'
12
+    CODE_UNAVAILABLE = 'outofstock'
10 13
 
11 14
     def is_available_to_buy(self, stockrecord):
12 15
         """
@@ -55,10 +58,10 @@ class DefaultWrapper(object):
55 58
         :param oscar.apps.partner.models.StockRecord stockrecord: stockrecord instance
56 59
         """
57 60
         if stockrecord.net_stock_level > 0:
58
-            return 'instock'
61
+            return self.CODE_IN_STOCK
59 62
         if self.is_available_to_buy(stockrecord):
60
-            return 'available'
61
-        return 'outofstock'
63
+            return self.CODE_AVAILABLE
64
+        return self.CODE_UNAVAILABLE
62 65
 
63 66
     def availability(self, stockrecord):
64 67
         """

tests/unit/partner/stock_tests.py → tests/functional/stock_alert_tests.py Просмотреть файл

@@ -8,59 +8,73 @@ from oscar.apps.partner.models import StockRecord, StockAlert
8 8
 from oscar.apps.order.utils import OrderCreator
9 9
 
10 10
 
11
-class StockActionTests(TestCase):
11
+# These aren't really unit tests - move them?
12
+class TestPlacingAnOrder(TestCase):
12 13
 
13 14
     def setUp(self):
14 15
         self.product = create_product(price=D('12.00'), num_in_stock=5)
15 16
         self.basket = Basket()
16 17
         self.basket.add_product(self.product)
17 18
 
18
-    def tearDown(self):
19
-        StockAlert.objects.all().delete()
20
-
21 19
     def set_threshold(self, threshold):
22 20
         self.product.stockrecord.low_stock_threshold = threshold
23 21
         self.product.stockrecord.save()
24 22
 
25
-    def test_stock_is_allocated_after_checkout(self):
26
-        order = OrderCreator().place_order(self.basket)
23
+    def test_correctly_allocates_stock(self):
24
+        OrderCreator().place_order(self.basket)
27 25
 
28 26
         record = StockRecord.objects.get(product=self.product)
29 27
         self.assertEqual(5, record.num_in_stock)
30 28
         self.assertEqual(1, record.num_allocated)
31 29
         self.assertEqual(4, record.net_stock_level)
32 30
 
33
-    def test_no_alert_raised_if_threshold_not_breached(self):
34
-        self.set_threshold(3)
35
-        order = OrderCreator().place_order(self.basket)
31
+    def test_does_not_raise_an_alert_if_threshold_not_broken(self):
32
+        self.set_threshold(4)
33
+        OrderCreator().place_order(self.basket)
36 34
 
37 35
         alerts = StockAlert.objects.all()
38 36
         self.assertEqual(0, len(alerts))
39 37
 
40
-    def test_alert_created_when_threhold_met(self):
38
+    def test_raises_alert_when_threshold_is_reached(self):
41 39
         self.set_threshold(5)
42
-        order = OrderCreator().place_order(self.basket)
40
+        OrderCreator().place_order(self.basket)
43 41
 
44
-        alerts = StockAlert.objects.filter(stockrecord=self.product.stockrecord)
42
+        alerts = StockAlert.objects.filter(
43
+            stockrecord=self.product.stockrecord)
45 44
         self.assertEqual(1, len(alerts))
46 45
 
47
-    def test_alert_only_created_when_no_alert_exists_already(self):
46
+    def test_only_raises_an_alert_once(self):
48 47
         self.set_threshold(5)
49 48
         StockAlert.objects.create(stockrecord=self.product.stockrecord,
50 49
                                   threshold=10)
51
-        order = OrderCreator().place_order(self.basket)
50
+        OrderCreator().place_order(self.basket)
52 51
 
53
-        alerts = StockAlert.objects.filter(stockrecord=self.product.stockrecord)
52
+        alerts = StockAlert.objects.filter(
53
+            stockrecord=self.product.stockrecord)
54 54
         self.assertEqual(1, len(alerts))
55 55
 
56
-    def test_alert_closed_when_stock_replenished(self):
56
+
57
+class TestRestockingProduct(TestCase):
58
+
59
+    def setUp(self):
60
+        self.product = create_product(price=D('12.00'), num_in_stock=5)
61
+        self.basket = Basket()
62
+        self.basket.add_product(self.product)
63
+
64
+    def set_threshold(self, threshold):
65
+        self.product.stockrecord.low_stock_threshold = threshold
66
+        self.product.stockrecord.save()
67
+
68
+    def test_closes_open_alert(self):
57 69
         self.set_threshold(5)
58
-        order = OrderCreator().place_order(self.basket)
70
+        OrderCreator().place_order(self.basket)
59 71
 
60 72
         alert = StockAlert.objects.get(stockrecord=self.product.stockrecord)
61 73
         self.assertEqual(StockAlert.OPEN, alert.status)
62 74
 
75
+        # Restock product
63 76
         self.product.stockrecord.num_in_stock = 15
64 77
         self.product.stockrecord.save()
78
+
65 79
         alert = StockAlert.objects.get(stockrecord=self.product.stockrecord)
66 80
         self.assertEqual(StockAlert.CLOSED, alert.status)

+ 0
- 23
tests/unit/partner/model_tests.py Просмотреть файл

@@ -1,5 +1,4 @@
1 1
 from decimal import Decimal as D
2
-import datetime
3 2
 
4 3
 from django.test import TestCase
5 4
 
@@ -66,28 +65,6 @@ class TestStockRecord(TestCase):
66 65
         self.assertEqual(10, self.stockrecord.max_purchase_quantity())
67 66
 
68 67
 
69
-class DefaultWrapperTests(TestCase):
70
-
71
-    def test_default_wrapper_for_in_stock(self):
72
-        product = create_product(price=D('10.00'), partner="Acme", num_in_stock=10)
73
-        self.assertEquals("In stock (10 available)", product.stockrecord.availability)
74
-        self.assertEqual("instock", product.stockrecord.availability_code)
75
-
76
-    def test_default_wrapper_for_out_of_stock(self):
77
-        product = create_product(price=D('10.00'), partner="Acme", num_in_stock=0)
78
-        self.assertEquals(u"Not available",
79
-                          unicode(product.stockrecord.availability))
80
-        self.assertEqual("outofstock", product.stockrecord.availability_code)
81
-
82
-    def test_dispatch_date_for_in_stock(self):
83
-        product = create_product(price=D('10.00'), partner="Acme", num_in_stock=1)
84
-        self.assertIsNone(product.stockrecord.dispatch_date)
85
-
86
-    def test_dispatch_date_for_out_of_stock(self):
87
-        product = create_product(price=D('10.00'), partner="Acme", num_in_stock=0)
88
-        self.assertIsNone(product.stockrecord.dispatch_date)
89
-
90
-
91 68
 class CustomWrapperTests(TestCase):
92 69
 
93 70
     def setUp(self):

+ 143
- 67
tests/unit/partner/wrapper_tests.py Просмотреть файл

@@ -1,77 +1,153 @@
1
-import datetime
1
+from decimal import Decimal as D
2 2
 
3 3
 from django.test import TestCase
4
-from mock import patch
5 4
 
6 5
 from oscar.apps.partner.wrappers import DefaultWrapper
6
+from oscar.apps.catalogue.models import Product, ProductClass
7 7
 from oscar.apps.partner.models import StockRecord
8
-from oscar.test.helpers import create_product
9 8
 
10 9
 
11
-class TestDefaultWrapper(TestCase):
10
+class TestStockRecordWithNullStockLevel(TestCase):
11
+    """
12
+    Stock record with num_in_stock=None
13
+    """
12 14
 
13 15
     def setUp(self):
14 16
         self.wrapper = DefaultWrapper()
15
-        self.product = create_product()
16
-
17
-    def test_num_in_stock_is_none_is_available_to_buy(self):
18
-        record = StockRecord(num_in_stock=None, product=self.product)
19
-        self.assertTrue(self.wrapper.is_available_to_buy(record))
20
-
21
-    def test_zero_stock_is_not_available_to_buy(self):
22
-        record = StockRecord(num_in_stock=0, product=self.product)
23
-        self.assertFalse(self.wrapper.is_available_to_buy(record))
24
-
25
-    def test_nonzero_stock_is_available_to_buy(self):
26
-        record = StockRecord(num_in_stock=10, product=self.product)
27
-        self.assertTrue(self.wrapper.is_available_to_buy(record))
28
-
29
-    def test_matching_purchase_is_permitted(self):
30
-        record = StockRecord(num_in_stock=4, product=self.product)
31
-        result, reason = self.wrapper.is_purchase_permitted(record, quantity=4)
32
-        self.assertTrue(result)
33
-
34
-    def test_smaller_purchase_is_permitted(self):
35
-        record = StockRecord(num_in_stock=4, product=self.product)
36
-        result, reason = self.wrapper.is_purchase_permitted(record, quantity=3)
37
-        self.assertTrue(result)
38
-
39
-    def test_too_large_purchase_is_not_permitted(self):
40
-        record = StockRecord(num_in_stock=4, product=self.product)
41
-        result, reason = self.wrapper.is_purchase_permitted(record, quantity=5)
42
-        self.assertFalse(result)
43
-
44
-    def test_max_purchase_quantity(self):
45
-        record = StockRecord(num_in_stock=4, product=self.product)
46
-        self.assertEqual(record.num_in_stock, self.wrapper.max_purchase_quantity(record))
47
-
48
-    def test_availability_code_for_in_stock(self):
49
-        record = StockRecord(num_in_stock=4, product=self.product)
50
-        self.assertEqual('instock', self.wrapper.availability_code(record))
51
-
52
-    def test_availability_code_for_zero_stock(self):
53
-        record = StockRecord(num_in_stock=0, product=self.product)
54
-        self.assertEqual('outofstock', self.wrapper.availability_code(record))
55
-
56
-    def test_availability_code_for_null_stock_but_available(self):
57
-        record = StockRecord(num_in_stock=None, product=self.product)
58
-        self.assertEqual('available', self.wrapper.availability_code(record))
59
-
60
-    def test_availability_message_for_in_stock(self):
61
-        record = StockRecord(num_in_stock=4, product=self.product)
62
-        self.assertEqual(u'In stock (4 available)', unicode(self.wrapper.availability(record)))
63
-
64
-    def test_availability_message_for_available(self):
65
-        record = StockRecord(num_in_stock=None, product=self.product)
66
-        self.assertEqual(u'Available', unicode(self.wrapper.availability(record)))
67
-
68
-    def test_availability_message_for_out_of_stock(self):
69
-        record = StockRecord(num_in_stock=0, product=self.product)
70
-        self.assertEqual(u'Not available', unicode(self.wrapper.availability(record)))
71
-
72
-    def test_backorder_purchase_is_permitted(self):
73
-        record = StockRecord(num_in_stock=None, product=self.product)
74
-        with patch.object(self.wrapper, 'max_purchase_quantity') as m:
75
-            m.return_value = None
76
-            result, reason = self.wrapper.is_purchase_permitted(record)
77
-            self.assertTrue(result)
17
+        self.product = Product()
18
+        self.product.product_class = ProductClass()
19
+        self.record = StockRecord(num_in_stock=None, product=self.product)
20
+
21
+    def test_is_available_to_buy(self):
22
+        self.assertTrue(self.wrapper.is_available_to_buy(self.record))
23
+
24
+    def test_permits_purchase(self):
25
+        is_permitted, reason = self.wrapper.is_purchase_permitted(
26
+            self.record)
27
+        self.assertTrue(is_permitted)
28
+
29
+    def test_has_no_max_purchase_quantity(self):
30
+        self.assertIsNone(self.wrapper.max_purchase_quantity(self.record))
31
+
32
+    def test_returns_available_code(self):
33
+        self.assertEqual(DefaultWrapper.CODE_AVAILABLE,
34
+                         self.wrapper.availability_code(self.record))
35
+
36
+    def test_returns_correct_availability_message(self):
37
+        self.assertEqual("Available",
38
+                         self.wrapper.availability(self.record))
39
+
40
+    def test_returns_no_estimated_dispatch_date(self):
41
+        self.assertIsNone(self.wrapper.dispatch_date(self.record))
42
+
43
+    def test_returns_zero_tax(self):
44
+        self.assertEqual(D('0.00'), self.wrapper.calculate_tax(self.record))
45
+
46
+
47
+class TestStockRecordOfDigitalProduct(TestCase):
48
+
49
+    def setUp(self):
50
+        self.wrapper = DefaultWrapper()
51
+        self.product = Product()
52
+        self.product.product_class = ProductClass(track_stock=False)
53
+        self.record = StockRecord(num_in_stock=None, product=self.product)
54
+
55
+    def test_is_available_to_buy(self):
56
+        self.assertTrue(self.wrapper.is_available_to_buy(self.record))
57
+
58
+    def test_permits_purchase(self):
59
+        is_permitted, reason = self.wrapper.is_purchase_permitted(
60
+            self.record)
61
+        self.assertTrue(is_permitted)
62
+
63
+    def test_has_no_max_purchase_quantity(self):
64
+        self.assertIsNone(self.wrapper.max_purchase_quantity(self.record))
65
+
66
+    def test_returns_available_code(self):
67
+        self.assertEqual(DefaultWrapper.CODE_AVAILABLE,
68
+                         self.wrapper.availability_code(self.record))
69
+
70
+    def test_returns_correct_availability_message(self):
71
+        self.assertEqual("Available",
72
+                         self.wrapper.availability(self.record))
73
+
74
+    def test_returns_no_estimated_dispatch_date(self):
75
+        self.assertIsNone(self.wrapper.dispatch_date(self.record))
76
+
77
+    def test_returns_zero_tax(self):
78
+        self.assertEqual(D('0.00'), self.wrapper.calculate_tax(self.record))
79
+
80
+
81
+class TestStockRecordOfZeroStockProduct(TestCase):
82
+
83
+    def setUp(self):
84
+        self.wrapper = DefaultWrapper()
85
+        self.product = Product()
86
+        self.product.product_class = ProductClass()
87
+        self.record = StockRecord(num_in_stock=0, product=self.product)
88
+
89
+    def test_is_not_available_to_buy(self):
90
+        self.assertFalse(self.wrapper.is_available_to_buy(self.record))
91
+
92
+    def test_does_not_permit_purchase(self):
93
+        is_permitted, reason = self.wrapper.is_purchase_permitted(
94
+            self.record)
95
+        self.assertFalse(is_permitted)
96
+
97
+    def test_has_zero_max_purchase_quantity(self):
98
+        self.assertEqual(0, self.wrapper.max_purchase_quantity(self.record))
99
+
100
+    def test_returns_unavailable_code(self):
101
+        self.assertEqual(DefaultWrapper.CODE_UNAVAILABLE,
102
+                         self.wrapper.availability_code(self.record))
103
+
104
+    def test_returns_correct_availability_message(self):
105
+        self.assertEqual("Not available",
106
+                         self.wrapper.availability(self.record))
107
+
108
+    def test_returns_no_estimated_dispatch_date(self):
109
+        self.assertIsNone(self.wrapper.dispatch_date(self.record))
110
+
111
+    def test_returns_zero_tax(self):
112
+        self.assertEqual(D('0.00'), self.wrapper.calculate_tax(self.record))
113
+
114
+
115
+class TestStockRecordWithPositiveStock(TestCase):
116
+
117
+    def setUp(self):
118
+        self.wrapper = DefaultWrapper()
119
+        self.product = Product()
120
+        self.product.product_class = ProductClass()
121
+        self.record = StockRecord(num_in_stock=5, product=self.product)
122
+
123
+    def test_is_available_to_buy(self):
124
+        self.assertTrue(self.wrapper.is_available_to_buy(self.record))
125
+
126
+    def test_does_permit_purchase_for_smaller_quantities(self):
127
+        for x in range(1, 6):
128
+            is_permitted, reason = self.wrapper.is_purchase_permitted(
129
+                self.record, quantity=x)
130
+            self.assertTrue(is_permitted)
131
+
132
+    def test_does_not_permit_purchase_for_larger_quantities(self):
133
+        for x in range(6, 10):
134
+            is_permitted, reason = self.wrapper.is_purchase_permitted(
135
+                self.record, quantity=x)
136
+            self.assertFalse(is_permitted)
137
+
138
+    def test_has_correct_max_purchase_quantity(self):
139
+        self.assertEqual(5, self.wrapper.max_purchase_quantity(self.record))
140
+
141
+    def test_returns_available_code(self):
142
+        self.assertEqual(DefaultWrapper.CODE_IN_STOCK,
143
+                         self.wrapper.availability_code(self.record))
144
+
145
+    def test_returns_correct_availability_message(self):
146
+        self.assertEqual("In stock (5 available)",
147
+                         self.wrapper.availability(self.record))
148
+
149
+    def test_returns_no_estimated_dispatch_date(self):
150
+        self.assertIsNone(self.wrapper.dispatch_date(self.record))
151
+
152
+    def test_returns_zero_tax(self):
153
+        self.assertEqual(D('0.00'), self.wrapper.calculate_tax(self.record))

Загрузка…
Отмена
Сохранить