def test_ordered(self):
        """Tests the handling of OrderedDeclaration."""
        class ExampleFactory(object):
            @classmethod
            def _generate_next_sequence(cls):
                return 42

        two = declarations.LazyAttribute(lambda o: 2 * o.one)
        three = declarations.LazyAttribute(lambda o: o.one + o.two)
        holder = DeclarationsHolder({'one': 1, 'two': two, 'three': three})

        self.assertEqual([('one', 1), ('two', two), ('three', three)],
                         holder.items())

        self.assertEqual(two, holder['two'])

        attrs = holder.build_attributes(ExampleFactory)
        self.assertEqual(1, attrs['one'])
        self.assertEqual(2, attrs['two'])
        self.assertEqual(3, attrs['three'])

        attrs = holder.build_attributes(ExampleFactory, False, {'one': 4})
        self.assertEqual(4, attrs['one'])
        self.assertEqual(8, attrs['two'])
        self.assertEqual(12, attrs['three'])

        attrs = holder.build_attributes(ExampleFactory, False, {'one': 4, 'two': 2})
        self.assertEqual(4, attrs['one'])
        self.assertEqual(2, attrs['two'])
        self.assertEqual(6, attrs['three'])
    def test_skip_specials(self):
        """Makes sure that attributes starting with _ are skipped."""
        holder = DeclarationsHolder({'one': 1, '_two': 2})
        self.assertTrue('one' in holder)
        self.assertFalse('_two' in holder)

        remains = holder.update_base({'_two': 2, 'three': 3})
        self.assertTrue('three' in holder)
        self.assertFalse('_two' in holder)
        self.assertEqual({'_two': 2}, remains)
    def test_sub_factory(self):
        """Tests the behaviour of sub-factories."""
        class TestObject(object):
            def __init__(self, one=None, two=None, three=None, four=None, five=None):
                self.one = one
                self.two = two
                self.three = three
                self.four = four
                self.five = five

        class TestObjectFactory(base.Factory):
            FACTORY_FOR = TestObject
            two = 2
            five = declarations.Sequence(lambda n: n+1, type=int)

            @classmethod
            def _generate_next_sequence(cls):
                return 42

        class ExampleFactory(object):
            @classmethod
            def _generate_next_sequence(cls):
                return 42

        sub = declarations.SubFactory(TestObjectFactory,
                                      three=3,
                                      four=declarations.LazyAttribute(
                                          lambda o: 2 * o.two))

        holder = DeclarationsHolder(defaults={'sub': sub, 'one': 1})
        self.assertEqual(sub, holder['sub'])
        self.assertTrue('sub' in holder)

        attrs = holder.build_attributes(ExampleFactory)
        self.assertEqual(1, attrs['one'])
        self.assertEqual(2, attrs['sub'].two)
        self.assertEqual(3, attrs['sub'].three)
        self.assertEqual(4, attrs['sub'].four)

        attrs = holder.build_attributes(ExampleFactory, False, {'sub__two': 8, 'three__four': 4})
        self.assertEqual(1, attrs['one'])
        self.assertEqual(4, attrs['three__four'])
        self.assertEqual(8, attrs['sub'].two)
        self.assertEqual(3, attrs['sub'].three)
        self.assertEqual(16, attrs['sub'].four)
    def test_simple(self):
        """Tests a simple use case without OrderedDeclaration."""
        class ExampleFactory(object):
            @classmethod
            def _generate_next_sequence(cls):
                return 42

        holder = DeclarationsHolder({'one': 1, 'two': 2})
        self.assertTrue('one' in holder)
        self.assertTrue('two' in holder)
        holder.update_base({'two': 3, 'three': 3})
        self.assertEqual(1, holder['one'])
        self.assertEqual(3, holder['two'])
        self.assertEqual(3, holder['three'])

        attrs = holder.build_attributes(ExampleFactory)
        self.assertEqual(1, attrs['one'])
        self.assertEqual(3, attrs['two'])
        self.assertEqual(3, attrs['three'])
        self.assertEqual(42, ExampleFactory.sequence)

        self.assertEqual(set([('one', 1), ('two', 3), ('three', 3)]),
                         set(holder.items()))

        attrs = holder.build_attributes(ExampleFactory, False, {'two': 2})
        self.assertEqual(1, attrs['one'])
        self.assertEqual(2, attrs['two'])
        self.assertEqual(3, attrs['three'])
 def test_empty(self):
     holder = DeclarationsHolder()
     self.assertRaises(KeyError, holder.__getitem__, 'one')
     holder.update_base({'one': 1})
     self.assertEqual(1, holder['one'])