Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow functions as models #1072

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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