Exemple #1
0
    def test_checkout_no_revisions(self):
        d1 = {"a": "aa", "b": "bb"}
        td1 = TraceableDict(d1)

        with self.assertRaises(Exception) as err:
            td1.checkout(revision=1)
        self.assertTrue(
            'no revisions available. you must commit an initial revision first.'
            in err.exception)
Exemple #2
0
    def test_checkout_invalid_revision(self):
        d1 = {"a": "aa", "b": "bb"}
        td1 = TraceableDict(d1)
        td1.commit(revision=1)

        with self.assertRaises(ValueError) as err:
            td1.checkout(revision=None)
        self.assertTrue("revision must be an integer" in err.exception)

        with self.assertRaises(ValueError) as err:
            td1.checkout(revision="invalid")
        self.assertTrue("revision must be an integer" in err.exception)
Exemple #3
0
    def test_basic(self):
        r1, r2, r3, r4, r5 = 1, 2, 3, 4, 5

        d1 = {"a": "aa", "b": "bb"}
        d2 = {"a": "a", "b": "bb"}
        d3 = {"a": "a", "b": "b"}
        d4 = {"a": "a"}
        d5 = {"aa": "aa", "bb": "bb", "c": "c"}

        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
        td1.commit(revision=r3)
        self.assertEquals(td1.as_dict(), d3)

        td1 = td1 | d4
        td1.commit(revision=r4)
        self.assertEquals(td1.as_dict(), d4)

        td1 = td1 | d5
        td1.commit(revision=r5)
        self.assertEquals(td1.as_dict(), d5)

        self.assertEquals(td1.revisions, [r1, r2, r3, r4, r5])

        td1.remove_oldest_revision()

        self.assertEquals(td1.as_dict(), d5)
        self.assertEquals(td1.revisions, [r2, r3, r4, r5])
        self.assertEquals(set(td1.trace.keys()),
                          set([str(r3), str(r4), str(r5)]))

        td_past_revision = td1.checkout(revision=r2)
        self.assertEquals(td_past_revision.as_dict(), d2)

        td1.remove_oldest_revision()

        self.assertEquals(td1.as_dict(), d5)
        self.assertEquals(td1.revisions, [r3, r4, r5])
        self.assertEquals(set(td1.trace.keys()), set([str(r4), str(r5)]))

        td_past_revision = td1.checkout(revision=r3)
        self.assertEquals(td_past_revision.as_dict(), d3)
Exemple #4
0
    def test_checkout_uncommitted_changes(self):
        r1 = 1

        d1 = {"a": "aa", "b": "bb"}
        td1 = TraceableDict(d1)

        td1["a"] = 1
        td1.commit(revision=r1)

        td1["b"] = 2
        self.assertTrue(td1.has_uncommitted_changes)

        with self.assertRaises(Exception) as err:
            td1.checkout(revision=r1)
        self.assertTrue(
            'dictionary has uncommitted changes. you must commit or revert first.'
            in err.exception)
Exemple #5
0
    def test_checkout_unknown_revision(self):
        r1, r2, r3 = 1, 2, 3

        d1 = {"a": "aa", "b": "bb"}
        td1 = TraceableDict(d1)
        td1.commit(revision=r1)

        td1["a"] = 1
        td1.commit(revision=r2)

        td1["b"] = 2
        td1.commit(revision=r3)

        unknown_revision = 55
        with self.assertRaises(ValueError) as err:
            td1.checkout(revision=unknown_revision)
        self.assertTrue('unknown revision %s' %
                        unknown_revision in err.exception)
