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

Commit 3ceb1dd

Browse files
committed
add unittests for utils.UnifiedConfiguration
1 parent 1bf7042 commit 3ceb1dd

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

tests/test_utils.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import sys
22

33
import unittest
4-
from mock import MagicMock
4+
import mock
5+
from mock import MagicMock, patch, mock_open
6+
import json
57

68

79
class TestDeepUpdate(unittest.TestCase):
810

911
def setUp(self):
12+
1013
self.u = {
1114
"first": {"a": "A"},
1215
"second": "B"
@@ -26,6 +29,36 @@ def test_deep_update(self):
2629
self.assertDictEqual(self.d, self.deep_update(self.d, self.u))
2730

2831

32+
class TestUnifiedConfiguration(unittest.TestCase):
33+
34+
def setUp(self):
35+
self.test_json = json.dumps({
36+
"email": {
37+
"value": ["test@cern.ch", "test@testmail.com"],
38+
"description": "The list of people that get the emails notifications"
39+
}
40+
})
41+
42+
def test_get_json(self):
43+
44+
from WmAgentScripts.utils import unifiedConfiguration, open_json_file
45+
mock_open_json = mock_open(read_data=self.test_json)
46+
with patch('__builtin__.open', mock_open_json):
47+
result = open_json_file('filename')
48+
uc = unifiedConfiguration(configFile='fake_file_path')
49+
self.assertEqual(
50+
uc.get("email"), ["test@cern.ch", "test@testmail.com"])
51+
52+
with self.assertRaises(SystemExit):
53+
self.assertEqual(
54+
uc.get("cernmails"), [
55+
"test@cern.ch", "test@testmail.com"])
56+
57+
def test_get_mongodb(self):
58+
59+
# TODO: mock db and write tests
60+
pass
61+
2962

3063
if __name__ == '__main__':
3164
unittest.main()

utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def mongo_client():
3636
return pymongo.MongoClient('mongodb://%s/?ssl=true' % mongo_db_url,
3737
ssl_cert_reqs=ssl.CERT_NONE)
3838

39+
def open_json_file(filename):
40+
with open(filename) as f:
41+
return json.loads(f.read())
3942

4043
class unifiedConfiguration:
4144
def __init__(self, configFile='unifiedConfiguration.json'):
@@ -45,7 +48,7 @@ def __init__(self, configFile='unifiedConfiguration.json'):
4548
self.configs = self.configFile
4649
else:
4750
try:
48-
self.configs = json.loads(open(self.configFile).read())
51+
self.configs = open_json_file(self.configFile)
4952
except Exception as ex:
5053
print("Could not read configuration file: %s\nException: %s" %
5154
(self.configFile, str(ex)))

0 commit comments

Comments
 (0)