def test_update_one(): crdt = CRDT() crdt.add('a', 1) crdt.update('a', 'b') assert len(crdt.log) == 2 assert crdt.data == {'b': 1} assert crdt.log[1].op == UPDATE
def test_update_key_collision(): crdt = CRDT() crdt.add('a', 1) crdt.add('b', 2) crdt.update('a', 'b') # In this implementation of LWW the update should overwrite the original key if the key already exists assert crdt.data == {'b': 1}
def test_merge_update_key_collision(): crdt_1, crdt_2 = CRDT(), CRDT() crdt_1.add('a', 1) crdt_2.add('b', 2) crdt_1.update('a', 'b') merged = merge(crdt_1, crdt_2) # In this implementation of LWW the update should overwrite the original key if the key already exists assert merged.data == {'b': 1}
def test_update_many(ans, upd): crdt = CRDT() for k, v in ans.items(): crdt.add(k, v) for k, v in upd.items(): crdt.update(k, v) # The keys are added then updated, the length of the log should be the sum of the lengths of the ans and upd set assert len(crdt.log) == len(ans) + len(upd) assert crdt.data == {upd[k]:v for k, v in ans.items()}
def test_merge_update_same_key_twice(): crdt_1, crdt_2 = CRDT(), CRDT() crdt_1.add('a', 1) crdt_2.add('a', 2) crdt_1.update('a', 'b') crdt_2.update('a', 'c') merged = merge(crdt_1, crdt_2) # In a LWW merge, only the latest update should be present assert merged.data == {'c': 2}
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_merge_add_update_remove(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) # Update the elements in the second half of the ans set in crdt_1 (opposite of test_merge_add_remove_update()) for k in ans_2: crdt_1.update(k, upd[k]) # Remove old keys that are updated in crdt_1 in crdt_2 (opposite of test_merge_add_remove_update()) for k in ans_2: crdt_2.remove(k) merged = merge(crdt_1, crdt_2) # Since this is a LWW set, the **removes** will overwrite the **updates** since they're executed later assert merged.data == ans_1