~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_controllers.py

Merge Francesco's fix for bug #812583

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import simplejson
 
2
 
1
3
from loggerhead.apps.branch import BranchWSGIApp
2
4
from loggerhead.controllers.annotate_ui import AnnotateUI
3
5
from loggerhead.controllers.inventory_ui import InventoryUI
4
6
from loggerhead.controllers.revision_ui import RevisionUI
5
 
from loggerhead.tests.test_simple import BasicTests
 
7
from loggerhead.tests.test_simple import BasicTests, consume_app
6
8
from loggerhead import util
7
9
 
8
10
 
9
11
class TestInventoryUI(BasicTests):
10
12
 
 
13
    def make_bzrbranch_for_tree_shape(self, shape):
 
14
        tree = self.make_branch_and_tree('.')
 
15
        self.build_tree(shape)
 
16
        tree.smart_add([])
 
17
        tree.commit('')
 
18
        self.addCleanup(tree.branch.lock_read().unlock)
 
19
        return tree.branch
 
20
 
11
21
    def make_bzrbranch_and_inventory_ui_for_tree_shape(self, shape):
12
 
        tree = self.make_branch_and_tree('.')
13
 
        self.build_tree(shape)
14
 
        tree.smart_add([])
15
 
        tree.commit('')
16
 
        tree.branch.lock_read()
17
 
        self.addCleanup(tree.branch.unlock)
18
 
        branch_app = BranchWSGIApp(tree.branch)
19
 
        return tree.branch, InventoryUI(branch_app, branch_app.get_history)
 
22
        branch = self.make_bzrbranch_for_tree_shape(shape)
 
23
        branch_app = self.make_branch_app(branch)
 
24
        return branch, InventoryUI(branch_app, branch_app.get_history)
20
25
 
21
26
    def test_get_filelist(self):
22
27
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
23
28
            ['filename'])
24
29
        inv = bzrbranch.repository.get_inventory(bzrbranch.last_revision())
25
 
        self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename')))
 
30
        self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename', 'head')))
 
31
 
 
32
    def test_smoke(self):
 
33
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
 
34
            ['filename'])
 
35
        start, content = consume_app(inv_ui,
 
36
            {'SCRIPT_NAME': '/files', 'PATH_INFO': ''})
 
37
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
 
38
                         start)
 
39
        self.assertContainsRe(content, 'filename')
 
40
 
 
41
    def test_no_content_for_HEAD(self):
 
42
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
 
43
            ['filename'])
 
44
        start, content = consume_app(inv_ui,
 
45
            {'SCRIPT_NAME': '/files', 'PATH_INFO': '',
 
46
             'REQUEST_METHOD': 'HEAD'})
 
47
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
 
48
                         start)
 
49
        self.assertEqual('', content)
 
50
 
 
51
    def test_get_values_smoke(self):
 
52
        branch = self.make_bzrbranch_for_tree_shape(['a-file'])
 
53
        branch_app = self.make_branch_app(branch)
 
54
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/files'}
 
55
        inv_ui = branch_app.lookup_app(env)
 
56
        inv_ui.parse_args(env)
 
57
        values = inv_ui.get_values('', {}, {})
 
58
        self.assertEqual('a-file', values['filelist'][0].filename)
 
59
 
 
60
    def test_json_render_smoke(self):
 
61
        branch = self.make_bzrbranch_for_tree_shape(['a-file'])
 
62
        branch_app = self.make_branch_app(branch)
 
63
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/files'}
 
64
        inv_ui = branch_app.lookup_app(env)
 
65
        self.assertOkJsonResponse(inv_ui, env)
26
66
 
27
67
 
28
68
class TestRevisionUI(BasicTests):
29
69
 
30
 
    def make_bzrbranch_and_revision_ui_for_tree_shapes(self, shape1, shape2):
 
70
    def make_branch_app_for_revision_ui(self, shape1, shape2):
31
71
        tree = self.make_branch_and_tree('.')
32
72
        self.build_tree_contents(shape1)
33
73
        tree.smart_add([])
34
 
        tree.commit('')
 
74
        tree.commit('msg 1', rev_id='rev-1')
35
75
        self.build_tree_contents(shape2)
36
76
        tree.smart_add([])
37
 
        tree.commit('')
38
 
        tree.branch.lock_read()
39
 
        self.addCleanup(tree.branch.unlock)
40
 
        branch_app = BranchWSGIApp(tree.branch)
41
 
        branch_app._environ = {
42
 
            'wsgi.url_scheme':'',
43
 
            'SERVER_NAME':'',
44
 
            'SERVER_PORT':'80',
45
 
            }
46
 
        branch_app._url_base = ''
47
 
        branch_app.friendly_name = ''
48
 
        return tree.branch, RevisionUI(branch_app, branch_app.get_history)
 
