summaryrefslogtreecommitdiff
path: root/tests/test_config.py
blob: 33eb8085546e058752aedb544b9a45e4d20494ba (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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import sys

import fakeapp.apps
import fakeapp.configapps as fc

from paste.deploy import appconfig, loadapp

ini_file = 'config:sample_configs/test_config.ini'
here = os.path.dirname(__file__)
config_path = os.path.join(here, 'sample_configs')
config_filename = os.path.join(config_path, 'test_config.ini')


def test_config_egg():
    app = loadapp('egg:FakeApp#configed')
    assert isinstance(app, fc.SimpleApp)


def test_config1():
    app = loadapp(ini_file, relative_to=here, name='test1')
    assert app.local_conf == {
        'setting1': 'foo',
        'setting2': 'bar',
        'apppath': os.path.join(config_path, 'app'),
    }
    assert app.global_conf == {
        'def1': 'a',
        'def2': 'b',
        'basepath': config_path,
        'here': config_path,
        '__file__': config_filename,
    }


def test_config2():
    app = loadapp(ini_file, relative_to=here, name='test2')
    assert app.local_conf == {'local conf': 'something'}
    assert app.global_conf == {
        'def1': 'test2',
        'def2': 'b',
        'basepath': config_path,
        'another': 'TEST',
        'here': config_path,
        '__file__': config_filename,
    }
    # Run this to make sure the global-conf-modified test2
    # didn't mess up the general global conf
    test_config1()


def test_config3():
    app = loadapp(ini_file, relative_to=here, name='test3')
    assert isinstance(app, fc.SimpleApp)
    assert app.local_conf == {
        'local conf': 'something',
        'another': 'something more\nacross several\nlines',
    }
    assert app.global_conf == {
        'def1': 'test3',
        'def2': 'b',
        'basepath': config_path,
        'another': 'TEST',
        'here': config_path,
        '__file__': config_filename,
    }
    test_config2()


def test_main():
    app = loadapp('config:test_func.ini', relative_to=config_path)
    assert app is fakeapp.apps.basic_app
    app = loadapp('config:test_func.ini#main', relative_to=config_path)
    assert app is fakeapp.apps.basic_app
    app = loadapp('config:test_func.ini', relative_to=config_path, name='main')
    assert app is fakeapp.apps.basic_app
    app = loadapp('config:test_func.ini#ignored', relative_to=config_path, name='main')
    assert app is fakeapp.apps.basic_app


def test_other():
    app = loadapp('config:test_func.ini#other', relative_to=config_path)
    assert app is fakeapp.apps.basic_app2


def test_composit():
    app = loadapp('config:test_func.ini#remote_addr', relative_to=config_path)
    assert isinstance(app, fakeapp.apps.RemoteAddrDispatch)
    assert app.map['127.0.0.1'] is fakeapp.apps.basic_app
    assert app.map['0.0.0.0'] is fakeapp.apps.basic_app2


def test_foreign_config():
    app = loadapp(ini_file, relative_to=here, name='test_foreign_config')
    assert isinstance(app, fc.SimpleApp)
    assert app.local_conf == {'another': 'FOO', 'bob': 'your uncle'}
    assert app.global_conf == {
        'def1': 'a',
        # Note overwrite of DEFAULT value from foreign config
        'def2': 'b',
        'def3': 'c',
        'basepath': config_path,
        'glob': 'override',
        'here': config_path,
        '__file__': os.path.join(config_path, 'test_config.ini'),
    }


def test_config_get():
    app = loadapp(ini_file, relative_to=here, name='test_get')
    assert isinstance(app, fc.SimpleApp)
    assert app.local_conf == {'def1': 'a', 'foo': 'TEST'}
    assert app.global_conf == {
        'def1': 'a',
        'def2': 'TEST',
        'basepath': os.path.join(here, 'sample_configs'),
        'here': config_path,
        '__file__': config_filename,
    }


def test_appconfig():
    conf = appconfig(ini_file, relative_to=here, name='test_get')
    assert conf == {
        'def1': 'a',
        'def2': 'TEST',
        'basepath': os.path.join(here, 'sample_configs'),
        'here': config_path,
        '__file__': config_filename,
        'foo': 'TEST',
    }
    assert conf.local_conf == {'def1': 'a', 'foo': 'TEST'}
    assert conf.global_conf == {
        'def1': 'a',
        'def2': 'TEST',
        'basepath': os.path.join(here, 'sample_configs'),
        'here': config_path,
        '__file__': config_filename,
    }


def test_appconfig_filter_with():
    conf = appconfig('config:test_filter_with.ini', relative_to=config_path)
    assert conf['example'] == 'test'


def test_global_conf():
    conf = appconfig(
        ini_file,
        relative_to=here,
        name='test_global_conf',
        global_conf={'def2': 'TEST DEF 2', 'inherit': 'bazbar'},
    )
    assert conf == {
        'def1': 'a',
        # Note overwrite of DEFAULT value
        'def2': 'TEST DEF 2',
        'basepath': os.path.join(here, 'sample_configs'),
        'here': config_path,
        'inherit': 'bazbar',
        '__file__': config_filename,
        'test_interp': 'this:bazbar',
    }
    assert conf.local_conf == {'test_interp': 'this:bazbar'}


def test_interpolate_exception():
    try:
        appconfig('config:test_error.ini', relative_to=config_path)
    except Exception:
        e = sys.exc_info()[1]
        expected = "Error in file %s" % os.path.join(config_path, 'test_error.ini')
        assert str(e).split(':')[0] == expected
    else:
        raise AssertionError('Should have raised an exception')