~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_filechangecache.py

  • Committer: Michael Hudson
  • Date: 2009-03-02 05:09:06 UTC
  • Revision ID: michael.hudson@canonical.com-20090302050906-n7fderzauzhsj7yw
comment out more forcefully

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for the FileChangeCache in loggerhead.changecache."""
 
2
 
 
3
import shutil
 
4
import tempfile
 
5
 
 
6
from loggerhead.changecache import FileChangeCache
 
7
 
 
8
 
 
9
class MockEntry(object):
 
10
 
 
11
    def __init__(self, revid):
 
12
        self.revid = revid
 
13
 
 
14
 
 
15
class MockHistory(object):
 
16
 
 
17
    def __init__(self):
 
18
        self.fetched_revids = set()
 
19
 
 
20
    def get_file_changes_uncached(self, entries):
 
21
        output = []
 
22
        for entry in entries:
 
23
            self.fetched_revids.add(entry.revid)
 
24
            output.append(entry.revid)
 
25
        return output
 
26
 
 
27
 
 
28
class TestFileChangeCache(object):
 
29
 
 
30
    # setup_method and teardown_method are so i can run the tests with
 
31
    # py.test and take advantage of the error reporting.
 
32
 
 
33
    def setup_method(self, meth):
 
34
        self.setUp()
 
35
 
 
36
    def teardown_method(self, meth):
 
37
        self.tearDown()
 
38
 
 
39
    def setUp(self):
 
40
        self.cache_folders = []
 
41
 
 
42
    def tearDown(self):
 
43
        for folder in self.cache_folders:
 
44
            shutil.rmtree(folder)
 
45
 
 
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)
 
51
 
 
52
        self.entries = [MockEntry(revid) for revid in revids]
 
53
 
 
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()
 
57
 
 
58
    def test_empty_cache(self):
 
59
        """An empty cache passes all the revids through to the history object.
 
60
        """
 
61
        revids = ['a', 'b']
 
62
        self.makeHistoryAndEntriesForRevids(revids)
 
63
 
 
64
        result = self.cache.get_file_changes(self.entries)
 
65
 
 
66
        assert result == revids
 
67
        assert self.history.fetched_revids == set(revids)
 
68
 
 
69
    def test_full_cache(self):
 
70
        """A full cache passes none of the revids through to the history
 
71
        object.
 
72
        """
 
73
        revids = ['a', 'b']
 
74
        self.makeHistoryAndEntriesForRevids(revids, fill_cache_with=revids)
 
75
 
 
76
        result = self.cache.get_file_changes(self.entries)
 
77
 
 
78
        assert result == revids
 
79
        assert self.history.fetched_revids == set()
 
80
 
 
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.
 
84
        """
 
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
 
87
        # being asked for.
 
88
        for i in range(3):
 
89
            cached_revids = ['a', 'b']
 
90
            revids = cached_revids[:]
 
91
            revids.insert(i, 'uncached')
 
92
            self.makeHistoryAndEntriesForRevids(
 
93
                revids, fill_cache_with=cached_revids)
 
94
 
 
95
            result = self.cache.get_file_changes(self.entries)
 
96
            assert result == revids
 
97
 
 
98
            assert self.history.fetched_revids == set(['uncached'])