~bzr-pqm/bzr/bzr.dev

2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for repository statistic-gathering apis."""
18
19
from bzrlib.tests.repository_implementations.test_repository import TestCaseWithRepository
20
21
22
class TestGatherStats(TestCaseWithRepository):
23
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
24
    def check_stats_has_size(self, stats):
25
        """Check that stats has a reasonable size entry."""
26
        # actual disk size varies from implementation to implementation,
27
        # but they should all provide it on their native transport.
28
        self.assertTrue('size' in stats)
29
        # and it should be a number
30
        self.assertIsInstance(stats['size'], (int, long))
31
        # and now remove it to make other assertions work without variation.
32
        del stats['size']
33
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
34
    def test_gather_stats(self):
35
        """First smoke test covering the refactoring into the Repository api."""
36
        tree = self.make_branch_and_memory_tree('.')
37
        tree.lock_write()
38
        tree.add('')
39
        # three commits: one to be included by reference, one to be 
40
        # requested, and one to be in the repository but [mostly] ignored.
41
        rev1 = tree.commit('first post', committer='person 1',
42
            timestamp=1170491381, timezone=0)
43
        rev2 = tree.commit('second post', committer='person 2',
44
            timestamp=1171491381, timezone=0)
45
        rev3 = tree.commit('third post', committer='person 3',
46
            timestamp=1172491381, timezone=0)
47
        tree.unlock()
48
        # now, in the same repository, asking for stats with/without the 
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
49
        # committers flag generates the same date information.
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
50
        stats = tree.branch.repository.gather_stats(rev2, committers=False)
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
51
        self.check_stats_has_size(stats)
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
52
        self.assertEqual({
53
            'firstrev': (1170491381.0, 0),
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
54
            'latestrev': (1171491381.0, 0),
55
            'revisions': 3,
56
            },
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
57
            stats)
58
        stats = tree.branch.repository.gather_stats(rev2, committers=True)
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
59
        self.check_stats_has_size(stats)
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
60
        self.assertEqual({
61
            'committers': 2,
62
            'firstrev': (1170491381.0, 0),
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
63
            'latestrev': (1171491381.0, 0),
64
            'revisions': 3,
65
            },
66
            stats)
67
68
    def test_gather_stats_norevid_gets_size(self):
69
        """Without a revid, repository size is still gathered."""
70
        tree = self.make_branch_and_memory_tree('.')
71
        tree.lock_write()
72
        tree.add('')
73
        # put something in the repository, because zero-size is borink.
74
        rev1 = tree.commit('first post')
75
        tree.unlock()
76
        # now ask for global repository stats.
77
        stats = tree.branch.repository.gather_stats()
78
        self.check_stats_has_size(stats)
79
        self.assertEqual({
80
            'revisions': 1
81
            },
82
            stats)
83
84
    def test_gather_stats_empty_repo(self):
85
        """An empty repository still has size and revisions."""
86
        tree = self.make_branch_and_memory_tree('.')
87
        # now ask for global repository stats.
88
        stats = tree.branch.repository.gather_stats()
89
        self.check_stats_has_size(stats)
90
        self.assertEqual({
91
            'revisions': 0
92
            },
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
93
            stats)