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

Commit c40504b

Browse files
committed
adds test for utils.GET
1 parent f0f5bff commit c40504b

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

tests/test_utils.py

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
22

33
import unittest
4+
import json
45
import mock
56
from mock import MagicMock, patch, mock_open
6-
import json
77

88

99
class TestDeepUpdate(unittest.TestCase):
@@ -44,7 +44,7 @@ def test_get_json(self):
4444
from WmAgentScripts.utils import unifiedConfiguration, open_json_file
4545
mock_open_json = mock_open(read_data=self.test_json)
4646
with patch('__builtin__.open', mock_open_json):
47-
result = open_json_file('filename')
47+
open_json_file('filename')
4848
uc = unifiedConfiguration(configFile='fake_file_path')
4949
self.assertEqual(
5050
uc.get("email"), ["test@cern.ch", "test@testmail.com"])
@@ -82,5 +82,90 @@ def test_url_encode_params(self):
8282
self.assertEqual(result, "query2=test2&query2=test3&query1=test1")
8383

8484

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+
85170
if __name__ == '__main__':
86171
unittest.main()

0 commit comments

Comments
 (0)