Skip to content

Commit 1dd4308

Browse files
committed
Fix up after adjustment to test behaviour in upload
1 parent f141834 commit 1dd4308

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

src/cmip6_data_citation_generator/upload.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from os.path import isfile, isdir, join
1+
from os.path import isfile, join
22
from glob import glob
33
import json
44

55

66
from netrc import netrc
7-
import httplib2
87
from httplib2 import Http
98

109

@@ -41,4 +40,4 @@ def upload_jsons(inputs, test=False):
4140

4241
errors = errors or not content.startswith("SUCCESS")
4342

44-
return errors
43+
return 1 if errors else 0

tests/unit/test_upload.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from unittest.mock import patch, MagicMock
1+
from unittest.mock import patch
22
import json
3-
import subprocess
43

54

65
import pytest
@@ -12,13 +11,9 @@
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"))
2217
def 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

Comments
 (0)