Exemple #1
0
 def test_query(self, dict_list):
     obj, test_list = dict_list
     obj2 = Object("test2")
     obj2.name = "foobar1"
     test_list.append(obj2)
     result = test_list.query("test1")  # matches only test1
     assert len(result) == 1
     result = test_list.query(u"test1", "id")  # matches with unicode
     assert len(result) == 1
     assert result[0] == obj
     result = test_list.query("foo", "name")  # matches only test2
     assert len(result) == 1
     assert result[0] == obj2
     result = test_list.query("test", "id")  # matches test1 and test2
     assert len(result) == 2
     # test with a regular expression
     result = test_list.query(re.compile("test[0-9]"), "id")
     assert len(result) == 2
     result = test_list.query(re.compile("test[29]"), "id")
     assert len(result) == 1
     # test query of name
     result = test_list.query(re.compile("foobar."), "name")
     assert len(result) == 1
     # test query with lambda function
     result = test_list.query(lambda x: x.id == 'test1')
     assert len(result) == 1
 def testQuery(self):
     obj2 = Object("test2")
     obj2.name = "foobar1"
     self.list.append(obj2)
     result = self.list.query("test1")  # matches only test1
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0], self.obj)
     result = self.list.query("foo", "name")  # matches only test2
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0], obj2)
     result = self.list.query("test")  # matches test1 and test2
     self.assertEqual(len(result), 2)
     # test with a regular expression
     result = self.list.query(re.compile("test[0-9]"))
     self.assertEqual(len(result), 2)
     result = self.list.query(re.compile("test[29]"))
     self.assertEqual(len(result), 1)
     # test query of name
     result = self.list.query(re.compile("foobar."), "name")
     self.assertEqual(len(result), 1)
 def testDeepcopy(self):
     self.list.append(Object("test2"))
     copied = deepcopy(self.list)
     self.assertIsNot(self.list, copied)
     self.assertIsInstance(copied, self.list.__class__)
     self.assertEqual(len(self.list), len(copied))
     for i, v in enumerate(self.list):
         self.assertEqual(self.list[i].id, copied[i].id)
         self.assertEqual(i, copied.index(v.id))
         self.assertIsNot(self.list[i], copied[i])
         self.assertIsNot(v, copied.get_by_id(v.id))
Exemple #4
0
 def testAdd(self):
     obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
     sum = self.list + obj_list
     self.assertIsNot(sum, self.list)
     self.assertIsNot(sum, obj_list)
     self.assertEqual(self.list[0].id, "test1")
     self.assertEqual(sum[1].id, "test2")
     self.assertEqual(sum.get_by_id("test2"), obj_list[0])
     self.assertEqual(sum[8].id, "test9")
     self.assertEqual(len(self.list), 1)
     self.assertEqual(len(sum), 9)
Exemple #5
0
 def test_deepcopy(self, dict_list):
     obj, test_list = dict_list
     test_list.append(Object("test2"))
     copied = deepcopy(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 not copied[i]
         assert v is not copied.get_by_id(v.id)
Exemple #6
0
 def test_add(self, dict_list):
     obj, test_list = dict_list
     obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
     sum = test_list + obj_list
     assert sum is not test_list
     assert sum is not obj_list
     assert test_list[0].id == "test1"
     assert sum[1].id == "test2"
     assert sum.get_by_id("test2") == obj_list[0]
     assert sum[8].id == "test9"
     assert len(test_list) == 1
     assert len(sum) == 9
Exemple #7
0
 def testPickle(self):
     self.list.append(Object("test2"))
     for protocol in range(HIGHEST_PROTOCOL):
         pickle_str = dumps(self.list, protocol=protocol)
         copied = loads(pickle_str)
         self.assertIsNot(self.list, copied)
         self.assertIsInstance(copied, self.list.__class__)
         self.assertEqual(len(self.list), len(copied))
         for i, v in enumerate(self.list):
             self.assertEqual(self.list[i].id, copied[i].id)
             self.assertEqual(i, copied.index(v.id))
             self.assertIsNot(self.list[i], copied[i])
             self.assertIsNot(v, copied.get_by_id(v.id))
Exemple #8
0
 def testRemoval(self):
     obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10))
     del obj_list[3]
     self.assertNotIn("test5", obj_list)
     self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
     del obj_list[3:5]
     self.assertNotIn("test6", obj_list)
     self.assertNotIn("test7", obj_list)
     self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
     removed = obj_list.pop(1)
     self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
     self.assertEqual(removed.id, "test3")
     self.assertNotIn("test3", obj_list)
