Skip to content

Commit

Permalink
Simplify test setup
Browse files Browse the repository at this point in the history
The attribute “two” is not used. Show the override using different values.
  • Loading branch information
francoisfreitag committed Mar 24, 2024
1 parent fea5a2d commit a9ab4fc
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions tests/test_using.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,57 +1423,47 @@ class Params:

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

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

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

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

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

class TestObjectFactory(factory.Factory):
class Meta:
model = TestObject
one = 1
two = 2

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

obj = TestObjectFactory.build(with_related_nested_override=True)
self.assertEqual(1, obj.one)
self.assertEqual(2, obj.two)
self.assertEqual(1, obj.related.one)
self.assertEqual(2, obj.related.two)
self.assertEqual(1, obj.related.nested.one)
self.assertEqual(2, obj.related.nested.two)
self.assertEqual(1, obj.related.attr)
self.assertEqual(3, obj.related.nested.attr)

def test_prevent_cyclic_traits(self):

Expand Down

0 comments on commit a9ab4fc

Please sign in to comment.