示例#1
0
    def test_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.__sub__(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset - set1
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.difference(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.difference(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)
示例#2
0
 def test_twoway_diff(self):
     for m in (self.a2, self.b2, self.c2):
         self.myset.add(m)
     for m in (self.a1, self.b2, self.c3):
         self.otherset.add(m)
     only_in_self, only_in_other, newer_in_self, newer_in_other = self.myset.diff(self.otherset)
     for coll in (only_in_self, only_in_other, newer_in_self, newer_in_other):
         self.assertIsInstance(coll, TwoWaySyncSet)
     self.assertEqual(only_in_self, TwoWaySyncSet())
     self.assertEqual(only_in_other, TwoWaySyncSet())
     self.assertEqual(newer_in_self, TwoWaySyncSet([self.a2]))
     self.assertEqual(newer_in_other, TwoWaySyncSet([self.c3]))
示例#3
0
 def setUp(self):
     self.myset = TwoWaySyncSet()
     self.otherset = TwoWaySyncSet()
     self.a1 = TestMember('a', 1)
     self.a2 = TestMember('a', 2)
     self.a3 = TestMember('a', 3)
     self.b1 = TestMember('b', 1)
     self.b2 = TestMember('b', 2)
     self.b3 = TestMember('b', 3)
     self.c1 = TestMember('c', 1)
     self.c2 = TestMember('c', 2)
     self.c3 = TestMember('c', 3)
示例#4
0
 def test_twoway_diff_2(self):
     m0 = TestMember(0, 1)
     m10 = TestMember(4, 1)
     for m in (self.a2, self.b1, m0):
         self.myset.add(m)
     for m in (self.a1, self.b2, m10):
         self.otherset.add(m)
     only_in_self, only_in_other, newer_in_self, newer_in_other = self.myset.diff(self.otherset)
     for coll in (only_in_self, only_in_other, newer_in_self, newer_in_other):
         self.assertIsInstance(coll, TwoWaySyncSet)
     self.assertEqual(only_in_self, TwoWaySyncSet([m0]))
     self.assertEqual(only_in_other, TwoWaySyncSet([m10]))
     self.assertEqual(newer_in_self, TwoWaySyncSet([self.a2]))
     self.assertEqual(newer_in_other, TwoWaySyncSet([self.b2]))
示例#5
0
 def test_constructor(self):
     self.assertEqual(self.myset, self.otherset)
     self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
     self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
     self.otherset = TwoWaySyncSet([self.a1, self.b1, self.c1])
     self.assertEqual(self.myset, self.otherset)
     self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
     self.otherset = TwoWaySyncSet([self.c1, self.a1, self.b1])
     self.assertEqual(self.myset, self.otherset)
     self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
     self.assertEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a1, self.a2]))
     self.assertTrue(TwoWaySyncSet([self.a2, self.a1]) == TwoWaySyncSet([self.a1, self.a2]))
     self.assertNotEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a3, self.a1]))
     self.assertNotEqual(TwoWaySyncSet([self.a1, self.b1]), TwoWaySyncSet([self.a1, self.b2]))
示例#6
0
    def test_union(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__or__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset | self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.union(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b2])
        set1 = TwoWaySyncSet([self.b1, self.c2])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.union(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b2, data)
        self.assertIn(self.c2, data)
示例#7
0
    def test_update(self):
        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.__ior__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset |= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)
        self.assertIn(self.c1, self.myset)
示例#8
0
    def test_intersection_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__iand__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset &= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.intersection_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.a1, self.b2])
        set2 = TwoWaySyncSet([self.a1, self.c1])
        self.myset.intersection_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)
示例#9
0
    def test_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__isub__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset -= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.difference_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)
示例#10
0
 def test_equal(self):
     self.assertEqual(
         TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()],
         TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()]
     )
     self.assertEqual(
         TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()],
         TwoWaySyncSet([self.b1, self.a1])[self.a1.get_id()]
     )
     self.assertEqual(TwoWaySyncSet([self.b1, self.a1]), TwoWaySyncSet([self.a1, self.b1]))
     self.assertNotEqual(OneWaySyncSet([self.a1]), OneWaySyncSet([self.a1, self.b1]))
     self.assertNotEqual(OneWaySyncSet([self.a1, self.b1]), OneWaySyncSet([self.a1, self.c1]))
