diff options
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | docs/fuzzy.rst | 8 | ||||
-rw-r--r-- | docs/recipes.rst | 31 | ||||
-rw-r--r-- | docs/reference.rst | 5 | ||||
-rw-r--r-- | factory/fuzzy.py | 3 | ||||
-rw-r--r-- | tests/test_using.py | 13 |
6 files changed, 58 insertions, 3 deletions
diff --git a/.travis.yml b/.travis.yml index ff805b0..938787b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +sudo: false language: python python: diff --git a/docs/fuzzy.rst b/docs/fuzzy.rst index 6b06608..5b03ec6 100644 --- a/docs/fuzzy.rst +++ b/docs/fuzzy.rst @@ -3,6 +3,12 @@ Fuzzy attributes .. module:: factory.fuzzy +.. note:: Now that FactoryBoy includes the :class:`factory.Faker` class, most of + these built-in fuzzers are deprecated in favor of their + `Faker <http://www.joke2k.net/faker/>`_ equivalents. Further + discussion here: + `<https://github.com/rbarrois/factory_boy/issues/271/>`_ + Some tests may be interested in testing with fuzzy, random values. This is handled by the :mod:`factory.fuzzy` module, which provides a few @@ -199,7 +205,7 @@ FuzzyDate FuzzyDateTime ------------- -.. class:: FuzzyDateTime(start_dt[, end_dt], tz=UTC, force_year=None, force_month=None, force_day=None, force_hour=None, force_minute=None, force_second=None, force_microsecond=None) +.. class:: FuzzyDateTime(start_dt[, end_dt], force_year=None, force_month=None, force_day=None, force_hour=None, force_minute=None, force_second=None, force_microsecond=None) The :class:`FuzzyDateTime` fuzzer generates random timezone-aware datetime within a given inclusive range. diff --git a/docs/recipes.rst b/docs/recipes.rst index a627e8b..a3df7be 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -444,3 +444,34 @@ Forcing the initial value for all projects >>> Account.objects.create(uid=42, name="Blah") >>> AccountFactory.create() # Sets up the account number based on the latest uid <Account uid=43, name=Test> + + +Converting a factory's output to a dict +--------------------------------------- + +In order to inject some data to, say, a REST API, it can be useful to fetch the factory's data +as a dict. + +Internally, a factory will: + +1. Merge declarations and overrides from all sources (class definition, call parameters, ...) +2. Resolve them into a dict +3. Pass that dict as keyword arguments to the model's ``build`` / ``create`` function + + +In order to get a dict, we'll just have to swap the model; the easiest way is to use +:meth:`factory.build`: + +.. code-block:: python + + class UserFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.User + + first_name = factory.Sequence(lambda n: "Agent %03d" % n) + username = factory.Faker('username') + +.. code-block:: pycon + + >>> factory.build(dict, FACTORY_CLASS=UserFactory) + {'first_name': "Agent 001", 'username': 'john_doe'} diff --git a/docs/reference.rst b/docs/reference.rst index 9e01213..e2f63db 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -747,8 +747,9 @@ The sequence counter is shared across all :class:`Sequence` attributes of the Inheritance ~~~~~~~~~~~ -When a :class:`Factory` inherits from another :class:`Factory`, their -sequence counter is shared: +When a :class:`Factory` inherits from another :class:`Factory` and the `model` +of the subclass inherits from the `model` of the parent, the sequence counter +is shared across the :class:`Factory` classes: .. code-block:: python diff --git a/factory/fuzzy.py b/factory/fuzzy.py index a7e834c..71d1884 100644 --- a/factory/fuzzy.py +++ b/factory/fuzzy.py @@ -217,6 +217,9 @@ class BaseFuzzyDateTime(BaseFuzzyAttribute): """%s boundaries should have start <= end, got %r > %r""" % ( self.__class__.__name__, start_dt, end_dt)) + def _now(self): + raise NotImplementedError() + def __init__(self, start_dt, end_dt=None, force_year=None, force_month=None, force_day=None, force_hour=None, force_minute=None, force_second=None, diff --git a/tests/test_using.py b/tests/test_using.py index 0a893c1..3ef5403 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -292,6 +292,19 @@ class SimpleBuildTestCase(unittest.TestCase): self.assertEqual(obj.three, 5) self.assertEqual(obj.four, None) + def test_build_to_dict(self): + # We have a generic factory + class TestObjectFactory(factory.Factory): + class Meta: + model = TestObject + + one = 'one' + two = factory.LazyAttribute(lambda o: o.one * 2) + + # Now, get a dict out of it + obj = factory.build(dict, FACTORY_CLASS=TestObjectFactory) + self.assertEqual({'one': 'one', 'two': 'oneone'}, obj) + class UsingFactoryTestCase(unittest.TestCase): def test_attribute(self): |