55
66import unittest
77from mock import patch
8+ import os
9+ from bases import ClearEnvTest
810
11+ from stackify import API_URL
912from stackify .application import get_configuration
1013
1114
12- class TestConfig (unittest .TestCase ):
15+ class TestConfig (ClearEnvTest ):
16+ '''
17+ Test automatic configuration for the ApiConfiguration
18+ '''
19+
20+ def test_required_kwargs (self ):
21+ '''API configuration requires appname, env and key'''
22+ env_map = {}
23+
24+ with patch .dict ('os.environ' , env_map ):
25+ with self .assertRaises (NameError ):
26+ get_configuration ()
27+ with self .assertRaises (NameError ):
28+ get_configuration (application = '1' )
29+ with self .assertRaises (NameError ):
30+ get_configuration (application = '1' , environment = '2' )
31+ with self .assertRaises (NameError ):
32+ get_configuration (application = '1' , environment = '2' , api_url = '3' )
33+
34+ get_configuration (application = '1' , environment = '2' , api_key = '3' )
35+
1336 def test_environment_config (self ):
37+ '''API configuration can load from env vars'''
1438 env_map = {
15- 'STACKIFY_APPLICATION' : 'test_appname ' ,
16- 'STACKIFY_ENVIRONMENT' : 'test_environment ' ,
17- 'STACKIFY_API_KEY' : 'test_apikey ' ,
18- 'STACKIFY_API_URL' : 'test_apiurl ' ,
39+ 'STACKIFY_APPLICATION' : 'test1_appname ' ,
40+ 'STACKIFY_ENVIRONMENT' : 'test1_environment ' ,
41+ 'STACKIFY_API_KEY' : 'test1_apikey ' ,
42+ 'STACKIFY_API_URL' : 'test1_apiurl ' ,
1943 }
2044
2145 with patch .dict ('os.environ' , env_map ):
2246 config = get_configuration ()
2347
24- self .assertEqual (config .application , 'test_appname ' )
25- self .assertEqual (config .environment , 'test_environment ' )
26- self .assertEqual (config .api_key , 'test_apikey ' )
27- self .assertEqual (config .api_url , 'test_apiurl ' )
48+ self .assertEqual (config .application , 'test1_appname ' )
49+ self .assertEqual (config .environment , 'test1_environment ' )
50+ self .assertEqual (config .api_key , 'test1_apikey ' )
51+ self .assertEqual (config .api_url , 'test1_apiurl ' )
2852
2953 def test_kwarg_mix (self ):
54+ '''API configuration can load from a mix of env vars and kwargs'''
3055 env_map = {
3156 'STACKIFY_APPLICATION' : 'test2_appname' ,
3257 'STACKIFY_ENVIRONMENT' : 'test2_environment' ,
@@ -41,6 +66,7 @@ def test_kwarg_mix(self):
4166 self .assertEqual (config .api_url , 'test2_apiurl' )
4267
4368 def test_kwargs (self ):
69+ '''API configuration can load from kwargs'''
4470 config = get_configuration (
4571 application = 'test3_appname' ,
4672 environment = 'test3_environment' ,
@@ -52,6 +78,18 @@ def test_kwargs(self):
5278 self .assertEqual (config .api_key , 'test3_apikey' )
5379 self .assertEqual (config .api_url , 'test3_apiurl' )
5480
81+ def test_api_url_default (self ):
82+ '''API URL is set automatically'''
83+ config = get_configuration (
84+ application = 'test4_appname' ,
85+ environment = 'test4_environment' ,
86+ api_key = 'test4_apikey' )
87+
88+ self .assertEqual (config .application , 'test4_appname' )
89+ self .assertEqual (config .environment , 'test4_environment' )
90+ self .assertEqual (config .api_key , 'test4_apikey' )
91+ self .assertEqual (config .api_url , API_URL )
92+
5593
5694if __name__ == '__main__' :
5795 unittest .main ()
0 commit comments