示例#11
0
    def test_symmetric_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__ixor__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset ^= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.symmetric_difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)
示例#12
0
    def test_symmetric_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__xor__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset ^ self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.symmetric_difference(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)
示例#13
0
    def test_symmetric_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__ixor__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset ^= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.symmetric_difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)
示例#14
0
 def test_constructor(self):
     self.assertEqual(self.myset, self.otherset)
     self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
     self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
     self.otherset = TwoWaySyncSet([self.a1, self.b1, self.c1])
     self.assertEqual(self.myset, self.otherset)
     self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
     self.otherset = TwoWaySyncSet([self.c1, self.a1, self.b1])
     self.assertEqual(self.myset, self.otherset)
     self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
     self.assertEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a1, self.a2]))
     self.assertTrue(TwoWaySyncSet([self.a2, self.a1]) == TwoWaySyncSet([self.a1, self.a2]))
     self.assertNotEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a3, self.a1]))
     self.assertNotEqual(TwoWaySyncSet([self.a1, self.b1]), TwoWaySyncSet([self.a1, self.b2]))
示例#15
0
 def test_copy(self):
     self.myset = TwoWaySyncSet([self.a1])
     set_copy = self.myset.copy()
     self.assertIsInstance(set_copy, TwoWaySyncSet)
示例#16
0
 def test_get(self):
     self.assertEqual(TwoWaySyncSet([self.a1, self.b1]).get(self.a1.get_id()), self.a1)