Exemple #6
0
    def test_basic(self):
        r1, r2, r3 = 1, 2, 3

        d1 = {"a": "aa", "b": "bb"}
        td1 = TraceableDict(d1)
        td1.commit(revision=r1)

        td1["a"] = 2
        td1.commit(revision=r2)

        td1["b"] = 3
        td1.commit(revision=r3)

        self.assertEquals(td1.as_dict(), {'a': 2, 'b': 3})
        self.assertEquals(
            td1.trace, {
                str(r2): [((root, 'a'), 'aa', key_updated)],
                str(r3): [((root, 'b'), 'bb', key_updated)]
            })
        self.assertEquals([r1, r2, r3], td1.revisions)

        result_r2 = td1.checkout(revision=r2)
        self.assertFalse(result_r2.has_uncommitted_changes)
        self.assertEquals(result_r2.as_dict(), {'a': 2, 'b': 'bb'})
        self.assertEquals(result_r2.trace,
                          {str(r2): [((root, 'a'), 'aa', key_updated)]})
        self.assertEquals([r1, r2], result_r2.revisions)

        result_r1_1 = td1.checkout(revision=r1)
        self.assertFalse(result_r1_1.has_uncommitted_changes)
        self.assertEquals(result_r1_1.as_dict(), {'a': 'aa', 'b': 'bb'})
        self.assertEquals(result_r1_1.trace, {})
        self.assertEquals([r1], result_r1_1.revisions)

        result_r1_2 = result_r2.checkout(revision=r1)
        self.assertEquals(result_r1_1.has_uncommitted_changes,
                          result_r1_2.has_uncommitted_changes)
        self.assertEquals(result_r1_1.as_dict(), result_r1_2.as_dict())
        self.assertEquals(result_r1_1.trace, result_r1_2.trace)
        self.assertEquals(result_r1_1.revisions, result_r1_2.revisions)
Exemple #7
0
    def test_checkout_current_revision(self):
        r1, r2 = 1, 2

        d1 = {"a": "aa", "b": "bb"}
        td1 = TraceableDict(d1)

        td1["a"] = 1
        td1.commit(revision=r1)

        td1["b"] = 2
        td1.commit(revision=r2)

        result_r2 = td1.checkout(revision=r2)
        self.assertEquals(result_r2.has_uncommitted_changes,
                          td1.has_uncommitted_changes)
        self.assertEquals(result_r2.as_dict(), td1.as_dict())
        self.assertEquals(result_r2.trace, td1.trace)
        self.assertEquals(result_r2.revisions, td1.revisions)
Exemple #8
0
    def test_checkout_key_removed(self):
        r1, r2 = 1, 2

        d1 = {}
        d2 = {"a": "aa", "b": "bb"}

        td1 = TraceableDict(d1)
        td1.commit(revision=r1)
        td2 = TraceableDict(d2)
        td2.commit(revision=r1)

        td1 = td1 | td2
        td1.commit(revision=r2)

        result_r1 = td1.checkout(revision=r1)
        self.assertFalse(result_r1.has_uncommitted_changes)
        self.assertEquals(result_r1.as_dict(), d1)
        self.assertEquals(result_r1.trace, {})
        self.assertEquals([r1], result_r1.revisions)
Exemple #9
0
    def test_full(self):
        r1 = int(time.time() * 1000)

        d1 = self._d1.copy()
        d2 = d1.copy()
        d2['new_key'] = 'new_val'

        D1 = TraceableDict(d1)
        D2 = TraceableDict(d2)
        D3 = TraceableDict(self._d2)

        D1 = D1 | D2
        D1_Base = TraceableDict(D1)
        D1.commit(revision=r1)

        self.assertEquals(D1.as_dict(), d2)
        self.assertEquals(D1.trace, {})
        self.assertEquals(D1.revisions, [r1])

        D1 = D1 | D3
        self.assertTrue(D1.has_uncommitted_changes)
        self.assertEquals(D1.as_dict(), self._d2)
        self.assertEquals(D1.revisions, [r1])

        r2 = int((time.time() - 5000) * 1000)
        with self.assertRaises(ValueError) as err:
            D1.commit(revision=r2)
        self.assertTrue('cannot commit to earlier revision' in err.exception)

        r2 = int((time.time() + 5000) * 1000)
        D1.commit(revision=r2)

        self.assertFalse(D1.has_uncommitted_changes)
        self.assertEquals(D1.as_dict(), self._d2)
        self.assertEquals(D1.revisions, [r1, r2])

        result_base = D1.checkout(revision=r1)
        self.assertEquals(result_base.as_dict(), D1_Base.as_dict())
        self.assertEquals(result_base.revisions, [r1])