1- from unittest .mock import patch , MagicMock
1+ from unittest .mock import patch
22import json
3- import subprocess
43
54
65import pytest
1211
1312@patch ("cmip6_data_citation_generator.upload.netrc" )
1413@patch ("cmip6_data_citation_generator.upload.Http" )
15- @pytest .mark .parametrize (
16- "tinput,ttest,tcontent" ,
17- [
18- (TEST_VALID_OUTPUT_JSON , False , "SUCCESS content" ),
19- (TEST_DATA_ROOT_DIR , True , "junk" ),
20- ],
21- )
14+ @pytest .mark .parametrize ("tinput" , (TEST_VALID_OUTPUT_JSON , TEST_DATA_ROOT_DIR ))
15+ @pytest .mark .parametrize ("ttest" , (True , False ))
16+ @pytest .mark .parametrize ("tcontent" , (b"SUCCESS content" , b"junk" ))
2217def test_upload_file (mock_Http , mock_netrc , tinput , ttest , tcontent ):
2318 tlogin = "login"
2419 tpassword = "pword"
@@ -27,31 +22,28 @@ class TResponse(object):
2722 status = "status"
2823
2924 tresponse = TResponse ()
30- tcontent = "SUCCESS content"
3125
3226 with open (TEST_VALID_OUTPUT_JSON ) as f :
3327 expected_data = json .dumps (json .load (f ))
3428
35- if ttest :
36- expected_dest = "http://ceracite.dkrz.de:5000/api/v1/citation?test=1"
37- else :
38- expected_dest = "http://ceracite.dkrz.de:5000/api/v1/citation"
39-
4029 mock_netrc .return_value .authenticators .return_value = (tlogin , "" , tpassword )
4130 mock_Http .return_value .request .return_value = (tresponse , tcontent )
4231
4332 res = upload_jsons (tinput , test = ttest )
44- if tcontent .startswith ("SUCCESS" ):
33+ if tcontent .decode ( "utf-8" ). startswith ("SUCCESS" ) or ttest :
4534 assert res == 0
4635 else :
4736 assert res == 1
4837
49- mock_netrc .return_value .authenticators .assert_called_with ("cera" )
50- mock_Http .return_value .add_credentials .assert_called_with (tlogin , tpassword )
38+ if not ttest :
39+ expected_dest = "http://ceracite.dkrz.de:5000/api/v1/citation"
40+
41+ mock_netrc .return_value .authenticators .assert_called_with ("cera" )
42+ mock_Http .return_value .add_credentials .assert_called_with (tlogin , tpassword )
5143
52- mock_Http .return_value .request .assert_called_with (
53- expected_dest ,
54- "POST" ,
55- expected_data ,
56- headers = {"Content-Type" : "application/json" },
57- )
44+ mock_Http .return_value .request .assert_called_with (
45+ expected_dest ,
46+ "POST" ,
47+ expected_data ,
48+ headers = {"Content-Type" : "application/json" },
49+ )
0 commit comments