Skip to content
Browse files

Fixed #5 -- Adding support for FloatField

1 parent c9b7896 commit 915e16c2271ca4506f3be15be9a6d38f377ba296 @gregmuellegger committed Feb 14, 2013
Showing with 25 additions and 5 deletions.
  1. +3 −5 CHANGES.rst
  2. +1 −0 autofixture/base.py
  3. +19 −0 autofixture/generators.py
  4. +2 −0 autofixture_tests/autofixture_test/models.py
View
8 CHANGES.rst
@@ -1,19 +1,17 @@
Changelog
=========
-0.3.0 (not released yet)
-------------------------
+0.3.0
+-----
* Adding better support for subclassing ``AutoFixture`` through merging of
nested ``Values`` classes.
-
* Renamed attribute and argument ``none_chance`` to better matching name ``empty_p`` for generators
and ``none_p`` for ``AutoFixture``.
-
* Fixed some issues with management command options. Thanks Mikko Hellsing for
his hard work.
-
* Fixed issues in unregister(). Thanks Mikko Hellsing for the report.
+* Adding support for ``FloatField``. Thanks to Jyr Gaxiola for the report.
0.2.5
-----
View
1 autofixture/base.py
@@ -101,6 +101,7 @@ class IGNORE_FIELD(object):
(fields.PositiveIntegerField, generators.PositiveIntegerGenerator),
(fields.SmallIntegerField, generators.SmallIntegerGenerator),
(fields.IntegerField, generators.IntegerGenerator),
+ (fields.FloatField, generators.FloatGenerator),
(fields.IPAddressField, generators.IPAddressGenerator),
(fields.TextField, generators.LoremGenerator),
(fields.TimeField, generators.TimeGenerator),
View
19 autofixture/generators.py
@@ -190,6 +190,25 @@ class PositiveSmallIntegerGenerator(SmallIntegerGenerator):
min_value = 0
+class FloatGenerator(IntegerGenerator):
+ coerce_type = float
+ decimal_digits = 1
+
+ def __init__(self, decimal_digits=None, *args, **kwargs):
+ if decimal_digits is not None:
+ self.decimal_digits = decimal_digits
+ super(IntegerGenerator, self).__init__(*args, **kwargs)
+
+ def generate(self):
+ value = super(FloatGenerator, self).generate()
+ value = float(value)
+ if self.decimal_digits:
+ digits = random.randint(1, 10 ^ self.decimal_digits) - 1
+ digits = float(digits)
+ value = value + digits / (10 ^ self.decimal_digits)
+ return value
+
+
class ChoicesGenerator(Generator):
def __init__(self, choices=(), values=(), *args, **kwargs):
assert len(choices) or len(values)
View
2 autofixture_tests/autofixture_test/models.py
@@ -39,6 +39,8 @@ class BasicModel(models.Model):
nullchars = models.CharField(max_length=100, blank=True, null=True)
slugfield = models.SlugField()
textfield = models.TextField()
+ blankfloatfield = models.FloatField(null=True, blank=True)
+ floatfield = models.FloatField()
defaultint = models.IntegerField(default=1)
intfield = models.IntegerField()

0 comments on commit 915e16c

Please sign in to comment.
Something went wrong with that request. Please try again.