示例#17
0
    def test_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.__sub__(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset - set1
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.difference(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.difference(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)
示例#18
0
    def test_union(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__or__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset | self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.union(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b2])
        set1 = TwoWaySyncSet([self.b1, self.c2])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.union(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b2, data)
        self.assertIn(self.c2, data)
示例#19
0
    def test_symmetric_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__xor__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset ^ self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.symmetric_difference(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)
示例#20
0
 def test_copy(self):
     self.myset = TwoWaySyncSet([self.a1])
     set_copy = self.myset.copy()
     self.assertIsInstance(set_copy, TwoWaySyncSet)
示例#21
0
class TwoWaySyncSetTest(_TwoWayBaseClass):
    def test_constructor(self):
        self.assertEqual(self.myset, self.otherset)
        self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
        self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        self.otherset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        self.assertEqual(self.myset, self.otherset)
        self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
        self.otherset = TwoWaySyncSet([self.c1, self.a1, self.b1])
        self.assertEqual(self.myset, self.otherset)
        self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
        self.assertEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a1, self.a2]))
        self.assertTrue(TwoWaySyncSet([self.a2, self.a1]) == TwoWaySyncSet([self.a1, self.a2]))
        self.assertNotEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a3, self.a1]))
        self.assertNotEqual(TwoWaySyncSet([self.a1, self.b1]), TwoWaySyncSet([self.a1, self.b2]))

    # Basic methods
    def test_copy(self):
        self.myset = TwoWaySyncSet([self.a1])
        set_copy = self.myset.copy()
        self.assertIsInstance(set_copy, TwoWaySyncSet)

    def test_add(self):
        for m in (self.a1, self.b1, self.c1):
            self.myset.add(m)
        self.assertEqual(len(self.myset), 3)
        self.assertEqual(self.myset[self.a1.get_id()], self.a1)
        self.assertNotEqual(self.myset[self.a1.get_id()], self.b1)
        self.assertNotEqual(self.myset[self.a1.get_id()], self.c1)
        self.assertEqual(self.myset[self.b1.get_id()], self.b1)
        self.assertEqual(self.myset[self.c1.get_id()], self.c1)

    def test_versions_add(self):
        # Also tests __getitem__
        for m in (self.a1, self.a2):
            self.myset.add(m)
        self.assertEqual(len(self.myset), 1)
        self.assertTrue(self.myset[self.a2.get_id()].__eq__(self.a1))
        self.assertEqual(self.myset[self.a2.get_id()].__cmp__(self.a2), 0)
        self.assertNotEqual(self.myset[self.a2.get_id()].__cmp__(self.a1), 0)

    def test_clear(self):
        for m in (self.a1, self.b3):
            self.myset.add(m)
        self.myset.clear()
        self.assertTrue(len(self.myset) == 0)

    def test_len(self):
        for m in (self.a1, self.a2):
            self.myset.add(m)
        self.assertTrue(len(self.myset) == 1)
        self.myset.add(self.b1)
        self.assertTrue(len(self.myset) == 2)

    # Basic operators
    def test_contains(self):
        self.myset.add(self.a1)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.a2, self.myset)
        self.assertNotIn(self.a3, self.myset)

    def test_contains_similar(self):
        self.myset.add(self.a1)
        self.assertTrue(self.myset.contains_similar(self.a1))
        self.assertTrue(self.myset.contains_similar(self.a2))
        self.assertFalse(self.myset.contains_similar(self.b1))

    def test_get(self):
        self.assertEqual(TwoWaySyncSet([self.a1, self.b1]).get(self.a1.get_id()), self.a1)

    def test_equal(self):
        self.assertEqual(
            TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()],
            TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()]
        )
        self.assertEqual(
            TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()],
            TwoWaySyncSet([self.b1, self.a1])[self.a1.get_id()]
        )
        self.assertEqual(TwoWaySyncSet([self.b1, self.a1]), TwoWaySyncSet([self.a1, self.b1]))
        self.assertNotEqual(OneWaySyncSet([self.a1]), OneWaySyncSet([self.a1, self.b1]))
        self.assertNotEqual(OneWaySyncSet([self.a1, self.b1]), OneWaySyncSet([self.a1, self.c1]))

    def test_remove(self):
        self.myset.add(self.a1)
        self.myset.remove(self.a1)
        self.assertTrue(len(self.myset) == 0)
        self.myset.add(self.a1)
        self.myset.remove(self.a2)
        self.assertTrue(len(self.myset) == 0)
        self.myset.add(self.a1)
        self.assertRaises(KeyError, self.myset.remove, self.b1)

    def test_discard(self):
        self.myset.add(self.a1)
        self.myset.discard(self.a1)
        self.assertTrue(len(self.myset) == 0)
        self.myset.add(self.a1)
        self.myset.discard(self.a2)
        self.assertTrue(len(self.myset) == 0)
        self.myset.discard(self.b1)
        self.assertTrue(len(self.myset) == 0)

    def test_pop(self):
        self.myset.add(self.a1)
        item = self.myset.pop()
        self.assertTrue(len(self.myset) == 0)
        self.assertEqual(id(item), id(self.a1))
        self.assertRaises(KeyError, self.myset.pop)

    # Boolean operators
    def test_isdisjoint(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.isdisjoint, self.otherset)

    def test_issubset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.issubset, self.otherset)
        self.assertRaises(UndefinedBehaviorError, self.myset.__le__, self.otherset)

    def test_istruesubset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.__lt__, self.otherset)

    def test_issuperset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.issuperset, self.otherset)
        self.assertRaises(UndefinedBehaviorError, self.myset.__ge__, self.otherset)

    def test_istruesuperset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.__gt__, self.otherset)

    # Operators
    def test_union(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__or__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset | self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.union(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b2])
        set1 = TwoWaySyncSet([self.b1, self.c2])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.union(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b2, data)
        self.assertIn(self.c2, data)

    def test_intersection(self):
        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave.__and__(set1)
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave & set1
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave.intersection(set1)
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a1, self.b2])
        set2 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave.intersection(set1, set2)
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.b2, data)
        self.assertNotIn(self.c1, data)

    def test_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.__sub__(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset - set1
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.difference(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.difference(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

    def test_symmetric_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__xor__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset ^ self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.symmetric_difference(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

    # In-place operators
    def test_update(self):
        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.__ior__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset |= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)
        self.assertIn(self.c1, self.myset)

    def test_intersection_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__iand__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset &= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.intersection_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.a1, self.b2])
        set2 = TwoWaySyncSet([self.a1, self.c1])
        self.myset.intersection_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)

    def test_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__isub__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset -= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.difference_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)

    def test_symmetric_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__ixor__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset ^= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.symmetric_difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

    # Diff tests
    def test_twoway_diff(self):
        for m in (self.a2, self.b2, self.c2):
            self.myset.add(m)
        for m in (self.a1, self.b2, self.c3):
            self.otherset.add(m)
        only_in_self, only_in_other, newer_in_self, newer_in_other = self.myset.diff(self.otherset)
        for coll in (only_in_self, only_in_other, newer_in_self, newer_in_other):
            self.assertIsInstance(coll, TwoWaySyncSet)
        self.assertEqual(only_in_self, TwoWaySyncSet())
        self.assertEqual(only_in_other, TwoWaySyncSet())
        self.assertEqual(newer_in_self, TwoWaySyncSet([self.a2]))
        self.assertEqual(newer_in_other, TwoWaySyncSet([self.c3]))

    def test_twoway_diff_2(self):
        m0 = TestMember(0, 1)
        m10 = TestMember(4, 1)
        for m in (self.a2, self.b1, m0):
            self.myset.add(m)
        for m in (self.a1, self.b2, m10):
            self.otherset.add(m)
        only_in_self, only_in_other, newer_in_self, newer_in_other = self.myset.diff(self.otherset)
        for coll in (only_in_self, only_in_other, newer_in_self, newer_in_other):
            self.assertIsInstance(coll, TwoWaySyncSet)
        self.assertEqual(only_in_self, TwoWaySyncSet([m0]))
        self.assertEqual(only_in_other, TwoWaySyncSet([m10]))
        self.assertEqual(newer_in_self, TwoWaySyncSet([self.a2]))
        self.assertEqual(newer_in_other, TwoWaySyncSet([self.b2]))

    # In-place updating
    def test_sync(self):
        only_in_self, only_in_master, outdated_in_self, updated_in_master = self.myset.diff(self.otherset)
        self.myset.sync(deleted=only_in_self, updated=updated_in_master, new=only_in_master)
        self.assertEqual(self.myset, self.otherset)