77
        tree.commit('msg 2', rev_id='rev-2')
 
78
        branch = tree.branch
 
79
        self.addCleanup(branch.lock_read().unlock)
 
80
        return self.make_branch_app(branch)
49
81
 
50
82
    def test_get_values(self):
51
 
        branch, rev_ui = self.make_bzrbranch_and_revision_ui_for_tree_shapes(
52
 
            [], [])
53
 
        rev_ui.args = ['2']
54
 
        util.set_context({})
55
 
        self.assertIsInstance(
56
 
            rev_ui.get_values('2', {}, []),
57
 
            dict)
58
 
 
59
 
    def test_get_changes_with_diff(self):
60
 
        branch, rev_ui = self.make_bzrbranch_and_revision_ui_for_tree_shapes(
61
 
            [('file', 'oldcontents'), ('file2', 'oldcontents')],
62
 
            [('file', 'newcontents'), ('file2', 'oldcontents')])
63
 
        change = rev_ui._history.get_changes([branch.last_revision()])[0]
64
 
        changes, diffs = rev_ui.get_changes_with_diff(change, None, None)
65
 
        self.assertEqual(1, len(diffs))
66
 
 
67
 
    def test_get_changes_with_diff_specific_path(self):
68
 
        branch, rev_ui = self.make_bzrbranch_and_revision_ui_for_tree_shapes(
69
 
            [('file', 'oldcontents'), ('file2', 'oldcontents')],
70
 
            [('file', 'newcontents'), ('file2', 'newcontents')])
71
 
        change = rev_ui._history.get_changes([branch.last_revision()])[0]
72
 
        changes, diffs = rev_ui.get_changes_with_diff(change, None, 'file')
73
 
        self.assertEqual(1, len(diffs))
 
83
        branch_app = self.make_branch_app_for_revision_ui([], [])
 
84
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/revision/2'}
 
85
        rev_ui = branch_app.lookup_app(env)
 
86
        rev_ui.parse_args(env)
 
87
        self.assertIsInstance(rev_ui.get_values('', {}, []), dict)
 
88
 
 
89
    def test_get_values_smoke(self):
 
90
        branch_app = self.make_branch_app_for_revision_ui(
 
91
                [('file', 'content\n'), ('other-file', 'other\n')],
 
92
                [('file', 'new content\n')])
 
93
        env = {'SCRIPT_NAME': '/',
 
94
               'PATH_INFO': '/revision/head:'}
 
95
        revision_ui = branch_app.lookup_app(env)
 
96
        revision_ui.parse_args(env)
 
97
        values = revision_ui.get_values('', {}, {})
 
98
 
 
99
        self.assertEqual(values['revid'], 'rev-2')
 
100
        self.assertEqual(values['change'].comment, 'msg 2')
 
101
        self.assertEqual(values['file_changes'].modified[0].filename, 'file')
 
102
        self.assertEqual(values['merged_in'], None)
 
103
 
 
104
    def test_json_render_smoke(self):
 
105
        branch_app = self.make_branch_app_for_revision_ui(
 
106
                [('file', 'content\n'), ('other-file', 'other\n')],
 
107
                [('file', 'new content\n')])
 
108
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/revision/head:'}
 
109
        revision_ui = branch_app.lookup_app(env)
 
110
        self.assertOkJsonResponse(revision_ui, env)
 
111
 
 
112
 
74
113
 
75
114
class TestAnnotateUI(BasicTests):
76
115
 
77
116
    def make_annotate_ui_for_file_history(self, file_id, rev_ids_texts):
78
117
        tree = self.make_branch_and_tree('.')
79
 
        open('filename', 'w').write('')
 
118
        self.build_tree_contents([('filename', '')])
80
119
        tree.add(['filename'], [file_id])
81
 
        for rev_id, text in rev_ids_texts:
82
 
            open('filename', 'w').write(text)
83
 
            tree.commit(rev_id=rev_id, message='.')
 
120
        for rev_id, text, message in rev_ids_texts:
 
121
            self.build_tree_contents([('filename', text)])
 
122
            tree.commit(rev_id=rev_id, message=message)
84
123
        tree.branch.lock_read()
85
124
        self.addCleanup(tree.branch.unlock)
86
 
        branch_app = BranchWSGIApp(tree.branch)
 
125
        branch_app = BranchWSGIApp(tree.branch, friendly_name='test_name')
87
126
        return AnnotateUI(branch_app, branch_app.get_history)
88
127
 
89
128
    def test_annotate_file(self):
90
 
        history = [('rev1', 'old\nold\n'), ('rev2', 'new\nold\n')]
 
129
        history = [('rev1', 'old\nold\n', '.'), ('rev2', 'new\nold\n', '.')]
91
130
        ann_ui = self.make_annotate_ui_for_file_history('file_id', history)
