Skip to content

Commit

Permalink
Allow functions as FactoryOptions.model
Browse files Browse the repository at this point in the history
Facilitates the use of a factory function, that may create several
related objects at once.
  • Loading branch information
instanceofmel authored and francoisfreitag committed Jul 3, 2024
1 parent d6349de commit 01a864c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ChangeLog

- :issue:`1031`: Do not require :attr:`~factory.alchemy.SQLAlchemyOptions.sqlalchemy_session` when
:attr:`~factory.alchemy.SQLAlchemyOptions.sqlalchemy_session_factory` is provided.
- :issue:`1072`: Allow using functions as models for a :attr:`~factory.FactoryOptions.model`.

*Removed:*

Expand Down
2 changes: 2 additions & 0 deletions factory/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import collections
import inspect
import logging
import warnings
from typing import Generic, List, Type, TypeVar
Expand Down Expand Up @@ -243,6 +244,7 @@ def _get_counter_reference(self):
if (self.model is not None
and self.base_factory is not None
and self.base_factory._meta.model is not None
and inspect.isclass(self.model)
and issubclass(self.model, self.base_factory._meta.model)):
return self.base_factory._meta.counter_reference
else:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ class Meta:
ones = {x.one for x in (parent, alt_parent, sub, alt_sub)}
self.assertEqual(4, len(ones))

def test_inheritance_with_function_as_meta_model(self):
def make_test_object(**kwargs):
return TestObject(**kwargs)

class TestObjectFactory(base.Factory):
class Meta:
model = make_test_object

one = "foo"

class TestSubFactory(TestObjectFactory):
one = "bar"

sub = TestSubFactory.build()
self.assertEqual(sub.one, "bar")


class FactorySequenceTestCase(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 01a864c

Please sign in to comment.