diff --git a/docs/introduction.rst b/docs/introduction.rst index 82cc5478..b46f1b37 100644 --- a/docs/introduction.rst +++ b/docs/introduction.rst @@ -337,7 +337,7 @@ All factories support two built-in strategies: * ``build`` provides a local object * ``create`` instantiates a local object, and saves it to the database. -* ``create async`` similar to ``create`` but can run asynchronous code. +* ``create_async`` similar to ``create`` but can run asynchronous code. .. note:: For 1.X versions, the ``create`` will actually call ``AssociatedClass.objects.create``, as for a Django model. diff --git a/docs/reference.rst b/docs/reference.rst index f2399e18..52ecdff6 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -203,7 +203,7 @@ Attributes and methods .. classmethod:: build_batch(cls, size, **kwargs) - Provides a list of :obj:`size` instances from the :class:`Factory`, + Provides a list of ``size`` instances from the :class:`Factory`, through the 'build' strategy. @@ -213,18 +213,18 @@ Attributes and methods .. classmethod:: create_batch(cls, size, **kwargs) - Provides a list of :obj:`size` instances from the :class:`Factory`, + Provides a list of ``size`` instances from the :class:`Factory`, through the 'create' strategy. .. classmethod:: create_async(cls, **kwargs) - Provides a new object, using the `create_async` strategy. + Provides a new object, using the ``create_async`` strategy. .. classmethod:: create_async_batch(cls, size, **kwargs) - Provides a list of :obj:`size` instances from the :class:`Factory`, - through the `create_async` strategy. + Asynchronously provides a list of ``size`` instances from the :class:`Factory`, + through the ``create_async`` strategy. .. classmethod:: stub(cls, **kwargs) @@ -415,7 +415,7 @@ Attributes and methods .. class:: AsyncFactory -Similar to the :class:`Factory` class but with `create_async` as default strategy. +Similar to the :class:`Factory` class but with ``create_async`` as default strategy. .. _parameters: @@ -629,13 +629,13 @@ factory_boy supports two main strategies for generating instances, plus stubs. :class:`Factory` wasn't overridden. -.. data:: CREATE_ASYNC_STRATEGY +.. data:: ASYNC_CREATE_STRATEGY - The `create_async` strategy is similar to the `create` strategy but asynchronous. + The ``create_async`` strategy is similar to the ``create`` strategy but asynchronous. This is the default strategy for subclasses of :class:`AsyncFactory`. - Default behavior is to call :meth:`~Factory._create`, this can be overridden in :meth:`_create_model_async`. + Default behavior is to call :meth:`~Factory._create`, this can be overridden in :meth:`Factory._create_model_async`. .. function:: use_strategy(strategy) diff --git a/factory/base.py b/factory/base.py index f264b597..cb32a1b2 100644 --- a/factory/base.py +++ b/factory/base.py @@ -327,7 +327,7 @@ def instantiate(self, step, args, kwargs): def use_postgeneration_results(self, step, instance, results): self.factory._after_postgeneration( instance, - create=step.builder.strategy != enums.BUILD_STRATEGY, + create=step.builder.strategy in (enums.CREATE_STRATEGY, enums.ASYNC_CREATE_STRATEGY), results=results, ) diff --git a/factory/declarations.py b/factory/declarations.py index 846de611..d9a7234e 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -679,7 +679,7 @@ def call(self, instance, step, context): context._asdict(), ), ) - create = step.builder.strategy != enums.BUILD_STRATEGY + create = step.builder.strategy in (enums.CREATE_STRATEGY, enums.ASYNC_CREATE_STRATEGY) return self.function( instance, create, context.value, **context.extra) diff --git a/tests/test_alchemy.py b/tests/test_alchemy.py index d4f55a1c..7bf93f9b 100644 --- a/tests/test_alchemy.py +++ b/tests/test_alchemy.py @@ -390,8 +390,8 @@ async def test(): statement = sqlalchemy.select( sqlalchemy.func.count(models.NoteModel.id) - ).where(models.NoteModel.text == "Text 0") + ) count = await models.async_session.scalar(statement) - assert count == 1 + self.assertEqual(count, 1) asyncio.run(test()) diff --git a/tests/test_using.py b/tests/test_using.py index 9ad3a6cd..a9fb5889 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -847,7 +847,7 @@ class Meta: async def test(): test_model = await TestModelFactory.create_async() self.assertEqual(test_model.one, 'one') - self.assertTrue(create_marker, test_model.id) + self.assertEqual(create_marker, test_model.id) asyncio.run(test()) @@ -866,7 +866,7 @@ async def test(): for i, obj in enumerate(objs): self.assertEqual('one', obj.one) self.assertEqual(i, obj.two) - self.assertTrue(obj.id) + self.assertEqual(obj.id, create_marker) asyncio.run(test()) def test_generate_build(self): @@ -966,7 +966,7 @@ async def test(): for i, obj in enumerate(objs): self.assertEqual('one', obj.one) self.assertEqual('two', obj.two) - self.assertTrue(obj.id) + self.assertEqual(obj.id, create_marker) asyncio.run(test())