92
 
        annotated = list(ann_ui.annotate_file('file_id', 'rev2'))
 
131
        # A lot of this state is set up by __call__, but we'll do it directly
 
132
        # here.
 
133
        ann_ui.args = ['rev2']
 
134
        annotate_info = ann_ui.get_values('filename',
 
135
            kwargs={'file_id': 'file_id'}, headers={})
 
136
        annotated = annotate_info['annotated']
93
137
        self.assertEqual(2, len(annotated))
94
 
        self.assertEqual('2', annotated[0].change.revno)
95
 
        self.assertEqual('1', annotated[1].change.revno)
 
138
        self.assertEqual('2', annotated[1].change.revno)
 
139
        self.assertEqual('1', annotated[2].change.revno)
 
140
    
 
141
    def test_annotate_empty_comment(self):
 
142
        # Testing empty comment handling without breaking
 
143
        history = [('rev1', 'old\nold\n', '.'), ('rev2', 'new\nold\n', '')]
 
144
        ann_ui = self.make_annotate_ui_for_file_history('file_id', history)
 
145
        ann_ui.args = ['rev2']
 
146
        annotate_info = ann_ui.get_values('filename',
 
147
            kwargs={'file_id': 'file_id'}, headers={})
 
148
    
 
149
class TestFileDiffUI(BasicTests):
 
150
 
 
151
    def make_branch_app_for_filediff_ui(self):
 
152
        builder = self.make_branch_builder('branch')
 
153
        builder.start_series()
 
154
        builder.build_snapshot('rev-1-id', None, [
 
155
            ('add', ('', 'root-id', 'directory', '')),
 
156
            ('add', ('filename', 'f-id', 'file', 'content\n'))],
 
157
            message="First commit.")
 
158
        builder.build_snapshot('rev-2-id', None, [
 
159
            ('modify', ('f-id', 'new content\n'))])
 
160
        builder.finish_series()
 
161
        branch = builder.get_branch()
 
162
        self.addCleanup(branch.lock_read().unlock)
 
163
        return self.make_branch_app(branch)
 
164
 
 
165
    def test_get_values_smoke(self):
 
166
        branch_app = self.make_branch_app_for_filediff_ui()
 
167
        env = {'SCRIPT_NAME': '/',
 
168
               'PATH_INFO': '/+filediff/rev-2-id/rev-1-id/f-id'}
 
169
        filediff_ui = branch_app.lookup_app(env)
 
170
        filediff_ui.parse_args(env)
 
171
        values = filediff_ui.get_values('', {}, {})
 
172
        chunks = values['chunks']
 
173
        self.assertEqual('insert', chunks[0].diff[1].type)
 
174
        self.assertEqual('new content', chunks[0].diff[1].line)
 
175
 
 
176
    def test_json_render_smoke(self):
 
177
        branch_app = self.make_branch_app_for_filediff_ui()
 
178
        env = {'SCRIPT_NAME': '/',
 
179
               'PATH_INFO': '/+json/+filediff/rev-2-id/rev-1-id/f-id'}
 
180
        filediff_ui = branch_app.lookup_app(env)
 
181
        self.assertOkJsonResponse(filediff_ui, env)
 
182
 
 
183
 
 
184
class TestRevLogUI(BasicTests):
 
185
 
 
186
    def make_branch_app_for_revlog_ui(self):
 
187
        builder = self.make_branch_builder('branch')
 
188
        builder.start_series()
 
189
        builder.build_snapshot('rev-id', None, [
 
190
            ('add', ('', 'root-id', 'directory', '')),
 
191
            ('add', ('filename', 'f-id', 'file', 'content\n'))],
 
192
            message="First commit.")
 
193
        builder.finish_series()
 
194
        branch = builder.get_branch()
 
195
        self.addCleanup(branch.lock_read().unlock)
 
196
        return self.make_branch_app(branch)
 
197
 
 
198
    def test_get_values_smoke(self):
 
199
        branch_app = self.make_branch_app_for_revlog_ui()
 
200
        env = {'SCRIPT_NAME': '/',
 
201
               'PATH_INFO': '/+revlog/rev-id'}
 
202
        revlog_ui = branch_app.lookup_app(env)
 
203
        revlog_ui.parse_args(env)
 
204
        values = revlog_ui.get_values('', {}, {})
 
205
        self.assertEqual(values['file_changes'].added[1].filename, 'filename')
 
206
        self.assertEqual(values['entry'].comment, "First commit.")
 
207
 
 
208
    def test_json_render_smoke(self):
 
209
        branch_app = self.make_branch_app_for_revlog_ui()
 
210
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/+revlog/rev-id'}
 
211
        revlog_ui = branch_app.lookup_app(env)
 
212
        self.assertOkJsonResponse(revlog_ui, env)
 
213