def test_inherited_declaration_collecting(self): lazy = declarations.LazyAttribute(lambda _o: 1) lazy2 = declarations.LazyAttribute(lambda _o: 2) postgen = declarations.PostGenerationDeclaration() postgen2 = declarations.PostGenerationDeclaration() class AbstractFactory(base.Factory): x = 1 y = lazy z = postgen class OtherFactory(AbstractFactory): a = lazy2 b = postgen2 # Declarations aren't removed self.assertEqual(lazy2, OtherFactory.a) self.assertEqual(postgen2, OtherFactory.b) self.assertEqual(1, OtherFactory.x) self.assertEqual(lazy, OtherFactory.y) self.assertEqual(postgen, OtherFactory.z) # And are available in class Meta self.assertEqual({'x': 1, 'y': lazy, 'a': lazy2}, OtherFactory._meta.declarations) self.assertEqual({'z': postgen, 'b': postgen2}, OtherFactory._meta.postgen_declarations)
def test_declaration_collecting(self): lazy = declarations.LazyFunction(int) lazy2 = declarations.LazyAttribute(lambda _o: 1) postgen = declarations.PostGenerationDeclaration() class AbstractFactory(base.Factory): x = 1 y = lazy y2 = lazy2 z = postgen # Declarations aren't removed self.assertEqual(1, AbstractFactory.x) self.assertEqual(lazy, AbstractFactory.y) self.assertEqual(lazy2, AbstractFactory.y2) self.assertEqual(postgen, AbstractFactory.z) # And are available in class Meta self.assertEqual( { 'x': 1, 'y': lazy, 'y2': lazy2 }, AbstractFactory._meta.pre_declarations.as_dict(), ) self.assertEqual( {'z': postgen}, AbstractFactory._meta.post_declarations.as_dict(), )
def test_lazy_attribute(self): la = declarations.LazyAttribute(lambda a: a.one * 2) class FakeFactory(object): @classmethod def declarations(cls, extra): d = {'one': 1, 'two': la} d.update(extra) return d @classmethod def _generate_next_sequence(cls): return 1 ab = containers.AttributeBuilder(FakeFactory) self.assertEqual({'one': 1, 'two': 2}, ab.build(create=False)) ab = containers.AttributeBuilder(FakeFactory, {'one': 4}) self.assertEqual({'one': 4, 'two': 8}, ab.build(create=False)) ab = containers.AttributeBuilder(FakeFactory, {'one': 4, 'three': la}) self.assertEqual({ 'one': 4, 'two': 8, 'three': 8 }, ab.build(create=False))
def test_inherited_declaration_shadowing(self): lazy = declarations.LazyFunction(int) lazy2 = declarations.LazyAttribute(lambda _o: 2) postgen = declarations.PostGenerationDeclaration() postgen2 = declarations.PostGenerationDeclaration() class AbstractFactory(base.Factory): x = 1 y = lazy z = postgen class OtherFactory(AbstractFactory): y = lazy2 z = postgen2 # Declarations aren't removed self.assertEqual(1, OtherFactory.x) self.assertEqual(lazy2, OtherFactory.y) self.assertEqual(postgen2, OtherFactory.z) # And are available in class Meta self.assertEqual( { 'x': 1, 'y': lazy2 }, OtherFactory._meta.pre_declarations.as_dict(), ) self.assertEqual( {'z': postgen2}, OtherFactory._meta.post_declarations.as_dict(), )
def test_lazy_attribute(self): la = declarations.LazyAttribute(lambda a: a.one * 2) FakeFactory = self.make_fake_factory({'one': 1, 'two': la}) ab = containers.AttributeBuilder(FakeFactory) self.assertEqual({'one': 1, 'two': 2}, ab.build(create=False)) ab = containers.AttributeBuilder(FakeFactory, {'one': 4}) self.assertEqual({'one': 4, 'two': 8}, ab.build(create=False)) ab = containers.AttributeBuilder(FakeFactory, {'one': 4, 'three': la}) self.assertEqual({'one': 4, 'two': 8, 'three': 8}, ab.build(create=False))
def test_make_factory(self): fact = base.make_factory(TestObject, two=2, three=declarations.LazyAttribute(lambda o: o.two + 1)) obj = fact.build() self.assertEqual(obj.one, None) self.assertEqual(obj.two, 2) self.assertEqual(obj.three, 3) self.assertEqual(obj.four, None) obj = fact.build(two=4) self.assertEqual(obj.one, None) self.assertEqual(obj.two, 4) self.assertEqual(obj.three, 5) self.assertEqual(obj.four, None)
class TestObjectFactory(base.Factory): FACTORY_FOR = TestObject one = declarations.LazyAttribute(lambda a: a.does_not_exist)
class TestObjectFactory(Factory): one = declarations.LazyAttribute(lambda a: 'abc' ) two = declarations.LazyAttribute(lambda a: a.one + ' xyz')
class WrappingTestObjectFactory(Factory): FACTORY_FOR = TestObject wrapped = declarations.SubFactory(TestObjectFactory) friend = declarations.LazyAttribute(lambda o: o.wrapped.two.four + 1)
class TestModel2Factory(Factory): FACTORY_FOR = TestModel2 two = declarations.SubFactory(TestModelFactory, one=declarations.Sequence(lambda n: 'x%sx' % n), two=declarations.LazyAttribute( lambda o: '%s%s' % (o.one, o.one)))
def test_complex(self): obj = base.build(TestObject, two=2, three=declarations.LazyAttribute(lambda o: o.two + 1)) self.assertEqual(obj.one, None) self.assertEqual(obj.two, 2) self.assertEqual(obj.three, 3) self.assertEqual(obj.four, None)
class TestObjectFactory(Factory): one = 'one' two = declarations.LazyAttribute(lambda a: a.one + ' two')
class TestObjectFactory2(TestObjectFactory): FACTORY_FOR = TestObject three = 'three' four = declarations.LazyAttribute(lambda a: a.three + ' four')
class TestObjectFactory(Factory): one = declarations.LazyAttribute(lambda a: a.does_not_exist )
class TestFactory(TestObjectFactory): three = 'three' four = declarations.LazyAttribute(lambda a: a.three + ' four')
class TestObjectFactory(base.Factory): class Meta: model = TestObject one = declarations.LazyAttribute(lambda a: a.does_not_exist )
def test_init(self): declarations.Maybe('foo', 1, 2) with self.assertRaisesRegex(TypeError, 'Inconsistent phases'): declarations.Maybe('foo', declarations.LazyAttribute(None), declarations.PostGenerationDeclaration())