|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+from decimal import Decimal as D
|
|
|
2
|
+
|
|
|
3
|
+from django.test import TestCase
|
|
|
4
|
+from django.contrib.auth.models import User
|
|
|
5
|
+from django_dynamic_fixture import G
|
|
|
6
|
+import mock
|
|
|
7
|
+
|
|
|
8
|
+from oscar.apps.offer import custom, models
|
|
|
9
|
+from oscar.apps.basket.models import Basket
|
|
|
10
|
+
|
|
|
11
|
+
|
|
|
12
|
+class ChangesOwnerName(models.Benefit):
|
|
|
13
|
+
|
|
|
14
|
+ class Meta:
|
|
|
15
|
+ proxy = True
|
|
|
16
|
+
|
|
|
17
|
+ def apply(self, basket, condition, offer=None):
|
|
|
18
|
+ basket.owner.first_name = "Terry"
|
|
|
19
|
+ basket.owner.save()
|
|
|
20
|
+ return D('0.00')
|
|
|
21
|
+
|
|
|
22
|
+ @property
|
|
|
23
|
+ def description(self):
|
|
|
24
|
+ return 'Changes owners name'
|
|
|
25
|
+
|
|
|
26
|
+
|
|
|
27
|
+class NoDescription(models.Benefit):
|
|
|
28
|
+
|
|
|
29
|
+ class Meta:
|
|
|
30
|
+ proxy = True
|
|
|
31
|
+
|
|
|
32
|
+ def apply(self, basket, condition, offer=None):
|
|
|
33
|
+ return D('0.00')
|
|
|
34
|
+
|
|
|
35
|
+
|
|
|
36
|
+class TestACustomBenefit(TestCase):
|
|
|
37
|
+
|
|
|
38
|
+ def test_must_implement_a_description_property(self):
|
|
|
39
|
+ with self.assertRaises(RuntimeError):
|
|
|
40
|
+ custom.create_benefit(NoDescription)
|
|
|
41
|
+
|
|
|
42
|
+
|
|
|
43
|
+class TestCustomBenefit(TestCase):
|
|
|
44
|
+
|
|
|
45
|
+ def setUp(self):
|
|
|
46
|
+ self.benefit = custom.create_benefit(ChangesOwnerName)
|
|
|
47
|
+ self.condition = mock.Mock()
|
|
|
48
|
+ self.basket = Basket()
|
|
|
49
|
+
|
|
|
50
|
+ def test_applies_correctly(self):
|
|
|
51
|
+ self.basket.owner = G(User, first_name="Alan")
|
|
|
52
|
+ self.benefit.proxy().apply(self.basket, self.condition)
|
|
|
53
|
+ self.assertEqual("Terry", self.basket.owner.first_name)
|