Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.x/cherrypy/test/test_config.py

Revision 1851 (checked in by fumanchu, 11 months ago)

test_config fix.

  • Property svn:eol-style set to native
Line 
1 """Tests for the CherryPy configuration system."""
2 import test
3 test.prefer_parent_path()
4
5 import StringIO
6 import cherrypy
7
8
9 def setup_server():
10    
11     class Root:
12         def index(self, key):
13             return cherrypy.config.get(key, "None")
14         index.exposed = True
15         global_ = index
16         xyz = index
17    
18     class Foo:
19         def index(self, key):
20             return cherrypy.config.get(key, "None")
21         index.exposed = True
22         bar = index
23         nex = index
24        
25         def getall(self, key):
26             return repr(cherrypy.config.getAll(key))
27         getall.exposed = True
28    
29     class Env:
30         def index(self, key):
31             return str(cherrypy.config.get(key, "None"))
32         index.exposed = True
33         prod = index
34         embed = index
35        
36         def wrong(self):
37             conf = "\n[global]\nserver.environment = production\n"
38             cherrypy.config.update(file=StringIO.StringIO(conf))
39         wrong.exposed=True
40    
41     cherrypy.tree.mount(Root())
42     cherrypy.root.foo = Foo()
43    
44     cherrypy.config.update({
45         'global': {'server.log_to_screen': False,
46                    'server.environment': 'production',
47                    'server.show_tracebacks': True,
48                    },
49         '/': {
50             'foo': 'this',
51             'bar': 'that',
52             },
53         '/foo': {
54             'foo': 'this2',
55             'baz': 'that2',
56             },
57         '/foo/bar': {
58             'foo': 'this3',
59             'bax': 'this4',
60             },
61     })
62
63     _env_conf = {'/': {'server.environment': 'development'},
64                  '/prod': {'server.environment': 'production'},
65                  '/embed': {'server.environment': 'embedded'},
66                  }
67     cherrypy.tree.mount(Env(), "/env", _env_conf)
68
69     # Shortcut syntax--should get put in the "global" bucket
70     cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove'})
71
72
73 #                             Client-side code                             #
74
75 import helper
76
77 class ConfigTests(helper.CPWebCase):
78    
79     def testConfig(self):
80         tests = [
81             ('/',        'nex', 'None'),
82             ('/',        'foo', 'this'),
83             ('/',        'bar', 'that'),
84             ('/xyz',     'foo', 'this'),
85             ('/foo/',    'foo', 'this2'),
86             ('/foo/',    'bar', 'that'),
87             ('/foo/',    'bax', 'None'),
88             ('/foo/bar', 'baz', 'that2'),
89             ('/foo/nex', 'baz', 'that2'),
90             # If 'foo' == 'this', then the mount point '/env' leaks into '/'.
91             ('/env/prod','foo', 'None'),
92         ]
93         for path, key, expected in tests:
94             self.getPage(path + "?key=" + key)
95             self.assertBody(expected)
96        
97         self.getPage("/foo/getall?key=foo")
98         self.assertBody("[('/', 'this'), ('/foo', 'this2')]")
99    
100     def testUnrepr(self):
101         err = ('WrongConfigValue: ("section: '
102                "'global', option: 'server.environment', value: 'production'"
103                '''", 'UnknownType', ('production',))''')
104         self.getPage("/env/wrong")
105         self.assertErrorPage(500, pattern=err)
106    
107     def testEnvironments(self):
108         for key, val in cherrypy.config.environments['development'].iteritems():
109             self.getPage("/env/?key=" + key)
110             # The dev environment will have logdebuginfo data
111             data = self.body.split("\n")[0]
112             self.assertEqual(data, str(val))
113         for key, val in cherrypy.config.environments['production'].iteritems():
114             self.getPage("/env/prod/?key=" + key)
115             self.assertBody(str(val))
116         for key, val in cherrypy.config.environments['embedded'].iteritems():
117             self.getPage("/env/embed/?key=" + key)
118             data = self.body.split("\n")[0]
119             self.assertEqual(data, str(val))
120
121
122 if __name__ == '__main__':
123     setup_server()
124     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets