summaryrefslogtreecommitdiff
path: root/tests/test_config.py
blob: 882f82c096eb5d481fcdc6b13f9cd4a7d444b07b (plain)
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
# (c) 2007 Philip Jenvey; written for Paste (http://pythonpaste.org)
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
from nose.tools import assert_raises
from paste.config import CONFIG, ConfigMiddleware
from paste.fixture import TestApp

test_key = 'test key'

def app_with_config(environ, start_response):
    start_response('200 OK', [('Content-type','text/plain')])
    return ['Variable is: %s\n' % CONFIG[test_key],
            'Variable is (in environ): %s' % environ['paste.config'][test_key]]

class NestingAppWithConfig(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        response = self.app(environ, start_response)
        assert isinstance(response, list)
        supplement = ['Nesting variable is: %s' % CONFIG[test_key],
                      'Nesting variable is (in environ): %s' % \
                          environ['paste.config'][test_key]]
        response.extend(supplement)
        return response

def test_request_config():
    config = {test_key: 'test value'}
    app = ConfigMiddleware(app_with_config, config)
    res = TestApp(app).get('/')
    assert 'Variable is: test value' in res
    assert 'Variable is (in environ): test value' in res

def test_request_config_multi():
    config = {test_key: 'test value'}
    app = ConfigMiddleware(app_with_config, config)
    config = {test_key: 'nesting value'}
    app = ConfigMiddleware(NestingAppWithConfig(app), config)
    res = TestApp(app).get('/')
    assert 'Variable is: test value' in res
    assert 'Variable is (in environ): test value' in res
    assert 'Nesting variable is: nesting value' in res
    print res
    assert 'Nesting variable is (in environ): nesting value' in res

def test_process_config(request_app=test_request_config):
    process_config = {test_key: 'bar', 'process_var': 'foo'}
    CONFIG.push_process_config(process_config)

    assert CONFIG[test_key] == 'bar'
    assert CONFIG['process_var'] == 'foo'

    request_app()

    assert CONFIG[test_key] == 'bar'
    assert CONFIG['process_var'] == 'foo'
    CONFIG.pop_process_config()
    
    assert_raises(AttributeError, lambda: 'process_var' not in CONFIG)
    assert_raises(IndexError, CONFIG.pop_process_config)

def test_process_config_multi():
    test_process_config(test_request_config_multi)