def test_revert_no_revisions(self): d1 = {"a": "aa", "b": "bb"} td1 = TraceableDict(d1) self.assertTrue(td1.has_uncommitted_changes) self.assertEquals(td1.as_dict(), d1) self.assertEquals(td1.trace, {}) self.assertEquals(td1.revisions, []) td1.revert() self.assertTrue(td1.has_uncommitted_changes) self.assertEquals(td1.as_dict(), d1) self.assertEquals(td1.trace, {}) self.assertEquals(td1.revisions, [])
def test_basic(self): base_revision = 0 d1 = {"a": "aa", "b": "bb"} td1 = TraceableDict(d1) td1.commit(revision=base_revision) self.assertEquals([base_revision], td1.revisions) r1 = 1 td1["a"] = 1 td1.commit(revision=r1) r2 = 2 td1["b"] = 2 td1.commit(revision=r2) self.assertFalse(td1.has_uncommitted_changes) self.assertEquals(td1.as_dict(), {'a': 1, 'b': 2}) self.assertEquals( td1.trace, { str(r1): [((root, 'a'), 'aa', key_updated)], str(r2): [((root, 'b'), 'bb', key_updated)] }) self.assertEquals([base_revision, r1, r2], td1.revisions) td1["b"] = 3 self.assertTrue(td1.has_uncommitted_changes) self.assertEquals(td1.as_dict(), {'a': 1, 'b': 3}) self.assertEquals( td1.trace, { str(r1): [((root, 'a'), 'aa', key_updated)], str(r2): [((root, 'b'), 'bb', key_updated)], uncommitted: [((root, 'b'), 2, key_updated)] }) self.assertEquals([base_revision, r1, r2], td1.revisions) td1.revert() self.assertFalse(td1.has_uncommitted_changes) self.assertEquals(td1.as_dict(), {"a": 1, "b": 2}) self.assertEquals( td1.trace, { str(r1): [((root, 'a'), 'aa', key_updated)], str(r2): [((root, 'b'), 'bb', key_updated)] }) self.assertEquals([base_revision, r1, r2], td1.revisions)
def test_remove_uncommitted_changes(self): r1, r2 = 1, 2 d1 = {"a": "a", "b": "b"} d2 = {"a": "a_updated", "b": "b"} d3 = {"a": "a_updated", "b": "b_updated"} td1 = TraceableDict(d1) td1.commit(revision=r1) self.assertEquals(td1.as_dict(), d1) td1 = td1 | d2 td1.commit(revision=r2) self.assertEquals(td1.as_dict(), d2) td1 = td1 | d3 self.assertEquals(td1.as_dict(), d3) self.assertTrue(td1.has_uncommitted_changes) self.assertEquals(td1.revisions, [r1, r2]) self.assertEquals(set(td1.trace.keys()), set([str(r2), uncommitted])) td1.remove_oldest_revision() self.assertEquals(td1.as_dict(), d3) self.assertEquals(td1.revisions, [r2]) self.assertEquals(set(td1.trace.keys()), set([uncommitted])) td1.remove_oldest_revision() self.assertEquals(td1.as_dict(), d3) self.assertEquals(td1.revisions, [r2]) self.assertEquals(set(td1.trace.keys()), set([uncommitted])) td1.revert() self.assertEquals(td1.as_dict(), d2) self.assertEquals(td1.revisions, [r2]) self.assertEquals(td1.trace, {})
def test_revert_to_first_revision(self): base_revision = 0 d1 = {"a": "aa", "b": "bb"} td1 = TraceableDict(d1) td1.commit(revision=base_revision) self.assertFalse(td1.has_uncommitted_changes) self.assertEquals(td1.as_dict(), d1) self.assertEquals(td1.trace, {}) self.assertEquals([base_revision], td1.revisions) td1["b"] = 1 self.assertTrue(td1.has_uncommitted_changes) self.assertNotEquals(td1.as_dict(), d1) self.assertNotEquals(td1.trace, {}) self.assertEquals([base_revision], td1.revisions) td1.revert() self.assertFalse(td1.has_uncommitted_changes) self.assertEquals(td1.as_dict(), d1) self.assertEquals(td1.trace, {}) self.assertEquals([base_revision], td1.revisions)
def test_revert_without_uncommitted_changes(self): r1, r2 = 1, 2 d1 = {"a": "aa", "b": "bb"} td1 = TraceableDict(d1) td1.commit(revision=r1) self.assertEquals([r1], td1.revisions) self.assertEquals(td1.as_dict(), d1) self.assertEquals(td1.trace, {}) self.assertFalse(td1.has_uncommitted_changes) td1.revert() self.assertEquals([r1], td1.revisions) self.assertEquals(td1.as_dict(), d1) self.assertEquals(td1.trace, {}) self.assertFalse(td1.has_uncommitted_changes) td1["a"] = 1 self.assertTrue(td1.has_uncommitted_changes) self.assertEquals(td1.trace, {uncommitted: [((root, 'a'), 'aa', key_updated)]}) self.assertEquals(td1.as_dict(), {"a": 1, "b": "bb"}) td1.revert() self.assertEquals(td1.as_dict(), {"a": "aa", "b": "bb"}) self.assertEquals(td1.trace, {}) td1["a"] = 1 td1.commit(revision=r2) _trace = {str(r2): [((root, 'a'), 'aa', key_updated)]} self.assertEquals([r1, r2], td1.revisions) self.assertEquals(td1.as_dict(), {"a": 1, "b": "bb"}) self.assertEquals(td1.trace, _trace) self.assertFalse(td1.has_uncommitted_changes) td1.revert() self.assertEquals([r1, r2], td1.revisions) self.assertEquals(td1.as_dict(), {"a": 1, "b": "bb"}) self.assertEquals(td1.trace, _trace) self.assertFalse(td1.has_uncommitted_changes)