def test_immutable_non_root(self): cfg = SimplerConfig() cfg.a = 1 cfg.level1.b = 2 cfg.level1.level2.c = 3 cfg.section1.d = 4 cfg.level1.set_immutable(recursive=True, 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.level1.loads("") self.assertTrue('loads' in str(context.exception)) try: cfg.loads("") except: self.fail("Other levels should not be immutable") try: tmp = cfg.b except: self.fail("Other levels should not be immutable") try: cfg.bb = 5 except: self.fail("Other levels should not be immutable") with self.assertRaises(ImmutableConfiguration) as context: tmp = cfg.level1.a with self.assertRaises(ImmutableConfiguration) as context: cfg.level1.a = 6 with self.assertRaises(ImmutableConfiguration) as context: tmp = cfg.level1.level2.b with self.assertRaises(ImmutableConfiguration) as context: cfg.level1.level2.b = 7 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")
def test_get_tuple(self): cfg = SimplerConfig() cfg.a = 1 cfg.level1.b = 2 cfg.level1.level2.c = 3 cfg.section1.d = 4 ret = cfg['level1', 'level2'].c self.assertEqual(ret, 3) ret = cfg['level1', 'level2', 'c'] self.assertEqual(ret, 3)
def test_sub_path(self): cfg = SimplerConfig() cfg.a = 1 cfg.level1.b = 2 cfg.level1.level2.c = 3 cfg.section1.d = 4 self.assertTupleEqual(tuple(), cfg.sub_path) self.assertTupleEqual(tuple(['level1']), cfg.level1.sub_path) self.assertTupleEqual(tuple(['level1', 'level2']), cfg.level1.level2.sub_path) self.assertTupleEqual(tuple(['section1']), cfg.section1.sub_path)
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