def test_exceptions(self): s = deepcopy(self.simple) with self.assertRaises(KeyError): Path("e.1.a").get_in(s) with self.assertRaises(IndexError): Path("e.5.a").get_in(s) with self.assertRaises(AttributeError): Path("e.2.x").get_in(s)
def test_iteritems_all(self): paths = [path for path in Path.items(self.simple, all=True)] self.assertEqual(len(paths), 50) new = {} for path, value in Path.items(self.simple, all=True): path._set_in(new, value) for path in Path.paths(new): self.assertEqual(path.get_in(self.simple), path.get_in(new))
def test_basic_del(self): p1 = Path("a") p2 = Path("1") d = dict(a=1) l = [0, 1] o = Object(a=1) p1._del_in(d) p2._del_in(l) p1._del_in(o) self.assertEqual(d, {}) self.assertEqual(l, [0]) self.assertEqual(len(o.__dict__), 0)
def test_sequence_class(self): class ListObject(list): def __init__(self, items, value): super(ListObject, self).__init__(items) self.value = value obj = ListObject([1, 2, 3], value=["a", "b", "c"]) self.assertEqual(Path("1").get_in(obj), 2) self.assertEqual(Path("value.1").get_in(obj), "b") Path("1").set_in(obj, 4) Path("value.1").set_in(obj, "d") self.assertEqual(Path("1").get_in(obj), 4) self.assertEqual(Path("value.1").get_in(obj), "d") Path("1").del_in(obj) Path("value.1").del_in(obj) self.assertEqual(Path("1").get_in(obj), 3) self.assertEqual(Path("value.1").get_in(obj), "c")
def test_default(self): obj = [ dict(a=[0, 1, 2], b="b1"), dict(a=[3, 4, 5], b="b2", c="c"), ] self.assertEqual(Path("0.c").get_in(obj, "default"), "default") self.assertEqual( WildPath("*.c").get_in(obj, "default"), ["default", "c"])
def test_path_tuple_example(self): assert Path("a.b") + Path("c") == Path("a.b.c") assert Path("a.b.c")[1:] == Path("b.c") assert repr(Path("a.b.c")) == "('a', 'b', 'c')" # however (this is not the tuple implementation of __str__): assert str(Path("a.b.c")) == "a.b.c"
def test_iterator_examples(self): agenda = deepcopy(self.agenda) try: from wildpath.paths import Path for path, value in sorted(Path.items(agenda)): print(" ".join([str(path), ":", value])) for path, value in sorted(Path.items(agenda, all=True)): print(" ".join([str(path), ":", str(value)])) new_dict = {} for path, value in Path.items(agenda, all=True): path._set_in(new_dict, value) assert new_dict == agenda except Exception as e: self.fail(e)
def test_iteritems_copy(self): simple = deepcopy(self.simple) new = {} for path, value in Path.items(simple, all=True): if isinstance(value, int): value = str(value) path._set_in(new, value) self.assertEqual(simple, self.simple)
def test_descriptor(self): class TestDescriptor(object): def __get__(self, obj, cls): if obj is None: return self return obj.attr def __set__(self, obj, value): obj.attr = value def __delete__(self, obj): del obj.attr class Test(object): desc = TestDescriptor() def __init__(self, desc): self.desc = desc test = Test("attribute") self.assertEqual(Path("desc").get_in(test), 'attribute') self.assertEqual( WildPath("*").get_in(test), { 'attr': 'attribute', 'desc': 'attribute' }) Path("desc").set_in(test, "betribute") self.assertEqual(Path("desc").get_in(test), 'betribute') WildPath("*").set_in(test, "cetribute") # sets both self.assertEqual(Path("desc").get_in(test), 'cetribute') Path("desc").del_in(test) self.assertEqual(Path("desc").has_in(test), False)
def test_property(self): class Some(object): def __init__(self): self._prop = "prop" @property def prop(self): return self._prop @prop.setter def prop(self, string): self._prop = string @prop.deleter def prop(self): del self._prop some = Some() self.assertEqual(Path("prop").get_in(some), "prop") self.assertEqual(WildPath("prop").get_in(some), "prop") self.assertEqual(WildPath("!_*").get_in(some), {'prop': 'prop'}) self.assertEqual(dict(Path.items(some)), { ('prop', ): 'prop', ('_prop', ): 'prop' }) Path("prop").set_in(some, "drop") self.assertEqual(Path("prop").get_in(some), "drop") Path("prop").del_in(some) self.assertFalse(Path("prop").has_in(some))
def test_class_items(self): class Test(object): dont_find_1 = 1 @classmethod def dont_find_2(cls): pass @staticmethod def dont_find_3(cls): pass self.assertEqual(list(Path.items(Test())), [])
def test_path_example(self): agenda = deepcopy(self.agenda) try: from wildpath.paths import Path path = Path("items.0.duration") assert str( path ) == "items.0.duration" # str(..) returns the original path string duration = path.get_in(agenda) # retrieves value at path location assert duration == "5 minutes" path._set_in(agenda, "10 minutes") # sets value at path location assert path.get_in(agenda) == "10 minutes" path._del_in(agenda) # deletes key-value at path loation assert path.has_in( agenda ) == False # has_in checks the presenca of a value at the path location except Exception as e: self.fail(e)
def test_some_basics(self): path = Path('')
def test_basic_set(self): p1 = Path("a") p2 = Path("1") d = dict(a=1) l = [0, 1] o = Object(a=1) p1._set_in(d, 0) p2._set_in(l, 0) p1._set_in(o, 0) self.assertEqual(p1.get_in(d), 0) self.assertEqual(p2.get_in(l), 0) self.assertEqual(p1.get_in(o), 0)
def test_longer_del(self): s = deepcopy(self.simple) p = Path("b.0") p._del_in(s) self.assertEqual(s.b, [3]) p = Path("c.d") p._del_in(s) self.assertEqual(s.c, {"e": 5}) p = Path("d.e") p._del_in(s) self.assertEqual(len(s.d.__dict__), 0)
def test_basic_get(self): p1 = Path("a") p2 = Path("1") self.assertEqual(p1.get_in(dict(a=1)), 1) self.assertEqual(p2.get_in([0, 1]), 1) self.assertEqual(p1.get_in(Object(a=1)), 1)
def test_longer_set(self): s = deepcopy(self.simple) p = Path("b.0") p._set_in(s, 11) self.assertEqual(p.get_in(s), 11) p = Path("c.d") p._set_in(s, 11) self.assertEqual(p.get_in(s), 11) p = Path("d.e") p._set_in(s, 11) self.assertEqual(p.get_in(s), 11)
def test_slice(self): path = Path("1.a.2.b.3.c") self.assertEqual(type(path[-1:0:-2]), Path) self.assertEqual(path[::2], Path("1.2.3")) self.assertEqual(path[1:-1], Path("a.2.b.3")) self.assertEqual(path[-1:0:-2], Path("c.b.a"))
def test_iteritems(self): items = [path for path in Path.items(self.simple, all=False)] self.assertTrue(all(isinstance(item, tuple) for item in items)) self.assertEqual(len(items), 28)
def test_unicode(self): path = Path(u"a.0") self.assertEqual(path.get_in({"a": [1]}), 1)
def test_longer_get(self): s = deepcopy(self.simple) p = Path("b.0") self.assertEqual(p.get_in(s), 2) p = Path("c.d") self.assertEqual(p.get_in(s), 4) p = Path("d.e") self.assertEqual(p.get_in(s), 6) p = Path("e.1.b") self.assertEqual(p.get_in(s), 9)