~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_controllers.py

  • Committer: John Arbash Meinel
  • Date: 2011-06-15 14:47:11 UTC
  • mfrom: (447.1.3 loggerhead)
  • Revision ID: john@arbash-meinel.com-20110615144711-wv0wgim27u1ismhi
Merge css updates for annotate from Jasper St. Pierre.

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
from loggerhead.apps.branch import BranchWSGIApp
 
9
from loggerhead.controllers.annotate_ui import AnnotateUI
 
10
from loggerhead.controllers.inventory_ui import InventoryUI
 
11
from loggerhead.controllers.revision_ui import RevisionUI
 
12
from loggerhead.tests.test_simple import BasicTests
 
13
from loggerhead import util
 
14
 
 
15
 
 
16
class TestInventoryUI(BasicTests):
 
17
 
 
18
    def make_bzrbranch_and_inventory_ui_for_tree_shape(self, shape):
 
19
        tree = self.make_branch_and_tree('.')
 
20
        self.build_tree(shape)
 
21
        tree.smart_add([])
 
22
        tree.commit('')
 
23
        tree.branch.lock_read()
 
24
        self.addCleanup(tree.branch.unlock)
 
25
        branch_app = BranchWSGIApp(tree.branch, '')
 
26
        branch_app.log.setLevel(logging.CRITICAL)
 
27
        # These are usually set in BranchWSGIApp.app(), which is set from env
 
28
        # settings set by BranchesFromTransportRoot, so we fake it.
 
29
        branch_app._static_url_base = '/'
 
30
        branch_app._url_base = '/'
 
31
        return tree.branch, InventoryUI(branch_app, branch_app.get_history)
 
32
 
 
33
    def consume_app(self, app, extra_environ=None):
 
34
        env = {'SCRIPT_NAME': '/files', 'PATH_INFO': ''}
 
35
        if extra_environ is not None:
 
36
            env.update(extra_environ)
 
37
        body = StringIO()
 
38
        start = []
 
39
        def start_response(status, headers, exc_info=None):
 
40
            start.append((status, headers, exc_info))
 
41
            return body.write
 
42
        extra_content = list(app(env, start_response))
 
43
        body.writelines(extra_content)
 
44
        return start[0], body.getvalue()
 
45
 
 
46
    def test_get_filelist(self):
 
47
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
 
48
            ['filename'])
 
49
        inv = bzrbranch.repository.get_inventory(bzrbranch.last_revision())
 
50
        self.assertEqual(1, len(inv_ui.get_filelist(inv, '', 'filename', 'head')))
 
51
 
 
52
    def test_smoke(self):
 
53
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
 
54
            ['filename'])
 
55
        start, content = self.consume_app(inv_ui)
 
56
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
 
57
                         start)
 
58
        self.assertContainsRe(content, 'filename')
 
59
 
 
60
    def test_no_content_for_HEAD(self):
 
61
        bzrbranch, inv_ui = self.make_bzrbranch_and_inventory_ui_for_tree_shape(
 
62
            ['filename'])
 
63
        start, content = self.consume_app(inv_ui,
 
64
                            extra_environ={'REQUEST_METHOD': 'HEAD'})
 
65
        self.assertEqual(('200 OK', [('Content-Type', 'text/html')], None),
 
66
                         start)
 
67
        self.assertEqual('', content)
 
68
 
 
69
 
 
70
class TestRevisionUI(BasicTests):
 
71
 
 
72
    def make_bzrbranch_and_revision_ui_for_tree_shapes(self, shape1, shape2):
 
73
        tree = self.make_branch_and_tree('.')
 
74
        self.build_tree_contents(shape1)
 
75
        tree.smart_add([])
 
76
        tree.commit('')
 
77
        self.build_tree_contents(shape2)
 
78
        tree.smart_add([])
 
79
        tree.commit('')
 
80
        tree.branch.lock_read()
 
81
        self.addCleanup(tree.branch.unlock)
 
82
        branch_app = BranchWSGIApp(tree.branch)
 
83
        branch_app._environ = {
 
84
            'wsgi.url_scheme':'',
 
85
            'SERVER_NAME':'',
 
86
            'SERVER_PORT':'80',
 
87
            }
 
88
        branch_app._url_base = ''
 
89
        branch_app.friendly_name = ''
 
90
        return tree.branch, RevisionUI(branch_app, branch_app.get_history)
 
91
 
 
92
    def test_get_values(self):
 
93
        branch, rev_ui = self.make_bzrbranch_and_revision_ui_for_tree_shapes(
 
94
            [], [])
 
95
        rev_ui.args = ['2']
 
96
        util.set_context({})
 
97
        self.assertIsInstance(
 
98
            rev_ui.get_values('', {}, []),
 
99
            dict)
 
100
 
 
101
 
 
102
class TestAnnotateUI(BasicTests):
 
103
 
 
104
    def make_annotate_ui_for_file_history(self, file_id, rev_ids_texts):
 
105
        tree = self.make_branch_and_tree('.')
 
106
        self.build_tree_contents([('filename', '')])
 
107
        tree.add(['filename'], [file_id])
 
108
        for rev_id, text in rev_ids_texts:
 
109
            self.build_tree_contents([('filename', text)])
 
110
            tree.commit(rev_id=rev_id, message='.')
 
111
        tree.branch.lock_read()
 
112
        self.addCleanup(tree.branch.unlock)
 
113
        branch_app = BranchWSGIApp(tree.branch, friendly_name='test_name')
 
114
        return AnnotateUI(branch_app, branch_app.get_history)
 
115
 
 
116
    def test_annotate_file(self):
 
117
        history = [('rev1', 'old\nold\n'), ('rev2', 'new\nold\n')]
 
118
        ann_ui = self.make_annotate_ui_for_file_history('file_id', history)
 
119
        # A lot of this state is set up by __call__, but we'll do it directly
 
120
        # here.
 
121
        ann_ui.args = ['rev2']
 
122
        annotate_info = ann_ui.get_values('filename',
 
123
            kwargs={'file_id': 'file_id'}, headers={})
 
124
        annotated = list(annotate_info['annotated'])
 
125
        self.assertEqual(2, len(annotated))
 
126
        self.assertEqual('2', annotated[0].change.revno)
 
127
        self.assertEqual('1', annotated[1].change.revno)