Пример #1
0
def test_block_change():

    with use_temporary_filename(
            'tests/test_catches_block_change.pkl') as file_path:

        pod1 = PersistentOrderedDict(file_path)
        pod1['a'] = 1
        assert list(pod1.items()) == [('a', 1)]
        pod2 = PersistentOrderedDict(file_path)

        with pod2:
            pod2['b'] = 2
            assert list(pod1.items()) == [('a', 1)]
        assert list(pod1.items()) == [('a', 1), ('b', 2)]
Пример #2
0
def test_has_changed():

    with use_temporary_filename(
            'tests/test_catches_modifications_has_changed.pkl') as file_path:

        pod1 = PersistentOrderedDict(file_path)
        pod1['a'] = 1
        assert not pod1.has_changed()

        pod2 = PersistentOrderedDict(file_path)
        assert not pod1.has_changed()

        assert pod2['a'] == 1
        assert not pod1.has_changed()

        pod2['a'] = 2
        assert pod1.has_changed()
Пример #3
0
def test_persistent_ordered_dict():

    with use_temporary_filename('tests/podtest.pkl') as file_path:

        pod = PersistentOrderedDict(file_path)
        assert list(pod.items()) == []
        pod['a'] = [1, 2, 3]
        pod['b'] = [4, 5, 6]
        pod['c'] = [7, 8]

        pod2 = PersistentOrderedDict(file_path)
        assert list(pod2.items()) == [('a', [1, 2, 3]), ('b', [4, 5, 6]),
                                      ('c', [7, 8])]
        pod['e'] = 11

        pod3 = PersistentOrderedDict(file_path)
        assert list(pod3.items()) == [('a', [1, 2, 3]), ('b', [4, 5, 6]),
                                      ('c', [7, 8]), ('e', 11)]
Пример #4
0
def test_persistent_ordered_dict():

    file_path = get_artemis_data_path('tests/podtest.pkl')
    if os.path.exists(file_path):
        os.remove(file_path)

    with PersistentOrderedDict(file_path) as pod:
        assert pod.items() == []
        pod['a'] = [1, 2, 3]
        pod['b'] = [4, 5, 6]
        pod['c'] = [7, 8]
    pod['d'] = [9, 10]  # Should not be recorded

    with PersistentOrderedDict(file_path) as pod:
        assert pod.items() == [('a', [1, 2, 3]), ('b', [4, 5, 6]), ('c', [7, 8])]
        pod['e']=11

    with PersistentOrderedDict(file_path) as pod:
        assert pod.items() == [('a', [1, 2, 3]), ('b', [4, 5, 6]), ('c', [7, 8]), ('e', 11)]
Пример #5
0
def test_catches_modifications():

    with use_temporary_filename(
            'tests/test_catches_modifications.pkl') as file_path:

        pod1 = PersistentOrderedDict(file_path)

        pod1['a'] = 1

        pod2 = PersistentOrderedDict(file_path)
        assert pod2['a'] == 1

        pod1['a'] = 2
        assert pod2['a'] == 2

        pod2['a'] = 3
        assert pod1['a'] == 3

        pod2['b'] = 4
        assert list(pod1.items()) == [('a', 3), ('b', 4)]

        pod3 = PersistentOrderedDict(file_path, items=[('b', 5), ('c', 6)])
        assert list(pod1.items()) == list(pod2.items()) == list(
            pod3.items()) == [('a', 3), ('b', 5), ('c', 6)]
Пример #6
0
 def __init__(self, file_path, write_text_version=True):
     before, ext = os.path.splitext(file_path)
     assert ext == '.pkl', 'Your file-path must be a pickle'
     self._text_path = before + '.txt' if write_text_version else None
     self.persistent_obj = PersistentOrderedDict(file_path=file_path)