示例#22
0
    def test_update(self):
        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.__ior__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset |= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)
        self.assertIn(self.c1, self.myset)
示例#23
0
    def test_intersection_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__iand__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset &= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.intersection_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.a1, self.b2])
        set2 = TwoWaySyncSet([self.a1, self.c1])
        self.myset.intersection_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)
示例#24
0
class TwoWaySyncSetTest(_TwoWayBaseClass):
    def test_constructor(self):
        self.assertEqual(self.myset, self.otherset)
        self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
        self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        self.otherset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        self.assertEqual(self.myset, self.otherset)
        self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
        self.otherset = TwoWaySyncSet([self.c1, self.a1, self.b1])
        self.assertEqual(self.myset, self.otherset)
        self.assertEqual(self.myset.item_dict, self.otherset.item_dict)
        self.assertEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a1, self.a2]))
        self.assertTrue(TwoWaySyncSet([self.a2, self.a1]) == TwoWaySyncSet([self.a1, self.a2]))
        self.assertNotEqual(TwoWaySyncSet([self.a2, self.a1]), TwoWaySyncSet([self.a3, self.a1]))
        self.assertNotEqual(TwoWaySyncSet([self.a1, self.b1]), TwoWaySyncSet([self.a1, self.b2]))

    # Basic methods
    def test_copy(self):
        self.myset = TwoWaySyncSet([self.a1])
        set_copy = self.myset.copy()
        self.assertIsInstance(set_copy, TwoWaySyncSet)

    def test_add(self):
        for m in (self.a1, self.b1, self.c1):
            self.myset.add(m)
        self.assertEqual(len(self.myset), 3)
        self.assertEqual(self.myset[self.a1.get_id()], self.a1)
        self.assertNotEqual(self.myset[self.a1.get_id()], self.b1)
        self.assertNotEqual(self.myset[self.a1.get_id()], self.c1)
        self.assertEqual(self.myset[self.b1.get_id()], self.b1)
        self.assertEqual(self.myset[self.c1.get_id()], self.c1)

    def test_versions_add(self):
        # Also tests __getitem__
        for m in (self.a1, self.a2):
            self.myset.add(m)
        self.assertEqual(len(self.myset), 1)
        self.assertTrue(self.myset[self.a2.get_id()].__eq__(self.a1))
        self.assertEqual(self.myset[self.a2.get_id()].__cmp__(self.a2), 0)
        self.assertNotEqual(self.myset[self.a2.get_id()].__cmp__(self.a1), 0)

    def test_clear(self):
        for m in (self.a1, self.b3):
            self.myset.add(m)
        self.myset.clear()
        self.assertTrue(len(self.myset) == 0)

    def test_len(self):
        for m in (self.a1, self.a2):
            self.myset.add(m)
        self.assertTrue(len(self.myset) == 1)
        self.myset.add(self.b1)
        self.assertTrue(len(self.myset) == 2)

    # Basic operators
    def test_contains(self):
        self.myset.add(self.a1)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.a2, self.myset)
        self.assertNotIn(self.a3, self.myset)

    def test_contains_similar(self):
        self.myset.add(self.a1)
        self.assertTrue(self.myset.contains_similar(self.a1))
        self.assertTrue(self.myset.contains_similar(self.a2))
        self.assertFalse(self.myset.contains_similar(self.b1))

    def test_get(self):
        self.assertEqual(TwoWaySyncSet([self.a1, self.b1]).get(self.a1.get_id()), self.a1)

    def test_equal(self):
        self.assertEqual(
            TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()],
            TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()]
        )
        self.assertEqual(
            TwoWaySyncSet([self.a1, self.b1])[self.a1.get_id()],
            TwoWaySyncSet([self.b1, self.a1])[self.a1.get_id()]
        )
        self.assertEqual(TwoWaySyncSet([self.b1, self.a1]), TwoWaySyncSet([self.a1, self.b1]))
        self.assertNotEqual(OneWaySyncSet([self.a1]), OneWaySyncSet([self.a1, self.b1]))
        self.assertNotEqual(OneWaySyncSet([self.a1, self.b1]), OneWaySyncSet([self.a1, self.c1]))

    def test_remove(self):
        self.myset.add(self.a1)
        self.myset.remove(self.a1)
        self.assertTrue(len(self.myset) == 0)
        self.myset.add(self.a1)
        self.myset.remove(self.a2)
        self.assertTrue(len(self.myset) == 0)
        self.myset.add(self.a1)
        self.assertRaises(KeyError, self.myset.remove, self.b1)

    def test_discard(self):
        self.myset.add(self.a1)
        self.myset.discard(self.a1)
        self.assertTrue(len(self.myset) == 0)
        self.myset.add(self.a1)
        self.myset.discard(self.a2)
        self.assertTrue(len(self.myset) == 0)
        self.myset.discard(self.b1)
        self.assertTrue(len(self.myset) == 0)

    def test_pop(self):
        self.myset.add(self.a1)
        item = self.myset.pop()
        self.assertTrue(len(self.myset) == 0)
        self.assertEqual(id(item), id(self.a1))
        self.assertRaises(KeyError, self.myset.pop)

    # Boolean operators
    def test_isdisjoint(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.isdisjoint, self.otherset)

    def test_issubset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.issubset, self.otherset)
        self.assertRaises(UndefinedBehaviorError, self.myset.__le__, self.otherset)

    def test_istruesubset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.__lt__, self.otherset)

    def test_issuperset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.issuperset, self.otherset)
        self.assertRaises(UndefinedBehaviorError, self.myset.__ge__, self.otherset)

    def test_istruesuperset(self):
        self.assertRaises(UndefinedBehaviorError, self.myset.__gt__, self.otherset)

    # Operators
    def test_union(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__or__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset | self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.union(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b1, data)
        self.assertIn(self.c1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b2])
        set1 = TwoWaySyncSet([self.b1, self.c2])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.union(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.b2, data)
        self.assertIn(self.c2, data)

    def test_intersection(self):
        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave.__and__(set1)
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave & set1
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave.intersection(set1)
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

        self.myslave = OneWaySyncSet([self.a1, self.b1])
        set1 = OneWaySyncSet([self.a1, self.b2])
        set2 = OneWaySyncSet([self.a2, self.c1])
        data = self.myslave.intersection(set1, set2)
        self.assertIsInstance(data, OneWaySyncSet)
        self.assertIn(self.a2, data)
        self.assertNotIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.b2, data)
        self.assertNotIn(self.c1, data)

    def test_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.__sub__(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset - set1
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        data = self.myset.difference(set1)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        data = self.myset.difference(set1, set2)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertNotIn(self.b1, data)
        self.assertNotIn(self.c1, data)

    def test_symmetric_difference(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.__xor__(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset ^ self.otherset
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c1])
        data = self.myset.symmetric_difference(self.otherset)
        self.assertIsInstance(data, TwoWaySyncSet)
        self.assertIn(self.a1, data)
        self.assertIn(self.c1, data)
        self.assertNotIn(self.b1, data)

    # In-place operators
    def test_update(self):
        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.__ior__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset |= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        self.otherset = TwoWaySyncSet([self.b1])
        self.myset.update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.b1, self.myset)
        self.assertIn(self.c1, self.myset)

    def test_intersection_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__iand__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset &= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.intersection_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.b1, self.myset)
        self.assertNotIn(self.a1, self.myset)
        self.assertNotIn(self.c3, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        set1 = TwoWaySyncSet([self.a1, self.b2])
        set2 = TwoWaySyncSet([self.a1, self.c1])
        self.myset.intersection_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)

    def test_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__isub__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset -= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.difference_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)

    def test_symmetric_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__ixor__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset ^= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.symmetric_difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertIn(self.c3, self.myset)
        self.assertNotIn(self.b1, self.myset)

    # Diff tests
    def test_twoway_diff(self):
        for m in (self.a2, self.b2, self.c2):
            self.myset.add(m)
        for m in (self.a1, self.b2, self.c3):
            self.otherset.add(m)
        only_in_self, only_in_other, newer_in_self, newer_in_other = self.myset.diff(self.otherset)
        for coll in (only_in_self, only_in_other, newer_in_self, newer_in_other):
            self.assertIsInstance(coll, TwoWaySyncSet)
        self.assertEqual(only_in_self, TwoWaySyncSet())
        self.assertEqual(only_in_other, TwoWaySyncSet())
        self.assertEqual(newer_in_self, TwoWaySyncSet([self.a2]))
        self.assertEqual(newer_in_other, TwoWaySyncSet([self.c3]))

    def test_twoway_diff_2(self):
        m0 = TestMember(0, 1)
        m10 = TestMember(4, 1)
        for m in (self.a2, self.b1, m0):
            self.myset.add(m)
        for m in (self.a1, self.b2, m10):
            self.otherset.add(m)
        only_in_self, only_in_other, newer_in_self, newer_in_other = self.myset.diff(self.otherset)
        for coll in (only_in_self, only_in_other, newer_in_self, newer_in_other):
            self.assertIsInstance(coll, TwoWaySyncSet)
        self.assertEqual(only_in_self, TwoWaySyncSet([m0]))
        self.assertEqual(only_in_other, TwoWaySyncSet([m10]))
        self.assertEqual(newer_in_self, TwoWaySyncSet([self.a2]))
        self.assertEqual(newer_in_other, TwoWaySyncSet([self.b2]))

    # In-place updating
    def test_sync(self):
        only_in_self, only_in_master, _, updated_in_master = self.myset.diff(self.otherset)
        self.myset.sync(deleted=only_in_self, updated=updated_in_master, new=only_in_master)
        self.assertEqual(self.myset, self.otherset)
示例#25
0
    def test_difference_update(self):
        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.__isub__(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset -= self.otherset
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1])
        self.otherset = TwoWaySyncSet([self.b1, self.c3])
        self.myset.difference_update(self.otherset)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)

        self.myset = TwoWaySyncSet([self.a1, self.b1, self.c1])
        set1 = TwoWaySyncSet([self.b1])
        set2 = TwoWaySyncSet([self.c1])
        self.myset.difference_update(set1, set2)
        self.assertIsInstance(self.myset, TwoWaySyncSet)
        self.assertIn(self.a1, self.myset)
        self.assertNotIn(self.b1, self.myset)
        self.assertNotIn(self.c1, self.myset)