Permalink
Browse files

ChoiceField remake

  • Loading branch information...
1 parent 45fcf44 commit 2354de0ea817ff5dd733365ce7cfede17a90c9b6 Mikko Hellsing committed Oct 18, 2010
Showing with 25 additions and 20 deletions.
  1. +4 −0 CHANGES.rst
  2. +1 −1 mockups/factory.py
  3. +16 −18 mockups/generators.py
  4. +1 −0 mockups_tests/mockups_test/models.py
  5. +3 −1 mockups_tests/mockups_test/tests.py
View
@@ -1,6 +1,10 @@
Changelog
=========
+0.4.4
+-----
+* Rename and remake ChoicesGenerator -> ChoiceGenerator + ChoiceFieldGenerator
+
0.4.3
-----
* Make generators take empty_value keyword argument
View
@@ -82,7 +82,7 @@ def get_generator(self, field, **kwargs):
if field.name in self.fieldname_to_generator:
obj = self.fieldname_to_generator[field.name]
elif field.choices:
- return generators.ChoicesGenerator(choices=field.choices)
+ return generators.ChoiceFieldGenerator(field)
elif field.default is not models.NOT_PROVIDED:
return None # bail out
elif field.__class__ in self.fieldclass_to_generator:
View
@@ -206,31 +206,24 @@ class PositiveSmallIntegerGenerator(SmallIntegerGenerator):
min_value = 0
-class ChoicesGenerator(Generator):
- def __init__(self, choices=(), values=(), *args, **kwargs):
- assert len(choices) or len(values)
- self.choices = list(choices)
- if not values:
- self.values = [k for k, v in self.choices]
- else:
- self.values = list(values)
- super(ChoicesGenerator, self).__init__(*args, **kwargs)
+class ChoiceGenerator(Generator):
+ choices = []
+
+ def __init__(self, choices=None, *args, **kwargs):
+ if choices is not None:
+ self.choices = choices
+ super(ChoiceGenerator, self).__init__(*args, **kwargs)
def generate(self):
- return random.choice(self.values)
+ return random.choice(self.choices)
-class BooleanGenerator(ChoicesGenerator):
- def __init__(self, none=False, *args, **kwargs):
- values = (True, False)
- if none:
- values = values + (None,)
- super(BooleanGenerator, self).__init__(values=values, *args, **kwargs)
+class BooleanGenerator(ChoiceGenerator):
+ choices = (True, False)
class NullBooleanGenerator(BooleanGenerator):
- def __init__(self, none=True, *args, **kwargs):
- super(NullBooleanGenerator, self).__init__(none=none, *args, **kwargs)
+ empty_p = 1 / 3.0
class DateTimeGenerator(Generator):
@@ -516,6 +509,11 @@ def generate(self):
return self._generator.generate()
+class ChoiceFieldGenerator(FieldGenerator):
+ def get_generator(self, field, *args, **kwargs):
+ return ChoiceGenerator([k for k, v in field.choices])
+
+
class ForeignKeyFieldGenerator(FieldGenerator):
def get_generator(self, field, generate_fk=None, follow_fk=None, **kwargs):
# if generate_fk is set, follow_fk is ignored.
@@ -39,6 +39,7 @@ class BasicModel(models.Model):
nullchars = models.CharField(max_length=100, blank=True, null=True)
slugfield = models.SlugField()
textfield = models.TextField()
+ nullbool = models.NullBooleanField()
defaultint = models.IntegerField(default=1)
intfield = models.IntegerField()
@@ -63,7 +63,8 @@ def test_factory(self):
char_values = (u'a', u'b')
class GF(Factory):
intfield = generators.StaticGenerator(1)
- chars = generators.ChoicesGenerator(values=char_values)
+ chars = generators.ChoiceGenerator(char_values)
+ nullbool = generators.NullBooleanGenerator
shortchars = generators.CallableGenerator(lambda: u'ab')
class M(ModelMockup):
factory = GF
@@ -72,6 +73,7 @@ class M(ModelMockup):
for obj in filler.create(100):
self.assertEqual(obj.intfield, int_value)
self.assertTrue(obj.chars in char_values)
+ self.assertTrue(obj.nullbool in (False, True, None))
self.assertEqual(obj.shortchars, u'ab')

0 comments on commit 2354de0

Please sign in to comment.