class BasicUsageTest(TestCase): def setUp(self): super(BasicUsageTest, self).setUp() self.conf = Config(dict( a=dict( b=2 ) )) def test__getting(self): self.assertEquals(self.conf.root.a.b, 2) def test__setting(self): self.conf.root.a.b = 3 self.assertEquals(self.conf.root.a.b, 3) def test__get_conf_from_proxy(self): self.assertIs(get_config_object_from_proxy(self.conf.root), self.conf) def test__proxy_dir(self): self.assertEquals(dir(self.conf.root), ['a']) self.assertEquals(dir(self.conf.root.a), ['b']) def test__pop(self): self.assertEquals(list(self.conf['a'].keys()), ['b']) self.conf['a'].pop('b') self.assertEquals(list(self.conf['a'].keys()), []) def test__setting_existing_paths_through_setitem(self): self.conf["a"]["b"] = 3 self.assertEquals(self.conf.root.a.b, 3) def test__setting_existing_paths_through_assign_path(self): self.conf.assign_path('a.b', 3) self.assertEquals(self.conf.root.a.b, 3) def test__setting_nonexistent_paths(self): with self.assertRaises(exceptions.CannotSetValue): self.conf['a']['c'] = 4 with self.assertRaises(AttributeError): self.conf.root.a.c = 4 def test__getting_through_getitem(self): self.assertIsInstance(self.conf['a'], Config) def test__contains(self): self.assertTrue("a" in self.conf) self.assertFalse("b" in self.conf) self.assertFalse("c" in self.conf["a"]) self.assertTrue("b" in self.conf["a"]) def test__item_not_found(self): with self.assertRaises(LookupError): self.conf.root.a.c def test__keys(self): self.assertEquals(set(self.conf.keys()), set(['a']))
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)
class PathAssignmentTest(TestCase): def setUp(self): super(PathAssignmentTest, self).setUp() self.conf = Config(dict(a=dict(b=dict(c=3)), d=4, e=None)) def tearDown(self): super(PathAssignmentTest, self).tearDown() def test_invalid_path_assignment_to_path(self): with self.assertRaises(exceptions.InvalidPath): self.conf.assign_path("a.g.d", 3) def test_invalid_path_getting(self): with self.assertRaises(exceptions.InvalidPath): self.conf.get_path("a.g.d") def test_get_path_direct(self): self.assertEquals(4, self.conf.get_path("d")) def test_path_deducing_with_none(self): self.conf.root.a.b.c = None self.assertIsNone(self.conf.root.a.b.c) with self.assertRaises(exceptions.CannotDeduceType): self.conf.assign_path_expression('a.b.c=2', deduce_type=True) self.assertIsNone(self.conf.root.a.b.c) def test_path_assign_value_deduce_type(self): self.conf.root.a.b.c = 1 self.conf.assign_path('a.b.c', '2', deduce_type=True) self.assertEquals(self.conf.root.a.b.c, 2) def test_path_deducing_with_none_force_type(self): self.conf.assign_path_expression( "a.b.c=2", deduce_type=True, default_type=str) self.assertEquals(self.conf.root.a.b.c, 2) def test_path_deducing_with_compound_types(self): for initial_value, value in [ ([1, 2, 3], ["a", "b", 3]), ((1, 2), (3, 4, 5)) ]: self.conf["a"]["b"] = initial_value self.conf.assign_path_expression( "a.b={0!r}".format(value), deduce_type=True) self.assertEquals(self.conf["a"]["b"], value) def test_path_deducing_with_booleans(self): for false_literal in ('false', 'False', 'no', 'n', 'No', 'N'): self.conf['a']['b']['c'] = True self.conf.assign_path_expression( 'a.b.c={0}'.format(false_literal), deduce_type=True) self.assertFalse(self.conf.root.a.b.c) for true_literal in ('true', 'True', 'yes', 'y', 'Yes', 'Y'): self.conf['a']['b']['c'] = False self.conf.assign_path_expression( 'a.b.c={0}'.format(true_literal), deduce_type=True) self.assertTrue(self.conf.root.a.b.c) for invalid_literal in ('trueee', 0, 23, 2.3): with self.assertRaises(ValueError): self.conf.assign_path_expression( 'a.b.c={0}'.format(invalid_literal), deduce_type=True) def test_assign_path_direct(self): self.conf.assign_path('d', 5) self.assertEquals(self.conf['d'], 5)