コード例 #1
0
    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))
コード例 #2
0
    def test_lazy_function(self):
        lf = declarations.LazyFunction(int)
        FakeFactory = self.make_fake_factory({'one': 1, 'two': lf})

        ab = containers.AttributeBuilder(FakeFactory)
        self.assertEqual({'one': 1, 'two': 0}, ab.build(create=False))

        ab = containers.AttributeBuilder(FakeFactory, {'one': 4})
        self.assertEqual({'one': 4, 'two': 0}, ab.build(create=False))

        ab = containers.AttributeBuilder(FakeFactory, {'one': 4, 'three': lf})
        self.assertEqual({'one': 4, 'two': 0, 'three': 0}, ab.build(create=False))
コード例 #3
0
    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))
コード例 #4
0
    def test_replaced_sequence(self):
        seq = declarations.Sequence(lambda n: 'xx%d' % n)
        seq2 = declarations.Sequence(lambda n: 'yy%d' % n)
        FakeFactory = self.make_fake_factory({'one': seq})

        ab = containers.AttributeBuilder(FakeFactory, extra={'one': seq2})
        self.assertEqual({'one': 'yy1'}, ab.build(create=False))
コード例 #5
0
    def test_empty(self):
        """Tests building attributes from an empty definition."""

        FakeFactory = self.make_fake_factory({})
        ab = containers.AttributeBuilder(FakeFactory)

        self.assertEqual({}, ab.build(create=False))
コード例 #6
0
    def _prepare_data(cls, **data):
        """Put on SubFactories fields only the id value"""

        attribute_builder = containers.AttributeBuilder(cls, data)
        model_declaration = cls.declarations({})
        for field in attribute_builder._subfields:
            factory = model_declaration[field].factory_wrapper.factory
            data[field] = factory._get_id_value(data[field])
        return data
コード例 #7
0
    def test_subfields(self):
        class FakeInnerFactory(object):
            pass

        sf = declarations.SubFactory(FakeInnerFactory)
        FakeFactory = self.make_fake_factory({'one': sf, 'two': 2})

        ab = containers.AttributeBuilder(FakeFactory, {'one__blah': 1, 'two__bar': 2})
        self.assertTrue(ab.has_subfields(sf))
        self.assertEqual(['one'], list(ab._subfields.keys()))
        self.assertEqual(2, ab._declarations['two__bar'])
コード例 #8
0
ファイル: base.py プロジェクト: bensternthal/firefox-flicks
    def attributes(cls, create=False, extra=None):
        """Build a dict of attribute values, respecting declaration order.

        The process is:
        - Handle 'orderless' attributes, overriding defaults with provided
            kwargs when applicable
        - Handle ordered attributes, overriding them with provided kwargs when
            applicable; the current list of computed attributes is available
            to the currently processed object.
        """
        return containers.AttributeBuilder(cls, extra).build(create)
コード例 #9
0
    def test_overridden(self):
        class FakeFactory(object):
            @classmethod
            def declarations(cls, extra):
                d = {'one': 1}
                d.update(extra)
                return d

            @classmethod
            def _generate_next_sequence(cls):
                return 1

        ab = containers.AttributeBuilder(FakeFactory, {'one': 2})
        self.assertEqual({'one': 2}, ab.build(create=False))
コード例 #10
0
    def test_empty(self):
        """Tests building attributes from an empty definition."""
        class FakeFactory(object):
            @classmethod
            def declarations(cls, extra):
                return extra

            @classmethod
            def _generate_next_sequence(cls):
                return 1

        ab = containers.AttributeBuilder(FakeFactory)

        self.assertEqual({}, ab.build(create=False))
コード例 #11
0
    def test_additionnal_sequence(self):
        seq = declarations.Sequence(lambda n: 'xx%d' % n)

        class FakeFactory(object):
            @classmethod
            def declarations(cls, extra):
                d = {'one': 1}
                d.update(extra)
                return d

            @classmethod
            def _generate_next_sequence(cls):
                return 1

        ab = containers.AttributeBuilder(FakeFactory, extra={'two': seq})
        self.assertEqual({'one': 1, 'two': 'xx1'}, ab.build(create=False))
コード例 #12
0
    def test_replaced_sequence(self):
        seq = declarations.Sequence(lambda n: 'xx' + n)
        seq2 = declarations.Sequence(lambda n: 'yy' + n)

        class FakeFactory(object):
            @classmethod
            def declarations(cls, extra):
                d = {'one': seq}
                d.update(extra)
                return d

            @classmethod
            def _generate_next_sequence(cls):
                return 1

        ab = containers.AttributeBuilder(FakeFactory, extra={'one': seq2})
        self.assertEqual({'one': 'yy1'}, ab.build(create=False))
コード例 #13
0
    def test_subfields(self):
        class FakeInnerFactory(object):
            pass

        sf = declarations.SubFactory(FakeInnerFactory)

        class FakeFactory(object):
            @classmethod
            def declarations(cls, extra):
                d = {'one': sf, 'two': 2}
                d.update(extra)
                return d

        ab = containers.AttributeBuilder(FakeFactory, {'one__blah': 1, 'two__bar': 2})
        self.assertTrue(ab.has_subfields(sf))
        self.assertEqual(['one'], ab._subfields.keys())
        self.assertEqual(2, ab._attrs['two__bar'])
コード例 #14
0
 def test_overridden(self):
     FakeFactory = self.make_fake_factory({'one': 1})
     ab = containers.AttributeBuilder(FakeFactory, {'one': 2})
     self.assertEqual({'one': 2}, ab.build(create=False))
コード例 #15
0
    def test_additionnal_sequence(self):
        seq = declarations.Sequence(lambda n: 'xx%d' % n)
        FakeFactory = self.make_fake_factory({'one': 1})

        ab = containers.AttributeBuilder(FakeFactory, extra={'two': seq})
        self.assertEqual({'one': 1, 'two': 'xx1'}, ab.build(create=False))
コード例 #16
0
    def test_factory_defined_sequence(self):
        seq = declarations.Sequence(lambda n: 'xx%d' % n)
        FakeFactory = self.make_fake_factory({'one': seq})

        ab = containers.AttributeBuilder(FakeFactory)
        self.assertEqual({'one': 'xx1'}, ab.build(create=False))
コード例 #17
0
    def test_factory_defined(self):
        FakeFactory = self.make_fake_factory({'one': 1})
        ab = containers.AttributeBuilder(FakeFactory)

        self.assertEqual({'one': 1}, ab.build(create=False))