Skip to content

Commit bc631ba

Browse files
committed
More tests, fixing bugs exposed by tests
1 parent 77847b2 commit bc631ba

5 files changed

Lines changed: 182 additions & 13 deletions

File tree

stackify/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
__listener_cache = {}
4646

4747

48-
def getLogger(name=None, **kwargs):
48+
def getLogger(name=None, auto_shutdown=True, **kwargs):
4949
'''
5050
Get a logger and attach a StackifyHandler if needed.
5151
'''
@@ -59,7 +59,8 @@ def getLogger(name=None, **kwargs):
5959
handler = StackifyHandler(**kwargs)
6060
logger.addHandler(handler)
6161

62-
atexit.register(stopLogging, logger)
62+
if auto_shutdown:
63+
atexit.register(stopLogging, logger)
6364

6465
if logger.getEffectiveLevel() == logging.NOTSET:
6566
logger.setLevel(DEFAULT_LEVEL)

stackify/application.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ def __init__(self, api_key, application, environment, api_url=API_URL):
2424
def arg_or_env(name, args, default=None):
2525
env_name = 'STACKIFY_{0}'.format(name.upper())
2626
try:
27-
return args.get(name, os.environ[env_name])
27+
value = args.get(name)
28+
if not value:
29+
value = os.environ[env_name]
30+
return value
2831
except KeyError:
2932
if default:
3033
return default
@@ -38,5 +41,5 @@ def get_configuration(**kwargs):
3841
application = arg_or_env('application', kwargs),
3942
environment = arg_or_env('environment', kwargs),
4043
api_key = arg_or_env('api_key', kwargs),
41-
api_url = arg_or_env('api_url', kwargs))
44+
api_url = arg_or_env('api_url', kwargs, API_URL))
4245

tests/bases.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import unittest
3+
4+
class ClearEnvTest(unittest.TestCase):
5+
'''
6+
This class clears the environment variables that the
7+
library uses for clean testing.
8+
'''
9+
10+
def setUp(self):
11+
# if you have these specified in the environment it will break tests
12+
to_save = [
13+
'STACKIFY_APPLICATION',
14+
'STACKIFY_ENVIRONMENT',
15+
'STACKIFY_API_KEY',
16+
'STACKIFY_API_URL',
17+
]
18+
self.saved = {}
19+
for key in to_save:
20+
if key in os.environ:
21+
self.saved[key] = os.environ[key]
22+
del os.environ[key]
23+
24+
def tearDown(self):
25+
# restore deleted environment variables
26+
for key, item in self.saved.items():
27+
os.environ[key] = item
28+
del self.saved

tests/test_application.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,53 @@
55

66
import unittest
77
from mock import patch
8+
import os
9+
from bases import ClearEnvTest
810

11+
from stackify import API_URL
912
from 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

5694
if __name__=='__main__':
5795
unittest.main()

tests/test_init.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python
2+
"""
3+
Test the stackify.__init__ setup functions
4+
"""
5+
6+
import unittest
7+
from mock import patch
8+
from bases import ClearEnvTest
9+
10+
import os
11+
import atexit
12+
13+
import stackify
14+
import logging
15+
16+
17+
class TestInit(ClearEnvTest):
18+
'''
19+
Test the logger init functionality
20+
'''
21+
22+
def setUp(self):
23+
super(TestInit, self).setUp()
24+
self.config = stackify.ApiConfiguration(
25+
application = 'test_appname',
26+
environment = 'test_environment',
27+
api_key = 'test_apikey',
28+
api_url = 'test_apiurl')
29+
self.loggers = []
30+
31+
def tearDown(self):
32+
super(TestInit, self).tearDown()
33+
global_loggers = logging.Logger.manager.loggerDict
34+
for logger in self.loggers:
35+
del global_loggers[logger.name]
36+
37+
def test_logger_no_config(self):
38+
'''Logger API config loads from the environment automatically'''
39+
env_map = {
40+
'STACKIFY_APPLICATION': 'test2_appname',
41+
'STACKIFY_ENVIRONMENT': 'test2_environment',
42+
'STACKIFY_API_KEY': 'test2_apikey',
43+
'STACKIFY_API_URL': 'test2_apiurl',
44+
}
45+
46+
with patch.dict('os.environ', env_map):
47+
logger = stackify.getLogger(auto_shutdown=False)
48+
self.loggers.append(logger)
49+
50+
config = logger.handlers[0].listener.http.api_config
51+
52+
self.assertEqual(config.application, 'test2_appname')
53+
self.assertEqual(config.environment, 'test2_environment')
54+
self.assertEqual(config.api_key, 'test2_apikey')
55+
self.assertEqual(config.api_url, 'test2_apiurl')
56+
57+
def test_logger_api_config(self):
58+
'''Logger API config loads from the specified config objects'''
59+
logger = stackify.getLogger(config=self.config, auto_shutdown=False)
60+
self.loggers.append(logger)
61+
62+
config = logger.handlers[0].listener.http.api_config
63+
64+
self.assertEqual(config.application, 'test_appname')
65+
self.assertEqual(config.environment, 'test_environment')
66+
self.assertEqual(config.api_key, 'test_apikey')
67+
self.assertEqual(config.api_url, 'test_apiurl')
68+
69+
def test_logger_name(self):
70+
'''The automatic logger name is the current module'''
71+
self.assertEqual(stackify.getCallerName(), 'tests.test_init')
72+
73+
def test_get_logger_defaults(self):
74+
'''The logger has sane defaults'''
75+
env_map = {
76+
'STACKIFY_APPLICATION': 'test2_appname',
77+
'STACKIFY_ENVIRONMENT': 'test2_environment',
78+
'STACKIFY_API_KEY': 'test2_apikey',
79+
}
80+
81+
with patch.dict('os.environ', env_map):
82+
logger = stackify.getLogger(auto_shutdown=False)
83+
self.loggers.append(logger)
84+
85+
handler = logger.handlers[0]
86+
config = handler.listener.http.api_config
87+
88+
self.assertEqual(logger.name, 'tests.test_init')
89+
self.assertEqual(config.api_url, stackify.API_URL)
90+
self.assertEqual(handler.listener.max_batch, stackify.MAX_BATCH)
91+
self.assertEqual(handler.queue.maxsize, stackify.QUEUE_SIZE)
92+
93+
def test_get_logger_reuse(self):
94+
pass
95+
96+
97+
if __name__=='__main__':
98+
unittest.main()
99+

0 commit comments

Comments
 (0)