def test_remove_one(): crdt = CRDT() crdt.add('a', 1) crdt.remove('a') assert len(crdt.log) == 2 assert len(crdt.data) == 0 assert crdt.log[1].op == REMOVE
def test_remove_many(ans): crdt = CRDT() for k, v in ans.items(): crdt.add(k, v) for k in ans: crdt.remove(k) # The same set of keys is added and removed, the length of the log should be twice that of the ans set assert len(crdt.log) == len(ans) * 2 assert len(crdt.data) == 0
def test_merge_remove_same_key_twice(): crdt_1, crdt_2 = CRDT(), CRDT() crdt_1.add('a', 1) crdt_2.add('a', 1) crdt_1.remove('a') crdt_2.remove('a') # In a merged set, since the key is removed before, the later remove should be ignored merged = merge(crdt_1, crdt_2) assert len(merged.data) == 0
def test_update_after_remove(): crdt = CRDT() crdt.add('a', 1) try: crdt.update('a', 'b') crdt.remove('a') # In the same instance, this operation should fail assert False except KeyError: pass
def test_remove_same_key_twice(): crdt = CRDT() crdt.add('a', 1) try: crdt.remove('a') crdt.remove('a') # In the same instance, this operation should fail assert False except KeyError: pass
def test_merge_add_remove_update(ans, upd): crdt_1, crdt_2 = CRDT(), CRDT() ans_1, ans_2 = slice_dict(ans, 0, len(ans) // 2), slice_dict(ans, len(ans) // 2, len(ans)) for k, v in ans.items(): crdt_1.add(k, v) crdt_2.add(k, v) # Remove the elements in the second half of the ans set in crdt_1 for k in ans_2: crdt_1.remove(k) # Update same keys that are removed in crdt_1 in crdt_2 for k in ans_2: crdt_2.update(k, upd[k]) merged = merge(crdt_1, crdt_2) # Since this is a LWW set, the **updates** will overwrite the **removes** since they're executed later ans_1.update({upd[k]:v for k, v in ans_2.items()}) assert merged.data == ans_1