~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_controllers.py

  • Committer: Matt Nordhoff
  • Date: 2010-04-24 12:40:17 UTC
  • Revision ID: mnordhoff@mattnordhoff.com-20100424124017-do5wupmzlkmejtvk
Don't let properties that start with _ be created in the first place.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from cStringIO import StringIO
2
 
import logging
3
 
import tarfile
4
 
 
5
 
from paste.httpexceptions import HTTPServerError
6
 
 
7
 
from bzrlib import errors
8
 
import simplejson
9
 
 
10
1
from loggerhead.apps.branch import BranchWSGIApp
11
2
from loggerhead.controllers.annotate_ui import AnnotateUI
12
3
from loggerhead.controllers.inventory_ui import InventoryUI
13
4
from loggerhead.controllers.revision_ui import RevisionUI
14
 
from loggerhead.tests.test_simple import BasicTests, consume_app
 
5
from loggerhead.tests.test_simple import BasicTests
15
6
from loggerhead import util
16
7
 
17
8
 
18
9
class TestInventoryUI(BasicTests):
19
10
 
20
 
    def make_bzrbranch_for_tree_shape(self, shape):
 
11
    def make_bzrbranch_and_inventory_ui_for_tree_shape(self, shape):
21
12
        tree = self.make_branch_and_tree('.')
22
13
        self.build_tree(shape)
23
14
        tree.smart_add([])
24
15
        tree.commit('')
25
 
        self.addCleanup(tree.branch.lock_read().unlock)
26
 
        return tree.branch
27
 
 
28
 
    def make_bzrbranch_and_inventory_ui_for_tree_shape(self, shape):
29
 
        branch = self.make_bzrbranch_for_tree_shape(shape)
30
 
        branch_app = self.make_branch_app(branch)
31
 
        return branch, InventoryUI(branch_app, branch_app.get_history)
 
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)
32
20
 
33
21
    def test_get_filelist(self):
34
22
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
35
23
            ['filename'])
36
24
        inv = bzrbranch.repository.get_inventory(bzrbranch.last_revision())
37
 
        self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename', 'head')))
38
 
 
39
 
    def test_smoke(self):
40
 
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
41
 
            ['filename'])
42
 
        start, content = consume_app(inv_ui,
43
 
            {'SCRIPT_NAME': '/files', 'PATH_INFO': ''})
44
 
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
45
 
                         start)
46
 
        self.assertContainsRe(content, 'filename')
47
 
 
48
 
    def test_no_content_for_HEAD(self):
49
 
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
50
 
            ['filename'])
51
 
        start, content = consume_app(inv_ui,
52
 
            {'SCRIPT_NAME': '/files', 'PATH_INFO': '',
53
 
             'REQUEST_METHOD': 'HEAD'})
54
 
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
55
 
                         start)
56
 
        self.assertEqual('', content)
57
 
 
58
 
    def test_get_values_smoke(self):
59
 
        branch = self.make_bzrbranch_for_tree_shape(['a-file'])
60
 
        branch_app = self.make_branch_app(branch)
61
 
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/files'}
62
 
        inv_ui = branch_app.lookup_app(env)
63
 
        inv_ui.parse_args(env)
64
 
        values = inv_ui.get_values('', {}, {})
65
 
        self.assertEqual('a-file', values['filelist'][0].filename)
66
 
 
67
 
    def test_json_render_smoke(self):
68
 
        branch = self.make_bzrbranch_for_tree_shape(['a-file'])
69
 
        branch_app = self.make_branch_app(branch)
70
 
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/files'}
71
 
        inv_ui = branch_app.lookup_app(env)
72
 
        self.assertOkJsonResponse(inv_ui, env)
 
25
        self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename')))
73
26
 
74
27
 
75
28
class TestRevisionUI(BasicTests):
76
29
 
77
 
    def make_branch_app_for_revision_ui(self, shape1, shape2):
 
30
    def make_bzrbranch_and_revision_ui_for_tree_shapes(self, shape1, shape2):
78
31
        tree = self.make_branch_and_tree('.')
79
32
        self.build_tree_contents(shape1)
80
33
        tree.smart_add([])
81
 
        tree.commit('msg 1', rev_id='rev-1')
 
34
        tree.commit('')
82
35
        self.build_tree_contents(shape2)
83
36
        tree.smart_add([])
84
 
        tree.commit('msg 2', rev_id='rev-2')
85
 
        branch = tree.branch
