示例#1
0
class SerializationTest(TestCase):

    def setUp(self):
        super(SerializationTest, self).setUp()
        self.dict = dict(
            a=dict(
                b=dict(
                    c=8
                )
            )
        )
        self.conf = Config(self.dict)

    def test__serialization(self):
        result = self.conf.serialize_to_dict()
        self.assertIsNot(result, self.dict)
        self.assertEquals(result, self.dict)
        self.assertIsNot(result['a'], self.dict['a'])
        self.assertEquals(result['a']['b']['c'], 8)

    def test__serialization_with_assignment(self):
        self.conf.assign_path("a.b.c", 9)
        result = self.conf.serialize_to_dict()
        self.assertEquals(result['a']['b']['c'], 9)
示例#2
0
class ExtendingTest(TestCase):

    def setUp(self):
        super(ExtendingTest, self).setUp()
        self.conf = Config({
            "a": 1
        })

    def test__extend_single_value(self):
        self.conf.extend({"b": 2})
        self.assertEquals(self.conf.root.b, 2)

    def test__extend_keyword_arguments(self):
        self.conf.extend(b=2)
        self.assertEquals(self.conf.root.b, 2)

    def test__extend_structure(self):
        self.conf.extend({
            "b": {
                "c": {
                    "d": 2
                }
            }
        })
        self.assertEquals(self.conf.root.b.c.d, 2)

    def test__extend_structure_and_keywords(self):
        self.conf.extend({"b":1, "c":3}, b=2)
        self.assertEquals(self.conf.root.b, 2)
        self.assertEquals(self.conf.root.c, 3)

    def test__extend_preserves_nodes(self):
        self.conf.extend({"b": {"c": 2}})
        self.conf.extend({"b": {"d": 3}})
        self.assertEquals(
            self.conf.serialize_to_dict(),
            {"a": 1, "b": {"c": 2, "d": 3}}
        )

    def test__extend_config(self):
        self.conf.extend(Config({
            "b": {
                "c": {
                    "d": 2
                }
            }
        }))
        self.assertEquals(self.conf.root.b.c.d, 2)

    def test__extend_config_propagates_changes(self):
        new_cfg = Config({'b': {'c': 2}})
        self.conf.extend(new_cfg)
        self.assertEquals(self.conf.root.b.c, 2)
        new_cfg.root.b.c = 3
        self.assertEquals(self.conf.root.b.c, 3)

    def test__extend_config_preserves_metadata(self):
        new_cfg = Config({'b': {'c': 2 // Metadata(x=3)}})
        self.conf.extend(new_cfg)
        self.assertEquals(self.conf.get_config('b.c').metadata, {'x': 3})

    def test__extend_config_preserves_nodes(self):
        self.conf.extend(Config({"b": {"c": 2}}))
        self.conf.extend(Config({"b": {"c": 2, "d": 3}}))
        self.assertEquals(
            self.conf.serialize_to_dict(),
            {"a": 1, "b": {"c": 2, "d": 3}}
        )

    def test__extend_config_prevents_losing_path(self):
        self.conf.extend(Config({"b": {"c": 2}}))
        with self.assertRaises(exceptions.CannotSetValue):
            self.conf.extend(Config({"b": {"d": 3}}))

    def test__update_config_preserves_nodes(self):
        self.conf.update(Config({"b": {"c": 2}}))
        self.conf.update(Config({"b": {"d": 3}}))
        self.assertEquals(
            self.conf.serialize_to_dict(),
            {"a": 1, "b": {"c": 2, "d": 3}}
        )