Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Integrating use of Values() in AutoFixture class.

  • Loading branch information...
commit c0dbf0f9d2f1753789177b32fc4c0ba3e4fc0355 1 parent ab8b3e0
@gregmuellegger authored
Showing with 35 additions and 12 deletions.
  1. +20 −10 autofixture/base.py
  2. +15 −2 autofixture_tests/autofixture_test/tests.py
View
30 autofixture/base.py
@@ -4,6 +4,7 @@
from django.db.models.fields import related
from django.utils.datastructures import SortedDict
from autofixture import constraints, generators, signals
+from autofixture.values import Values
class CreateInstanceError(Exception):
@@ -58,7 +59,18 @@ def get_deep_links(self, field):
return Link(fields, default=self.default)
-class AutoFixture(object):
+class AutoFixtureMetaclass(type):
+ def __new__(mcs, name, bases, attrs):
+ values = Values()
+ for base in bases[::-1]:
+ values += base.field_values
+ values += Values(attrs.pop('Values', {}))
+ values += attrs.get('field_values', Values())
+ attrs['field_values'] = values
+ return super(AutoFixtureMetaclass, mcs).__new__(mcs, name, bases, attrs)
+
+
+class AutoFixtureBase(object):
'''
.. We don't support the following fields yet:
@@ -71,9 +83,6 @@ class AutoFixture(object):
class IGNORE_FIELD(object):
pass
- class Values(object):
- pass
-
overwrite_defaults = False
follow_fk = True
generate_fk = False
@@ -97,7 +106,7 @@ class Values(object):
(fields.TimeField, generators.TimeGenerator),
))
- field_values = {}
+ field_values = Values()
default_constraints = [
constraints.unique_constraint,
@@ -149,11 +158,8 @@ def __init__(self, model,
will be ignored if this parameter is set.
'''
self.model = model
- self.field_values = dict((
- (k,v) for k,v in self.Values.__dict__.items()
- if k[:2] != '__' and k[-2:] != '__' and not k.startswith('_Values__')))
- self.field_values.update(self.__class__.field_values.copy())
- self.field_values.update(field_values or {})
+ self.field_values = Values(self.__class__.field_values)
+ self.field_values += Values(field_values)
self.constraints = constraints or []
if none_chance is not None:
self.none_chance = none_chance
@@ -485,3 +491,7 @@ def iter(self, count=1, commit=True):
def __iter__(self):
yield self.create_one()
+
+
+class AutoFixture(AutoFixtureBase):
+ __metaclass__ = AutoFixtureMetaclass
View
17 autofixture_tests/autofixture_test/tests.py
@@ -5,6 +5,7 @@
from django.test import TestCase
from autofixture import generators
from autofixture.base import AutoFixture, CreateInstanceError, Link
+from autofixture.values import Values
from autofixture_tests.autofixture_test.models import y2k
from autofixture_tests.autofixture_test.models import (
SimpleModel, OtherSimpleModel, DeepLinkModel1, DeepLinkModel2,
@@ -18,10 +19,19 @@ class SimpleAutoFixture(AutoFixture):
}
-class BasicValueFixture(AutoFixture):
+class BasicValueFixtureBase(AutoFixture):
+ field_values = Values(blankchars='bar')
+
+
+class BasicValueFixture(BasicValueFixtureBase):
class Values:
chars = 'foo'
- shortchars = lambda: 'a'
+ shortchars = staticmethod(lambda: 'a')
+ intfield = generators.IntegerGenerator(min_value=1, max_value=13)
+
+ field_values = {
+ 'nullchars': 'spam',
+ }
class TestBasicModel(TestCase):
@@ -387,6 +397,9 @@ def test_values_class(self):
for obj in autofixture.create(BasicModel, 10):
self.assertEqual(obj.chars, 'foo')
self.assertEqual(obj.shortchars, 'a')
+ self.assertEqual(obj.blankchars, 'bar')
+ self.assertEqual(obj.nullchars, 'spam')
+ self.assertTrue(1 <= obj.intfield <= 13)
class TestManagementCommand(TestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.