Skip to content

Commit

Permalink
Fixed #5 -- Adding support for FloatField
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmuellegger committed Feb 14, 2013
1 parent c9b7896 commit 915e16c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
8 changes: 3 additions & 5 deletions 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
-----
Expand Down
1 change: 1 addition & 0 deletions autofixture/base.py
Expand Up @@ -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),
Expand Down
19 changes: 19 additions & 0 deletions autofixture/generators.py
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions autofixture_tests/autofixture_test/models.py
Expand Up @@ -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()
Expand Down

0 comments on commit 915e16c

Please sign in to comment.