1010from unittest .case import TestCase
1111
1212import mock
13+ import requests
1314from mock import patch
1415from nose2 .tools import params
1516from requests .models import HTTPError
@@ -31,6 +32,11 @@ def create_dummy_client(server_url="http://www.test.com"):
3132class TestOsduClient (TestCase ):
3233 """Test cases for base OSDU client"""
3334
35+ dummy_headers = {"headers" : "value" }
36+ sample_json = {
37+ "name" : "value" ,
38+ }
39+
3440 def test_init (self ):
3541 """Test the init method"""
3642 server_url = "http://www.test.com"
@@ -65,6 +71,26 @@ def test_get_headers(self, mock_get_token): # pylint: disable=W0613
6571
6672 self .assertDictEqual (expected_headers , headers )
6773
74+ # region test get
75+ @params (
76+ "http://www.test.com/" ,
77+ "http://www.test.com/test2/" ,
78+ )
79+ @patch .object (OsduClient , "get_headers" , return_value = dummy_headers )
80+ def test_get (self , url , _ ):
81+ """Test valid get returns expected values"""
82+ response_mock = mock .Mock ()
83+ with mock .patch .object (requests , "get" , return_value = response_mock ) as mock_get :
84+ client = create_dummy_client ()
85+
86+ response = client .get (url )
87+
88+ mock_get .assert_called_once ()
89+ mock_get .assert_called_with (url , headers = self .dummy_headers )
90+ self .assertEqual (response_mock , response )
91+
92+ # endregion test get
93+
6894 # region test get_returning_json
6995 @params ((None , 200 ), ([200 ], 200 ), ([200 , 202 ], 202 ), ([202 ], 202 ))
7096 def test_get_returning_json (self , expected_status_codes , actual_status_code ):
@@ -104,9 +130,56 @@ def test_get_returning_json_http_error_throws_exception(
104130
105131 # endregion test get_returning_json
106132
133+ # region test post
134+ @params (
135+ "string1" ,
136+ "string2" ,
137+ )
138+ @patch .object (OsduClient , "get_headers" , return_value = dummy_headers )
139+ def test_post_string (self , string_data , _ ):
140+ """Test valid post with string returns expected values"""
141+ response_mock = mock .Mock ()
142+ with mock .patch .object (requests , "post" , return_value = response_mock ) as mock_post :
143+ client = create_dummy_client ()
144+
145+ response = client .post ("http://www.test.com/" , string_data )
146+
147+ mock_post .assert_called_once ()
148+ mock_post .assert_called_with (
149+ "http://www.test.com/" , data = string_data , json = None , headers = self .dummy_headers
150+ )
151+ self .assertEqual (response_mock , response )
152+
153+ @params (
154+ {"name" : "value" },
155+ {"name2" : "value2" },
156+ )
157+ @patch .object (OsduClient , "get_headers" , return_value = dummy_headers )
158+ def test_post_json (self , json , _ ):
159+ """Test valid post with json returns expected values"""
160+ response_mock = mock .Mock ()
161+ with mock .patch .object (requests , "post" , return_value = response_mock ) as mock_post :
162+ client = create_dummy_client ()
163+
164+ response = client .post ("http://www.test.com/" , json )
165+
166+ mock_post .assert_called_once ()
167+ mock_post .assert_called_with (
168+ "http://www.test.com/" , data = None , json = json , headers = self .dummy_headers
169+ )
170+ self .assertEqual (response_mock , response )
171+
172+ # endregion test post
173+
107174 # region test post_returning_json
108- @params ((None , 200 ), ([200 ], 200 ), ([200 , 202 ], 202 ), ([202 ], 202 ))
109- def test_post_returning_json (self , expected_status_codes , actual_status_code ):
175+
176+ @params (
177+ (None , 200 ),
178+ ([200 ], 200 ),
179+ ([200 , 202 ], 202 ),
180+ ([202 ], 202 ),
181+ )
182+ def test_post_returning_json_status_codes (self , expected_status_codes , actual_status_code ):
110183 """Test valid post returns expected values"""
111184 input_data = {
112185 "name" : "value" ,
@@ -151,6 +224,100 @@ def test_post_returning_json_http_error_throws_exception(
151224
152225 # endregion test post_returning_json
153226
227+ # region test put
228+ @params (
229+ "string1" ,
230+ "string2" ,
231+ )
232+ @patch .object (OsduClient , "get_headers" , return_value = dummy_headers )
233+ def test_put_string (self , string_data , _ ):
234+ """Test valid put with string returns expected values"""
235+ response_mock = mock .Mock ()
236+ with mock .patch .object (requests , "put" , return_value = response_mock ) as mock_put :
237+ client = create_dummy_client ()
238+
239+ response = client .put ("http://www.test.com/" , string_data )
240+
241+ mock_put .assert_called_once ()
242+ mock_put .assert_called_with (
243+ "http://www.test.com/" , data = string_data , json = None , headers = self .dummy_headers
244+ )
245+ self .assertEqual (response_mock , response )
246+
247+ @params (
248+ {"name" : "value" },
249+ {"name2" : "value2" },
250+ )
251+ @patch .object (OsduClient , "get_headers" , return_value = dummy_headers )
252+ def test_put_json (self , json , _ ):
253+ """Test valid put with json returns expected values"""
254+ response_mock = mock .Mock ()
255+ with mock .patch .object (requests , "put" , return_value = response_mock ) as mock_put :
256+ client = create_dummy_client ()
257+
258+ response = client .put ("http://www.test.com/" , json )
259+
260+ mock_put .assert_called_once ()
261+ mock_put .assert_called_with (
262+ "http://www.test.com/" , data = None , json = json , headers = self .dummy_headers
263+ )
264+ self .assertEqual (response_mock , response )
265+
266+ # endregion test put
267+
268+ # region test put_returning_json
269+
270+ @params (
271+ (None , 200 ),
272+ ([200 ], 200 ),
273+ ([200 , 202 ], 202 ),
274+ ([202 ], 202 ),
275+ )
276+ def test_put_returning_json_status_codes (self , expected_status_codes , actual_status_code ):
277+ """Test valid put returns expected values"""
278+ input_data = {
279+ "name" : "value" ,
280+ }
281+ expected_response_data = input_data
282+ ok_response_mock = mock .Mock ()
283+ type(ok_response_mock ).status_code = mock .PropertyMock (return_value = actual_status_code )
284+ ok_response_mock .json .return_value = expected_response_data
285+ with mock .patch ("osdu.client.OsduClient.put" , return_value = ok_response_mock ) as mock_get :
286+ client = create_dummy_client ()
287+
288+ if expected_status_codes :
289+ response = client .put_returning_json (
290+ "http://www.test.com/" , input_data , expected_status_codes
291+ )
292+ else :
293+ response = client .put_returning_json ("http://www.test.com/" , input_data )
294+
295+ mock_get .assert_called_once ()
296+ mock_get .assert_called_with ("http://www.test.com/" , input_data )
297+ self .assertDictEqual (expected_response_data , response )
298+
299+ @params ((None , 404 ), (None , 201 ), ([200 ], 404 ), ([200 , 202 ], 500 ))
300+ def test_put_returning_json_http_error_throws_exception (
301+ self , expected_status_codes , actual_status_code
302+ ):
303+ """Test put error returns expected values"""
304+ input_data = {
305+ "name" : "value" ,
306+ }
307+ error_response_mock = mock .MagicMock ()
308+ type(error_response_mock ).status_code = mock .PropertyMock (return_value = actual_status_code )
309+ with mock .patch ("osdu.client.OsduClient.put" , return_value = error_response_mock ):
310+ with self .assertRaises (HTTPError ):
311+ client = create_dummy_client ()
312+ if expected_status_codes :
313+ _ = client .put_returning_json (
314+ "http://www.test.com/" , input_data , expected_status_codes
315+ )
316+ else :
317+ _ = client .put_returning_json ("http://www.test.com/" , input_data )
318+
319+ # endregion test put_returning_json
320+
154321 # region test delete
155322 @params ((None , 200 ), ([200 ], 200 ), ([200 , 202 ], 202 ), ([202 ], 202 ))
156323 @patch .object (OsduClient , "get_headers" , return_value = (None ))
0 commit comments