|
1 | 1 | import sys
|
2 | 2 |
|
3 | 3 | import unittest
|
| 4 | +import json |
4 | 5 | import mock
|
5 | 6 | from mock import MagicMock, patch, mock_open
|
6 |
| -import json |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class TestDeepUpdate(unittest.TestCase):
|
@@ -44,7 +44,7 @@ def test_get_json(self):
|
44 | 44 | from WmAgentScripts.utils import unifiedConfiguration, open_json_file
|
45 | 45 | mock_open_json = mock_open(read_data=self.test_json)
|
46 | 46 | with patch('__builtin__.open', mock_open_json):
|
47 |
| - result = open_json_file('filename') |
| 47 | + open_json_file('filename') |
48 | 48 | uc = unifiedConfiguration(configFile='fake_file_path')
|
49 | 49 | self.assertEqual(
|
50 | 50 | uc.get("email"), ["test@cern.ch", "test@testmail.com"])
|
@@ -82,5 +82,90 @@ def test_url_encode_params(self):
|
82 | 82 | self.assertEqual(result, "query2=test2&query2=test3&query1=test1")
|
83 | 83 |
|
84 | 84 |
|
| 85 | +class TestGET(unittest.TestCase): |
| 86 | + |
| 87 | + def test_get(self): |
| 88 | + |
| 89 | + class MockResponse: |
| 90 | + def __init__(self, *args, **kwargs): |
| 91 | + self.response = None, 404 |
| 92 | + |
| 93 | + def request(self, *args, **kwargs): |
| 94 | + if args[1] == 'test.json': |
| 95 | + self.response = {"key1": "value1"}, 200 |
| 96 | + elif args[1] == 'anothertest.json': |
| 97 | + self.response = {"key2": "value2"}, 200 |
| 98 | + else: |
| 99 | + self.response = None, 404 |
| 100 | + |
| 101 | + def getresponse(self): |
| 102 | + return self.response |
| 103 | + |
| 104 | + from WmAgentScripts.utils import GET |
| 105 | + with patch('WmAgentScripts.utils.make_x509_conn', MockResponse): |
| 106 | + response = GET( |
| 107 | + url='http://someurl.com/', |
| 108 | + there='test.json', |
| 109 | + l=False) |
| 110 | + self.assertDictEqual({"key1": "value1"}, response[0]) |
| 111 | + self.assertEqual(200, response[1]) |
| 112 | + |
| 113 | + response = GET( |
| 114 | + url='http://someurl.com/', |
| 115 | + there='anothertest.json', |
| 116 | + l=False) |
| 117 | + self.assertDictEqual({"key2": "value2"}, response[0]) |
| 118 | + self.assertEqual(200, response[1]) |
| 119 | + |
| 120 | + response = GET(url=None, there=None, l=False) |
| 121 | + self.assertEqual((None, 404), response) |
| 122 | + |
| 123 | + def test_get_json_object(self): |
| 124 | + |
| 125 | + from StringIO import StringIO |
| 126 | + |
| 127 | + class ContextualStringIO(StringIO): |
| 128 | + def __enter__(self): |
| 129 | + return self |
| 130 | + |
| 131 | + def __exit__(self, *args): |
| 132 | + self.close() |
| 133 | + return False |
| 134 | + |
| 135 | + class MockResponseStringIo: |
| 136 | + def __init__(self, *args, **kwargs): |
| 137 | + self.response = None, 404 |
| 138 | + |
| 139 | + def request(self, *args, **kwargs): |
| 140 | + if args[1] == 'test.json': |
| 141 | + self.response = {"key1": "value1"}, 200 |
| 142 | + elif args[1] == 'anothertest.json': |
| 143 | + self.response = {"key2": "value2"}, 200 |
| 144 | + else: |
| 145 | + self.response = None, 404 |
| 146 | + |
| 147 | + def getresponse(self): |
| 148 | + return ContextualStringIO(json.dumps(self.response)) |
| 149 | + |
| 150 | + from WmAgentScripts.utils import GET |
| 151 | + with patch('WmAgentScripts.utils.make_x509_conn', MockResponseStringIo): |
| 152 | + response = GET( |
| 153 | + url='http://someurl.com/', |
| 154 | + there='test.json', |
| 155 | + l=True) |
| 156 | + self.assertDictEqual({"key1": "value1"}, response[0]) |
| 157 | + self.assertEqual(200, response[1]) |
| 158 | + |
| 159 | + response = GET( |
| 160 | + url='http://someurl.com/', |
| 161 | + there='anothertest.json', |
| 162 | + l=True) |
| 163 | + self.assertDictEqual({"key2": "value2"}, response[0]) |
| 164 | + self.assertEqual(200, response[1]) |
| 165 | + |
| 166 | + response = GET(url=None, there=None, l=True) |
| 167 | + self.assertEqual([None, 404], response) |
| 168 | + |
| 169 | + |
85 | 170 | if __name__ == '__main__':
|
86 | 171 | unittest.main()
|
0 commit comments