File: test_config.py

package info (click to toggle)
python-cherrypy 2.2.1-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 796 kB
  • ctags: 1,079
  • sloc: python: 7,869; makefile: 15
file content (118 lines) | stat: -rw-r--r-- 3,743 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Tests for the CherryPy configuration system."""
import test
test.prefer_parent_path()

import StringIO
import cherrypy


def setup_server():
    
    class Root:
        def index(self, key):
            return cherrypy.config.get(key, "None")
        index.exposed = True
        global_ = index
        xyz = index
    
    class Foo:
        def index(self, key):
            return cherrypy.config.get(key, "None")
        index.exposed = True
        bar = index
        nex = index
    
    class Env:
        def index(self, key):
            return str(cherrypy.config.get(key, "None"))
        index.exposed = True
        prod = index
        embed = index
        
        def wrong(self):
            conf = "\n[global]\nserver.environment = production\n"
            cherrypy.config.update(file=StringIO.StringIO(conf))
        wrong.exposed=True
    
    cherrypy.tree.mount(Root())
    cherrypy.root.foo = Foo()
    
    cherrypy.config.update({
        'global': {'server.log_to_screen': False,
                   'server.environment': 'production',
                   'server.show_tracebacks': True,
                   },
        '/': {
            'foo': 'this',
            'bar': 'that',
            },
        '/foo': {
            'foo': 'this2',
            'baz': 'that2',
            },
        '/foo/bar': {
            'foo': 'this3',
            'bax': 'this4',
            },
    })

    _env_conf = {'/': {'server.environment': 'development'},
                 '/prod': {'server.environment': 'production'},
                 '/embed': {'server.environment': 'embedded'},
                 }
    cherrypy.tree.mount(Env(), "/env", _env_conf)

    # Shortcut syntax--should get put in the "global" bucket
    cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove'})


#                             Client-side code                             #

import helper

class ConfigTests(helper.CPWebCase):
    
    def testConfig(self):
        tests = [
            ('*',        'luxuryyacht', 'throatwobblermangrove'),
            ('/',        'nex', 'None'),
            ('/',        'foo', 'this'),
            ('/',        'bar', 'that'),
            ('/xyz',     'foo', 'this'),
            ('/foo/',    'foo', 'this2'),
            ('/foo/',    'bar', 'that'),
            ('/foo/',    'bax', 'None'),
            ('/foo/bar', 'baz', 'that2'),
            ('/foo/nex', 'baz', 'that2'),
            # If 'foo' == 'this', then the mount point '/env' leaks into '/'.
            ('/env/prod','foo', 'None'),
        ]
        for path, key, expected in tests:
            self.getPage(path + "?key=" + key)
            self.assertBody(expected)
    
    def testUnrepr(self):
        err = ('WrongConfigValue: ("section: '
               "'global', option: 'server.environment', value: 'production'"
               '''", 'UnknownType', ('production',))''')
        self.getPage("/env/wrong")
        self.assertErrorPage(500, pattern=err)
    
    def testEnvironments(self):
        for key, val in cherrypy.config.environments['development'].iteritems():
            self.getPage("/env/?key=" + key)
            # The dev environment will have logdebuginfo data
            data = self.body.split("\n")[0]
            self.assertEqual(data, str(val))
        for key, val in cherrypy.config.environments['production'].iteritems():
            self.getPage("/env/prod/?key=" + key)
            self.assertBody(str(val))
        for key, val in cherrypy.config.environments['embedded'].iteritems():
            self.getPage("/env/embed/?key=" + key)
            data = self.body.split("\n")[0]
            self.assertEqual(data, str(val))


if __name__ == '__main__':
    setup_server()
    helper.testmain()