86
 
        self.addCleanup(branch.lock_read().unlock)
87
 
        return self.make_branch_app(branch)
 
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)
88
49
 
89
50
    def test_get_values(self):
90
 
        branch_app = self.make_branch_app_for_revision_ui([], [])
91
 
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/revision/2'}
92
 
        rev_ui = branch_app.lookup_app(env)
93
 
        rev_ui.parse_args(env)
94
 
        self.assertIsInstance(rev_ui.get_values('', {}, []), dict)
95
 
 
96
 
    def test_get_values_smoke(self):
97
 
        branch_app = self.make_branch_app_for_revision_ui(
98
 
                [('file', 'content\n'), ('other-file', 'other\n')],
99
 
                [('file', 'new content\n')])
100
 
        env = {'SCRIPT_NAME': '/',
101
 
               'PATH_INFO': '/revision/head:'}
102
 
        revision_ui = branch_app.lookup_app(env)
103
 
        revision_ui.parse_args(env)
104
 
        values = revision_ui.get_values('', {}, {})
105
 
 
106
 
        self.assertEqual(values['revid'], 'rev-2')
107
 
        self.assertEqual(values['change'].comment, 'msg 2')
108
 
        self.assertEqual(values['file_changes'].modified[0].filename, 'file')
109
 
        self.assertEqual(values['merged_in'], None)
110
 
 
111
 
    def test_json_render_smoke(self):
112
 
        branch_app = self.make_branch_app_for_revision_ui(
113
 
                [('file', 'content\n'), ('other-file', 'other\n')],
114
 
                [('file', 'new content\n')])
115
 
        env = {'SCRIPT_NAME': '', 'PATH_INFO': '/+json/revision/head:'}
116
 
        revision_ui = branch_app.lookup_app(env)
117
 
        self.assertOkJsonResponse(revision_ui, env)
118
 
 
 
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('', {}, []),
 
57
            dict)
119
58
 
120
59
 
121
60
class TestAnnotateUI(BasicTests):
122
61
 
123
62
    def make_annotate_ui_for_file_history(self, file_id, rev_ids_texts):
124
63
        tree = self.make_branch_and_tree('.')
125
 
        self.build_tree_contents([('filename', '')])
 
64
        open('filename', 'w').write('')
126
65
        tree.add(['filename'], [file_id])
127
66
        for rev_id, text in rev_ids_texts:
128
 
            self.build_tree_contents([('filename', text)])
 
67
            open('filename', 'w').write(text)
129
68
            tree.commit(rev_id=rev_id, message='.')
130
69
        tree.branch.lock_read()
131
70
        self.addCleanup(tree.branch.unlock)
132
 
        branch_app = BranchWSGIApp(tree.branch, friendly_name='test_name')
 
71
        branch_app = BranchWSGIApp(tree.branch)
133
72
        return AnnotateUI(branch_app, branch_app.get_history)
134
73
 
135
74
    def test_annotate_file(self):
136
75
        history = [('rev1', 'old\nold\n'), ('rev2', 'new\nold\n')]
137
76
        ann_ui = self.make_annotate_ui_for_file_history('file_id', history)
138
 
        # A lot of this state is set up by __call__, but we'll do it directly
139
 
        # here.
140
 
        ann_ui.args = ['rev2']
141
 
        annotate_info = ann_ui.get_values('filename',
142
 
            kwargs={'file_id': 'file_id'}, headers={})
143
 
        annotated = annotate_info['annotated']
 
77
        annotated = list(ann_ui.annotate_file('file_id', 'rev2'))
144
78
        self.assertEqual(2, len(annotated))
145
 
        self.assertEqual('2', annotated[1].change.revno)
146
 
        self.assertEqual('1', annotated[2].change.revno)
147
 
 
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
 
 
214
 
class TestDownloadTarballUI(BasicTests):
215
 
 
216
 
    def setUp(self):
217
 
        super(TestDownloadTarballUI, self).setUp()
218
 
        self.createBranch()
219
 
 
220
 
    def test_download_tarball(self):
221
 
        app = self.setUpLoggerhead()
222
 
        res = app.get('/tarball')
223
 
        f = open('tarball', 'w')
224
 
        f.write(res)
225
 
        f.close()
226
 
        self.failIf(not tarfile.is_tarfile('tarball'))
 
 
b'\\ No newline at end of file'
 
79
        self.assertEqual('2', annotated[0].change.revno)
 
80
        self.assertEqual('1', annotated[1].change.revno)