Exemplo n.º 1
0
    def test_immutable_non_recursive(self):
        cfg = SimplerConfig()
        cfg.a = 1
        cfg.level1.b = 2
        cfg.level1.level2.c = 3
        cfg.section1.d = 4

        cfg.set_immutable(recursive=False, from_root=False)
        self.assertEqual(cfg.a, 1)
        self.assertEqual(cfg.level1.b, 2)
        self.assertEqual(cfg.level1.level2.c, 3)
        self.assertEqual(cfg.section1.d, 4)

        with self.assertRaises(ImmutableConfiguration) as context:
            cfg.loads("")

        self.assertTrue('loads' in str(context.exception))

        with self.assertRaises(ImmutableConfiguration):
            tmp = cfg.b

        with self.assertRaises(ImmutableConfiguration):
            cfg.b = 5

        try:
            tmp = cfg.level1.a
        except:
            self.fail("Other levels should not be immutable")

        try:
            cfg.level1.aa = 6
        except:
            self.fail("Other levels should not be immutable")

        try:
            tmp = cfg.level1.level2.b
        except:
            self.fail("Other levels should not be immutable")

        try:
            cfg.level1.level2.bb = 7
        except:
            self.fail("Other levels should not be immutable")

        try:
            tmp = cfg.section1.c
        except:
            self.fail("Other levels should not be immutable")

        try:
            cfg.section1.cc = 7
        except:
            self.fail("Other levels should not be immutable")
Exemplo n.º 2
0
    def test_immutable(self):
        cfg = SimplerConfig()
        cfg.a = 1
        cfg.level1.b = 2
        cfg.level1.level2.c = 3
        cfg.section1.d = 4

        cfg.set_immutable()
        self.assertEqual(cfg.a, 1)
        self.assertEqual(cfg.level1.b, 2)
        self.assertEqual(cfg.level1.level2.c, 3)
        self.assertEqual(cfg.section1.d, 4)

        with self.assertRaises(ImmutableConfiguration) as context:
            cfg.loads("")

        self.assertTrue('loads' in str(context.exception))

        with self.assertRaises(ImmutableConfiguration):
            tmp = cfg.b

        with self.assertRaises(ImmutableConfiguration):
            cfg.b = 5

        with self.assertRaises(ImmutableConfiguration):
            tmp = cfg.level1.a

        with self.assertRaises(ImmutableConfiguration):
            cfg.level1.a = 6

        with self.assertRaises(ImmutableConfiguration):
            tmp = cfg.level1.level2.b

        with self.assertRaises(ImmutableConfiguration):
            cfg.level1.level2.b = 7

        with self.assertRaises(ImmutableConfiguration):
            tmp = cfg.section1.c

        with self.assertRaises(ImmutableConfiguration):
            cfg.section1.c = 7