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

Add failing test for traits chaining along with RelatedFactory and SubFactory #1066

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions tests/test_using.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,59 @@ class Params:
dict(one=None, two=False, three=None, four=True, five=None),
)

def test_chaining_traits_and_related_with_nested_factories(self):
class TestRelatedObject:
def __init__(self, obj=None, attr=None, nested=None):
obj.related = self
self.nested = nested
self.attr = attr

class TestNestedObject:
def __init__(self, attr=None):
self.attr = attr

class TestNestedObjectFactory(factory.Factory):
class Meta:
model = TestNestedObject
attr = 1

class TestRelatedObjectFactory(factory.Factory):
class Meta:
model = TestRelatedObject
attr = 1

nested = factory.SubFactory(TestNestedObjectFactory, attr=None)

class TestObjectFactory(factory.Factory):
class Meta:
model = TestObject

class Params:
with_related = factory.Trait(
related_obj=factory.RelatedFactory(
TestRelatedObjectFactory,
factory_related_name='obj',
nested__attr=2,
),
)
with_related_nested_override = factory.Trait(
with_related=True,
related_obj__nested__attr=3,
)

obj = TestObjectFactory.build(with_related_nested_override=True)
self.assertEqual(1, obj.related.attr)
self.assertEqual(3, obj.related.nested.attr)
obj = TestObjectFactory.build(with_related_nested_override=True, related_obj__nested__attr=4)
self.assertEqual(1, obj.related.attr)
self.assertEqual(4, obj.related.nested.attr)
obj = TestObjectFactory.build(with_related=True)
self.assertEqual(1, obj.related.attr)
self.assertEqual(2, obj.related.nested.attr)
obj = TestObjectFactory.build(with_related_nested_override=False)
with self.assertRaises(AttributeError):
obj.related

def test_prevent_cyclic_traits(self):

with self.assertRaises(errors.CyclicDefinitionError):
Expand Down
Loading