Exemple #9
0
 def test_pickle(self, dict_list):
     obj, test_list = dict_list
     test_list.append(Object("test2"))
     for protocol in range(HIGHEST_PROTOCOL):
         pickle_str = dumps(test_list, protocol=protocol)
         copied = loads(pickle_str)
         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 not copied[i]
             assert v is not copied.get_by_id(v.id)
Exemple #10
0
 def test_query(self, dict_list):
     obj, test_list = dict_list
     obj2 = Object("test2")
     obj2.name = "foobar1"
     test_list.append(obj2)
     result = test_list.query("test1")  # matches only test1
     assert len(result) == 1
     result = test_list.query(u"test1")  # matches with unicode
     assert len(result) == 1
     assert result[0] == obj
     result = test_list.query("foo", "name")  # matches only test2
     assert len(result) == 1
     assert result[0] == obj2
     result = test_list.query("test")  # matches test1 and test2
     assert len(result) == 2
     # test with a regular expression
     result = test_list.query(re.compile("test[0-9]"))
     assert len(result) == 2
     result = test_list.query(re.compile("test[29]"))
     assert len(result) == 1
     # test query of name
     result = test_list.query(re.compile("foobar."), "name")
     assert len(result) == 1
Exemple #11
0
 def testSet(self):
     obj_list = DictList(Object("test%d" % (i)) for i in range(10))
     obj_list[4] = Object("testa")
     self.assertEqual(obj_list.index("testa"), 4)
     self.assertEqual(obj_list[4].id, "testa")
     obj_list[5:7] = [Object("testb"), Object("testc")]
     self.assertEqual(obj_list.index("testb"), 5)
     self.assertEqual(obj_list[5].id, "testb")
     self.assertEqual(obj_list.index("testc"), 6)
     self.assertEqual(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
     self.assertRaises(ValueError, obj_list.__setitem__, slice(5, 7),
                       [Object("testd"), Object("testd")])
Exemple #12
0
 def test_removal(self):
     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
Exemple #13
0
 def test_set(self):
     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")])
Exemple #14
0
 def testUnion(self):
     self.list.union([Object("test1"), Object("test2")])
     # should only add 1 element
     self.assertEqual(len(self.list), 2)
     self.assertEqual(self.list.index("test2"), 1)
Exemple #15
0
 def setUp(self):
     self.obj = Object("test1")
     self.list = DictList()
     self.list.append(self.obj)
Exemple #16
0
 def testIndex(self):
     self.assertEqual(self.list.index("test1"), 0)
     self.assertEqual(self.list.index(self.obj), 0)
     self.assertRaises(ValueError, self.list.index, "f")
     self.assertRaises(ValueError, self.list.index, Object("f"))
Exemple #17
0
 def test_contains(self, dict_list):
     obj, test_list = dict_list
     assert obj in test_list
     assert obj.id in test_list
     assert Object("not_in") not in test_list
     assert 'not_in' not in test_list
Exemple #18
0
 def test_union(self, dict_list):
     obj, test_list = dict_list
     test_list.union([Object("test1"), Object("test2")])
     # should only add 1 element
     assert len(test_list) == 2
     assert test_list.index("test2") == 1
Exemple #19
0
 def testContains(self):
     self.assertIn(self.obj, self.list)
     self.assertIn(self.obj.id, self.list)
     self.assertNotIn(Object("not_in"), self.list)
     self.assertNotIn('not_in', self.list)
Exemple #20
0
def dict_list():
    obj = Object("test1")
    test_list = DictList()
    test_list.append(obj)
    return obj, test_list