~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/workingtree_implementations/test_views.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-02-21 07:21:10 UTC
  • mfrom: (4029.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090221072110-uf8tjt38l6r3vo6o
Filtered views (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 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
"""Views stored within a working tree.
 
18
 
 
19
The views are actually in the WorkingTree.views namespace, but these are
 
20
1:1 with WorkingTree implementations so can be tested from here.
 
21
"""
 
22
 
 
23
 
 
24
from bzrlib import errors
 
25
from bzrlib.tests import TestSkipped
 
26
from bzrlib.workingtree import WorkingTree
 
27
 
 
28
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
 
29
 
 
30
 
 
31
class TestTreeViews(TestCaseWithWorkingTree):
 
32
 
 
33
    def setUp(self):
 
34
        # formats that don't support views can skip the rest of these
 
35
        # tests...
 
36
        fmt = self.workingtree_format
 
37
        f = getattr(fmt, 'supports_views')
 
38
        if f is None:
 
39
            raise TestSkipped("format %s doesn't declare whether it "
 
40
                "supports views, assuming not" % fmt)
 
41
        if not f():
 
42
            raise TestSkipped("format %s doesn't support views" % fmt)
 
43
        TestCaseWithWorkingTree.setUp(self)
 
44
 
 
45
    def test_views_initially_empty(self):
 
46
        wt = self.make_branch_and_tree('wt')
 
47
        current, views = wt.views.get_view_info()
 
48
        self.assertEqual(None, current)
 
49
        self.assertEqual({}, views)
 
50
 
 
51
    def test_set_and_get_view_info(self):
 
52
        wt = self.make_branch_and_tree('wt')
 
53
        view_current = 'view-name'
 
54
        view_dict = {
 
55
            view_current: ['dir-1'],
 
56
            'other-name': ['dir-2']}
 
57
        wt.views.set_view_info(view_current, view_dict)
 
58
        current, views = wt.views.get_view_info()
 
59
        self.assertEquals(view_current, current)
 
60
        self.assertEquals(view_dict, views)
 
61
        # then reopen the tree and see they're still there
 
62
        wt = WorkingTree.open('wt')
 
63
        current, views = wt.views.get_view_info()
 
64
        self.assertEquals(view_current, current)
 
65
        self.assertEquals(view_dict, views)
 
66
        # test setting a current view which does not exist
 
67
        self.assertRaises(errors.NoSuchView,
 
68
            wt.views.set_view_info, 'yet-another', view_dict)
 
69
        current, views = wt.views.get_view_info()
 
70
        self.assertEquals(view_current, current)
 
71
        self.assertEquals(view_dict, views)
 
72
        # test clearing the current view
 
73
        wt.views.set_view_info(None, view_dict)
 
74
        current, views = wt.views.get_view_info()
 
75
        self.assertEquals(None, current)
 
76
        self.assertEquals(view_dict, views)
 
77
 
 
78
    def test_lookup_view(self):
 
79
        wt = self.make_branch_and_tree('wt')
 
80
        view_current = 'view-name'
 
81
        view_dict = {
 
82
            view_current: ['dir-1'],
 
83
            'other-name': ['dir-2']}
 
84
        wt.views.set_view_info(view_current, view_dict)
 
85
        # test lookup of the default view
 
86
        result = wt.views.lookup_view()
 
87
        self.assertEqual(result, ['dir-1'])
 
88
        # test lookup of a named view
 
89
        result = wt.views.lookup_view('other-name')
 
90
        self.assertEqual(result, ['dir-2'])
 
91
 
 
92
    def test_set_view(self):
 
93
        wt = self.make_branch_and_tree('wt')
 
94
        # test that set_view sets the current view by default
 
95
        wt.views.set_view('view-1', ['dir-1'])
 
96
        current, views = wt.views.get_view_info()
 
97
        self.assertEquals('view-1', current)
 
98
        self.assertEquals({'view-1': ['dir-1']}, views)
 
99
        # test adding a view and not making it the current one
 
100
        wt.views.set_view('view-2', ['dir-2'], make_current=False)
 
101
        current, views = wt.views.get_view_info()
 
102
        self.assertEquals('view-1', current)
 
103
        self.assertEquals({'view-1': ['dir-1'], 'view-2': ['dir-2']}, views)
 
104
 
 
105
    def test_unicode_view(self):
 
106
        wt = self.make_branch_and_tree('wt')
 
107
        view_name = u'\u3070'
 
108
        view_files = ['foo', 'bar/']
 
109
        view_dict = {view_name: view_files}
 
110
        wt.views.set_view_info(view_name, view_dict)
 
111
        current, views = wt.views.get_view_info()
 
112
        self.assertEquals(view_name, current)
 
113
        self.assertEquals(view_dict, views)
 
114
 
 
115
    def test_no_such_view(self):
 
116
        wt = self.make_branch_and_tree('wt')
 
117
        try:
 
118
            wt.views.lookup_view('opaque')
 
119
        except errors.NoSuchView, e:
 
120
            self.assertEquals(e.view_name, 'opaque')
 
121
            self.assertEquals(str(e), 'No such view: opaque.')
 
122
        else:
 
123
            self.fail("didn't get expected exception")
 
124
 
 
125
    def test_delete_view(self):
 
126
        wt = self.make_branch_and_tree('wt')
 
127
        view_name = u'\N{GREEK SMALL LETTER ALPHA}'
 
128
        view_files = ['alphas/']
 
129
        wt.views.set_view(view_name, view_files)
 
130
        # now try to delete it
 
131
        wt.views.delete_view(view_name)
 
132
        # now you can't look it up
 
133
        self.assertRaises(errors.NoSuchView,
 
134
            wt.views.lookup_view, view_name)
 
135
        # and it's not in the dictionary
 
136
        self.assertEquals(wt.views.get_view_info()[1], {})
 
137
        # and you can't remove it a second time
 
138
        self.assertRaises(errors.NoSuchView,
 
139
            wt.views.delete_view, view_name)
 
140
        # or remove a view that never existed
 
141
        self.assertRaises(errors.NoSuchView,
 
142
            wt.views.delete_view, view_name + '2')
 
143
 
 
144
 
 
145
class TestUnsupportedViews(TestCaseWithWorkingTree):
 
146
    """Formats that don't support views should give reasonable errors."""
 
147
 
 
148
    def setUp(self):
 
149
        fmt = self.workingtree_format
 
150
        supported = getattr(fmt, 'supports_views')
 
151
        if supported is None:
 
152
            warn("Format %s doesn't declare whether it supports views or not"
 
153
                 % fmt)
 
154
            raise TestSkipped('No view support at all')
 
155
        if supported():
 
156
            raise TestSkipped("Format %s declares that views are supported"
 
157
                              % fmt)
 
158
            # it's covered by TestTreeViews
 
159
        TestCaseWithWorkingTree.setUp(self)
 
160
 
 
161
    def test_view_methods_raise(self):
 
162
        wt = self.make_branch_and_tree('wt')
 
163
        self.assertRaises(errors.ViewsNotSupported,
 
164
                wt.views.set_view_info, 'bar', {'bar': ['bars/']})
 
165
        self.assertRaises(errors.ViewsNotSupported,
 
166
                wt.views.get_view_info)
 
167
        self.assertRaises(errors.ViewsNotSupported,
 
168
            wt.views.lookup_view, 'foo')
 
169
        self.assertRaises(errors.ViewsNotSupported,
 
170
            wt.views.set_view, 'foo', 'bar')
 
171
        self.assertRaises(errors.ViewsNotSupported,
 
172
            wt.views.delete_view, 'foo')