~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_filechangecache.py

  • Committer: Martin Albisetti
  • Date: 2008-08-06 19:20:57 UTC
  • mfrom: (197.1.9 pathargs)
  • Revision ID: argentina@gmail.com-20080806192057-3bvnwx9ap53kw8v0
Misplaced fix syntax patch

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
class MockEntry(object):
 
9
    def __init__(self, revid):
 
10
        self.revid = revid
 
11
 
 
12
 
 
13
class MockHistory(object):
 
14
    def __init__(self):
 
15
        self.fetched_revids = set()
 
16
    def get_file_changes_uncached(self, entries):
 
17
        output = []
 
18
        for entry in entries:
 
19
            self.fetched_revids.add(entry.revid)
 
20
            output.append(entry.revid)
 
21
        return output
 
22
 
 
23
 
 
24
class TestFileChangeCache(object):
 
25
 
 
26
    # setup_method and teardown_method are so i can run the tests with
 
27
    # py.test and take advantage of the error reporting.
 
28
    def setup_method(self, meth):
 
29
        self.setUp()
 
30
 
 
31
    def teardown_method(self, meth):
 
32
        self.tearDown()
 
33
 
 
34
    def setUp(self):
 
35
        self.cache_folders = []
 
36
 
 
37
    def tearDown(self):
 
38
        for folder in self.cache_folders:
 
39
            shutil.rmtree(folder)
 
40
 
 
41
 
 
42
    def makeHistoryAndEntriesForRevids(self, revids, fill_cache_with=[]):
 
43
        cache_folder = tempfile.mkdtemp()
 
44
        self.cache_folders.append(cache_folder)
 
45
        self.history = MockHistory()
 
46
        self.cache = FileChangeCache(self.history, cache_folder)
 
47
 
 
48
        self.entries = [MockEntry(revid) for revid in revids]
 
49
 
 
50
        self.cache.get_file_changes([entry for entry in self.entries
 
51
                                     if entry.revid in fill_cache_with])
 
52
        self.history.fetched_revids.clear()
 
53
 
 
54
 
 
55
    def test_empty_cache(self):
 
56
        """An empty cache passes all the revids through to the history object.
 
57
        """
 
58
        revids = ['a', 'b']
 
59
        self.makeHistoryAndEntriesForRevids(revids)
 
60
 
 
61
        result = self.cache.get_file_changes(self.entries)
 
62
 
 
63
        assert result == revids
 
64
        assert self.history.fetched_revids == set(revids)
 
65
 
 
66
    def test_full_cache(self):
 
67
        """A full cache passes none of the revids through to the history
 
68
        object.
 
69
        """
 
70
        revids = ['a', 'b']
 
71
        self.makeHistoryAndEntriesForRevids(revids, fill_cache_with=revids)
 
72
 
 
73
        result = self.cache.get_file_changes(self.entries)
 
74
 
 
75
        assert result == revids
 
76
        assert self.history.fetched_revids == set()
 
77
 
 
78
    def test_partial_cache(self):
 
79
        """A partially full cache passes some of the revids through to the
 
80
        history object, and preserves the ordering of the argument list.
 
81
        """
 
82
        # To test the preservation of argument order code, we put the uncached
 
83
        # revid at the beginning, middle and then end of the list of revids
 
84
        # being asked for.
 
85
        for i in range(3):
 
86
            cached_revids = ['a', 'b']
 
87
            revids = cached_revids[:]
 
88
            revids.insert(i, 'uncached')
 
89
            self.makeHistoryAndEntriesForRevids(
 
90
                revids, fill_cache_with=cached_revids)
 
91
 
 
92
            result = self.cache.get_file_changes(self.entries)
 
93
            assert result == revids
 
94
 
 
95
            assert self.history.fetched_revids == set(['uncached'])