def test_sort_and_reverse(): dl = DictList(Object("test%d" % (i)) for i in reversed(range(10))) assert dl[0].id == "test9" dl.sort() assert len(dl) == 10 assert dl[0].id == "test0" assert dl.index("test0") == 0 dl.reverse() assert dl[0].id == "test9" assert dl.index("test0") == 9
def test_set(): obj_list = DictList(Object("test%d" % (i)) for i in range(10)) obj_list[4] = Object("testa") assert obj_list.index("testa") == 4 assert obj_list[4].id == "testa" obj_list[5:7] = [Object("testb"), Object("testc")] assert obj_list.index("testb") == 5 assert obj_list[5].id == "testb" assert obj_list.index("testc") == 6 assert obj_list[6].id == "testc" # Even if the object is unique, if it is present twice in the new # list, it should still raise an exception with pytest.raises(ValueError): obj_list.__setitem__(slice(5, 7), [Object("testd"), Object("testd")])
def test_removal(): obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10)) del obj_list[3] assert "test5" not in obj_list assert obj_list.index(obj_list[-1]) == len(obj_list) - 1 assert len(obj_list) == 7 del obj_list[3:5] assert "test6" not in obj_list assert "test7" not in obj_list assert obj_list.index(obj_list[-1]) == len(obj_list) - 1 assert len(obj_list) == 5 removed = obj_list.pop(1) assert obj_list.index(obj_list[-1]) == len(obj_list) - 1 assert removed.id == "test3" assert "test3" not in obj_list assert len(obj_list) == 4 removed = obj_list.pop() assert removed.id == "test9" assert removed.id not in obj_list assert len(obj_list) == 3
def test_init_copy(dict_list): obj, test_list = dict_list test_list.append(Object("test2")) copied = DictList(test_list) assert test_list is not copied assert isinstance(copied, test_list.__class__) assert len(test_list) == len(copied) for i, v in enumerate(test_list): assert test_list[i].id == copied[i].id assert i == copied.index(v.id) assert test_list[i] is copied[i] assert v is copied.get_by_id(v.id)