1
"""Tests for the FileChangeCache in loggerhead.changecache."""
6
from loggerhead.changecache import FileChangeCache
9
class MockEntry(object):
11
def __init__(self, revid):
15
class MockHistory(object):
18
self.fetched_revids = set()
20
def get_file_changes_uncached(self, entries):
23
self.fetched_revids.add(entry.revid)
24
output.append(entry.revid)
28
class TestFileChangeCache(object):
30
# setup_method and teardown_method are so i can run the tests with
31
# py.test and take advantage of the error reporting.
33
def setup_method(self, meth):
36
def teardown_method(self, meth):
40
self.cache_folders = []
43
for folder in self.cache_folders:
46
def makeHistoryAndEntriesForRevids(self, revids, fill_cache_with=[]):
47
cache_folder = tempfile.mkdtemp()
48
self.cache_folders.append(cache_folder)
49
self.history = MockHistory()
50
self.cache = FileChangeCache(self.history, cache_folder)
52
self.entries = [MockEntry(revid) for revid in revids]
54
self.cache.get_file_changes([entry for entry in self.entries
55
if entry.revid in fill_cache_with])
56
self.history.fetched_revids.clear()
58
def test_empty_cache(self):
59
"""An empty cache passes all the revids through to the history object.
62
self.makeHistoryAndEntriesForRevids(revids)
64
result = self.cache.get_file_changes(self.entries)
66
assert result == revids
67
assert self.history.fetched_revids == set(revids)
69
def test_full_cache(self):
70
"""A full cache passes none of the revids through to the history
74
self.makeHistoryAndEntriesForRevids(revids, fill_cache_with=revids)
76
result = self.cache.get_file_changes(self.entries)
78
assert result == revids
79
assert self.history.fetched_revids == set()
81
def test_partial_cache(self):
82
"""A partially full cache passes some of the revids through to the
83
history object, and preserves the ordering of the argument list.
85
# To test the preservation of argument order code, we put the uncached
86
# revid at the beginning, middle and then end of the list of revids
89
cached_revids = ['a', 'b']
90
revids = cached_revids[:]
91
revids.insert(i, 'uncached')
92
self.makeHistoryAndEntriesForRevids(
93
revids, fill_cache_with=cached_revids)
95
result = self.cache.get_file_changes(self.entries)
96
assert result == revids
98
assert self.history.fetched_revids == set(['uncached'])