Parcourir la source

fixed error in validating option group values

master
Sebastian Vetter il y a 13 ans
Parent
révision
5bab67616a
2 fichiers modifiés avec 57 ajouts et 20 suppressions
  1. 1
    1
      oscar/apps/catalogue/abstract_models.py
  2. 56
    19
      tests/unit/catalogue_tests.py

+ 1
- 1
oscar/apps/catalogue/abstract_models.py Voir le fichier

@@ -584,7 +584,7 @@ class AbstractProductAttribute(models.Model):
584 584
         if not value.pk:
585 585
             raise ValidationError(_(u"AttributeOption has not been saved yet"))
586 586
         valid_values = self.option_group.options.values_list('option', flat=True)
587
-        if value not in valid_values:
587
+        if value.option not in valid_values:
588 588
             raise ValidationError(_(u"%(enum)s is not a valid choice "
589 589
                                         u"for %(attr)s") % \
590 590
                                        {'enum': value, 'attr': self})

+ 56
- 19
tests/unit/catalogue_tests.py Voir le fichier

@@ -1,8 +1,10 @@
1 1
 from django.test import TestCase
2 2
 from django.core.exceptions import ValidationError
3 3
 
4
-from oscar.apps.catalogue.models import Product, ProductClass, Category, \
5
-        ProductAttribute
4
+from oscar.apps.catalogue.models import (Product, ProductClass, Category,
5
+                                         ProductAttribute,
6
+                                         AttributeOptionGroup,
7
+                                         AttributeOption)
6 8
 from oscar.apps.catalogue.categories import create_from_breadcrumbs
7 9
 
8 10
 
@@ -26,37 +28,37 @@ class TestCategoryFactory(TestCase):
26 28
 
27 29
     def setUp(self):
28 30
         Category.objects.all().delete()
29
-    
31
+
30 32
     def test_can_create_single_level_category(self):
31 33
         trail = 'Books'
32 34
         category = create_from_breadcrumbs(trail)
33 35
         self.assertIsNotNone(category)
34 36
         self.assertEquals(category.name, 'Books')
35
-        self.assertEquals(category.slug, 'books')      
36
-    
37
+        self.assertEquals(category.slug, 'books')
38
+
37 39
     def test_can_create_parent_and_child_categories(self):
38 40
         trail = 'Books > Science-Fiction'
39 41
         category = create_from_breadcrumbs(trail)
40
-        
42
+
41 43
         self.assertIsNotNone(category)
42 44
         self.assertEquals(category.name, 'Science-Fiction')
43 45
         self.assertEquals(category.get_depth(), 2)
44 46
         self.assertEquals(category.get_parent().name, 'Books')
45 47
         self.assertEquals(2, Category.objects.count())
46 48
         self.assertEquals(category.slug, 'books/science-fiction')
47
-        
49
+
48 50
     def test_can_create_multiple_categories(self):
49 51
         trail = 'Books > Science-Fiction > Star Trek'
50 52
         create_from_breadcrumbs(trail)
51 53
         trail = 'Books > Factual > Popular Science'
52 54
         category = create_from_breadcrumbs(trail)
53
-        
55
+
54 56
         self.assertIsNotNone(category)
55 57
         self.assertEquals(category.name, 'Popular Science')
56 58
         self.assertEquals(category.get_depth(), 3)
57 59
         self.assertEquals(category.get_parent().name, 'Factual')
58 60
         self.assertEquals(5, Category.objects.count())
59
-        self.assertEquals(category.slug, 'books/factual/popular-science', )        
61
+        self.assertEquals(category.slug, 'books/factual/popular-science', )
60 62
 
61 63
     def test_can_use_alternative_separator(self):
62 64
         trail = 'Food|Cheese|Blue'
@@ -72,7 +74,7 @@ class TestCategoryFactory(TestCase):
72 74
         create_from_breadcrumbs(trail)
73 75
         trail = 'A > E > G'
74 76
         create_from_breadcrumbs(trail)
75
-        
77
+
76 78
         trail = 'T'
77 79
         target = create_from_breadcrumbs(trail)
78 80
         category = Category.objects.get(name='A')
@@ -156,30 +158,65 @@ class ProductCreationTests(ProductTests):
156 158
                           title='testing')
157 159
         product.attr.num_pages = 100
158 160
         product.save()
159
-   
161
+
160 162
 
161 163
 class TopLevelProductTests(ProductTests):
162
-    
164
+
163 165
     def test_top_level_products_must_have_titles(self):
164 166
         self.assertRaises(ValidationError, Product.objects.create, product_class=self.product_class)
165
-        
166
-        
167
+
168
+
167 169
 class VariantProductTests(ProductTests):
168
-    
170
+
169 171
     def setUp(self):
170 172
         super(VariantProductTests, self).setUp()
171 173
         self.parent = Product.objects.create(title="Parent product", product_class=self.product_class)
172
-    
174
+
173 175
     def test_variant_products_dont_need_titles(self):
174 176
         Product.objects.create(parent=self.parent, product_class=self.product_class)
175
-        
177
+
176 178
     def test_variant_products_dont_need_a_product_class(self):
177 179
         Product.objects.create(parent=self.parent)
178
-       
180
+
179 181
     def test_variant_products_inherit_parent_titles(self):
180 182
         p = Product.objects.create(parent=self.parent, product_class=self.product_class)
181 183
         self.assertEquals("Parent product", p.get_title())
182
-        
184
+
183 185
     def test_variant_products_inherit_product_class(self):
184 186
         p = Product.objects.create(parent=self.parent)
185 187
         self.assertEquals("Clothing", p.get_product_class().name)
188
+
189
+
190
+class ProductAttributeCreationTests(TestCase):
191
+
192
+    def setUp(self):
193
+        self.product_class,_ = ProductClass.objects.get_or_create(
194
+            name='Clothing'
195
+        )
196
+        self.option_group = AttributeOptionGroup.objects.create(name='group')
197
+        self.option_1 = AttributeOption.objects.create(group=self.option_group, option='first')
198
+        self.option_2 = AttributeOption.objects.create(group=self.option_group, option='second')
199
+
200
+    def test_validating_option_attribute(self):
201
+        pa = ProductAttribute.objects.create(product_class=self.product_class,
202
+                                             name='test group',
203
+                                             code='test_group',
204
+                                             type='option',
205
+                                             option_group=self.option_group)
206
+
207
+        self.assertRaises(ValidationError, pa.get_validator(), 'invalid')
208
+
209
+        try:
210
+            pa.get_validator()(self.option_1)
211
+        except ValidationError:
212
+            self.fail("valid option '%s' not validated" % self.option_1)
213
+
214
+        try:
215
+            pa.get_validator()(self.option_2)
216
+        except ValidationError:
217
+            self.fail("valid option '%s' not validated" % self.option_1)
218
+
219
+        invalid_option = AttributeOption()
220
+        invalid_option.option = 'invalid option'
221
+        self.assertRaises(ValidationError, pa.get_validator(),
222
+                          invalid_option)

Chargement…
Annuler
Enregistrer