Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 39b4c32

Browse files
author
Mangled Deutz
committed
Add tests for new config
Docker-DCO-1.1-Signed-off-by: Mangled Deutz <[email protected]> (github: dmp42)
1 parent 8db0c74 commit 39b4c32

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

tests/fixtures/test_config.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
booltrue: True
2+
boolfalse: False
3+
uint: 10
4+
int: -10
5+
float: 0.01
6+
emptystring: ''
7+
isnone:
8+
nonemptystring: nonemptystring
9+
anothernonemptystring: 'nonemptystring'
10+
yetanothernonemptystring: 'nonemptystring'
11+
array: [one, two, three]
12+
dict:
13+
one: valueone
14+
two: valuetwo
15+
three: valuethree
16+
something: 'else'
17+
uni: "ß∞"
18+
19+
20+
ENV:
21+
booltrue: _env:BOOLTRUE:True
22+
boolfalse: _env:BOOLFALSE:False
23+
uint: _env:UINT:10
24+
int: _env:INT:-10
25+
float: _env:FLOAT:0.01
26+
emptystring: _env:EMPTYSTRING:''
27+
isnone: _env:ISNONE
28+
nonemptystring: _env:NONEMPTYSTRING:nonemptystring
29+
anothernonemptystring: _env:ANOTHERNONEMPTYSTRING:'nonemptystring'
30+
yetanothernonemptystring: _env:YETANOTHERNONEMPTYSTRING:'nonemptystring'
31+
bugger: _env:BUGGER:bug:me:endlessly
32+
array: _env:ARRAY:[one, two, three]

tests/test_config.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# from nose import tools
4+
5+
# from docker_registry.core import exceptions
6+
# import docker_registry.testing as testing
7+
8+
import mock
9+
import os
10+
11+
from docker_registry.lib import config
12+
13+
14+
fakeenv = {}
15+
16+
17+
def mockget(key, opt=None):
18+
if key in fakeenv:
19+
print('%s key is %s' % (key, fakeenv[key]))
20+
return fakeenv[key]
21+
return opt
22+
23+
24+
@mock.patch('os.environ.get', mockget)
25+
class TestConfig():
26+
27+
def __init__(self):
28+
p = os.path.join(
29+
os.path.dirname(__file__), 'fixtures', 'test_config.yaml')
30+
31+
self.c = config.Config(open(p, 'rb').read())
32+
33+
def test_accessors(self):
34+
assert self.c.booltrue == self.c['booltrue']
35+
assert self.c.dict.one == self.c.dict['one']
36+
assert self.c.dict.one == self.c['dict']['one']
37+
38+
def test_key_existence(self):
39+
assert 'boolfalse' in self.c
40+
assert 'whatevertheflush' not in self.c
41+
42+
def test_non_existent_access(self):
43+
assert self.c['whatevertheflush'] is None
44+
assert self.c.whatevertheflush is None
45+
46+
def test_simple_types(self):
47+
conf = self.c
48+
assert conf.booltrue is True
49+
assert not conf.booltrue == 'True'
50+
assert conf.boolfalse is False
51+
assert not conf.booltrue == 'False'
52+
assert conf.uint == 10
53+
assert not conf.uint == '10'
54+
assert conf.int == -10
55+
assert not conf.int == '-10'
56+
assert conf.float == 0.01
57+
assert not conf.float == '0.01'
58+
assert conf.emptystring == ''
59+
assert conf.emptystring is not None
60+
assert conf.isnone is None
61+
assert conf.nonemptystring == 'nonemptystring'
62+
assert conf.anothernonemptystring == 'nonemptystring'
63+
assert conf.yetanothernonemptystring == 'nonemptystring'
64+
assert conf.array[2] == 'three'
65+
assert len(conf.array) == 3
66+
assert conf.dict.two == 'valuetwo'
67+
68+
def test_env_defaults(self):
69+
global fakeenv
70+
fakeenv = {}
71+
72+
conf = self.c.ENV
73+
assert conf.booltrue is True
74+
assert conf.boolfalse is False
75+
assert conf.uint == 10
76+
assert conf.int == -10
77+
assert conf.float == 0.01
78+
assert conf.emptystring == ''
79+
assert conf.emptystring is not None
80+
assert conf.isnone is None
81+
assert conf.nonemptystring == 'nonemptystring'
82+
assert conf.anothernonemptystring == 'nonemptystring'
83+
assert conf.yetanothernonemptystring == 'nonemptystring'
84+
assert conf.bugger == 'bug:me:endlessly'
85+
assert conf.array[2] == 'three'
86+
assert len(conf.array) == 3
87+
88+
def test_env_overrides(self):
89+
global fakeenv
90+
fakeenv['BOOLTRUE'] = 'False'
91+
fakeenv['BOOLFALSE'] = 'True'
92+
fakeenv['UINT'] = '0'
93+
fakeenv['INT'] = '0'
94+
fakeenv['FLOAT'] = '0'
95+
fakeenv['EMPTYSTRING'] = 'NOTREALLY'
96+
fakeenv['ISNONE'] = 'False'
97+
fakeenv['NONEMPTYSTRING'] = '""'
98+
fakeenv['BUGGER'] = '"whatever:the:flush:"'
99+
fakeenv['ARRAY'] = '[one, again]'
100+
101+
conf = self.c.ENV
102+
assert conf.booltrue is False
103+
assert conf.boolfalse is True
104+
assert conf.uint == 0
105+
assert conf.int == 0
106+
assert conf.float == 0
107+
assert conf.emptystring == 'NOTREALLY'
108+
assert conf.isnone is False
109+
assert conf.isnone is not None
110+
assert conf.nonemptystring == ''
111+
assert conf.anothernonemptystring == 'nonemptystring'
112+
assert conf.yetanothernonemptystring == 'nonemptystring'
113+
assert conf.bugger == 'whatever:the:flush:'
114+
assert conf.array[1] == 'again'
115+
assert len(conf.array) == 2
116+
117+
fakeenv['ISNONE'] = ''
118+
assert conf.isnone is None
119+
120+
def test_write(self):
121+
conf = self.c
122+
assert conf.something == 'else'
123+
conf.something = 'or'
124+
assert conf.something == 'or'
125+
conf.something = None
126+
assert conf.something is None
127+
128+
def test_unicode(self):
129+
assert self.c.uni == u'ß∞'

0 commit comments

Comments
 (0)