~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_controllers.py

  • Committer: Matt Nordhoff
  • Date: 2009-05-13 14:30:32 UTC
  • Revision ID: mnordhoff@mattnordhoff.com-20090513143032-wv2sb6nxh4uedyff
NEWS for bug #375948

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from cStringIO import StringIO
2
 
import logging
3
 
 
4
 
from paste.httpexceptions import HTTPServerError
5
 
 
6
 
from bzrlib import errors
7
 
 
8
1
from loggerhead.apps.branch import BranchWSGIApp
9
2
from loggerhead.controllers.annotate_ui import AnnotateUI
10
3
from loggerhead.controllers.inventory_ui import InventoryUI
12
5
from loggerhead.tests.test_simple import BasicTests
13
6
from loggerhead import util
14
7
 
15
 
from os import tmpnam
16
 
from tarfile import is_tarfile
17
 
 
18
8
 
19
9
class TestInventoryUI(BasicTests):
20
10
 
25
15
        tree.commit('')
26
16
        tree.branch.lock_read()
27
17
        self.addCleanup(tree.branch.unlock)
28
 
        branch_app = BranchWSGIApp(tree.branch, '')
29
 
        branch_app.log.setLevel(logging.CRITICAL)
30
 
        # These are usually set in BranchWSGIApp.app(), which is set from env
31
 
        # settings set by BranchesFromTransportRoot, so we fake it.
32
 
        branch_app._static_url_base = '/'
33
 
        branch_app._url_base = '/'
 
18
        branch_app = BranchWSGIApp(tree.branch)
34
19
        return tree.branch, InventoryUI(branch_app, branch_app.get_history)
35
20
 
36
 
    def consume_app(self, app, extra_environ=None):
37
 
        env = {'SCRIPT_NAME': '/files', 'PATH_INFO': ''}
38
 
        if extra_environ is not None:
39
 
            env.update(extra_environ)
40
 
        body = StringIO()
41
 
        start = []
42
 
        def start_response(status, headers, exc_info=None):
43
 
            start.append((status, headers, exc_info))
44
 
            return body.write
45
 
        extra_content = list(app(env, start_response))
46
 
        body.writelines(extra_content)
47
 
        return start[0], body.getvalue()
48
 
 
49
21
    def test_get_filelist(self):
50
22
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
51
23
            ['filename'])
52
24
        inv = bzrbranch.repository.get_inventory(bzrbranch.last_revision())
53
25
        self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename')))
54
26
 
55
 
    def test_smoke(self):
56
 
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
57
 
            ['filename'])
58
 
        start, content = self.consume_app(inv_ui)
59
 
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
60
 
                         start)
61
 
        self.assertContainsRe(content, 'filename')
62
 
 
63
 
    def test_no_content_for_HEAD(self):
64
 
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
65
 
            ['filename'])
66
 
        start, content = self.consume_app(inv_ui,
67
 
                            extra_environ={'REQUEST_METHOD': 'HEAD'})
68
 
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
69
 
                         start)
70
 
        self.assertEqual('', content)
71
 
 
72
27
 
73
28
class TestRevisionUI(BasicTests):
74
29
 
106
61
 
107
62
    def make_annotate_ui_for_file_history(self, file_id, rev_ids_texts):
108
63
        tree = self.make_branch_and_tree('.')
109
 
        self.build_tree_contents([('filename', '')])
 
64
        open('filename', 'w').write('')
110
65
        tree.add(['filename'], [file_id])
111
66
        for rev_id, text in rev_ids_texts:
112
 
            self.build_tree_contents([('filename', text)])
 
67
            open('filename', 'w').write(text)
113
68
            tree.commit(rev_id=rev_id, message='.')
114
69
        tree.branch.lock_read()
115
70
        self.addCleanup(tree.branch.unlock)
116
 
        branch_app = BranchWSGIApp(tree.branch, friendly_name='test_name')
 
71
        branch_app = BranchWSGIApp(tree.branch)
117
72
        return AnnotateUI(branch_app, branch_app.get_history)
118
73
 
119
74
    def test_annotate_file(self):
120
75
        history = [('rev1', 'old\nold\n'), ('rev2', 'new\nold\n')]
121
76
        ann_ui = self.make_annotate_ui_for_file_history('file_id', history)
122
 
        # A lot of this state is set up by __call__, but we'll do it directly
123
 
        # here.
124
 
        ann_ui.args = ['rev2']
125
 
        annotate_info = ann_ui.get_values('filename',
126
 
            kwargs={'file_id': 'file_id'}, headers={})
127
 
        annotated = list(annotate_info['annotated'])
 
77
        annotated = list(ann_ui.annotate_file('file_id', 'rev2'))
128
78
        self.assertEqual(2, len(annotated))
129
79
        self.assertEqual('2', annotated[0].change.revno)
130
80
        self.assertEqual('1', annotated[1].change.revno)
131
 
 
132
 
class TestDownloadTarballUI(BasicTests):
133
 
    
134
 
    def test_download_tarball(self):
135
 
        app = self.setUpLoggerhead()
136
 
        res = app.get('/tarball')
137
 
        tmpname = tmpnam()
138
 
        f = open(tmpname, 'w')
139
 
        f.write(res)
140
 
        f.close()
141
 
        self.failIf(not is_tarfile(tmpname))