1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import pytest
import requests
import requests_mock
import sodapy.utils as utils
@pytest.mark.parametrize(
("status_code", "status_type", "reason", "raises_exception"),
[
(200, "Success", "OK", False),
(300, "Redirection", "Multiple Choices", False),
(400, "Client Error", "Bad Request", True),
(500, "Server Error", "Internal Server Error", True),
(600, "Foo Bar", "Here be dragons", False),
],
)
def test_raise_for_status(status_code, status_type, reason, raises_exception):
response = requests.models.Response()
response.status_code = status_code
response.reason = reason
if raises_exception:
with pytest.raises(
requests.exceptions.HTTPError,
match="{} {}: {}".format(status_code, status_type, reason),
):
utils.raise_for_status(response)
else:
utils.raise_for_status(response)
@pytest.mark.parametrize(
("elems", "result"),
[
({}, {}),
({"a": 1, "b": None, "c": "d"}, {"a": 1, "c": "d"}),
({"s": "", "c": 0}, {"s": "", "c": 0}),
],
)
def test_clear_empty_values(elems, result):
assert utils.clear_empty_values(elems) == result
def test_format_old_api_request_exception():
with pytest.raises(Exception):
utils.format_old_api_request()
@pytest.mark.parametrize(
("dataid", "content_type", "path"),
[
("abcd", None, "/api/views/abcd"),
("abcd", "json", "/api/views/abcd.json"),
(None, "json", "/api/views.json"),
],
)
def test_format_old_api_request(dataid, content_type, path):
assert (
utils.format_old_api_request(dataid=dataid, content_type=content_type) == path
)
@pytest.mark.parametrize(
("dataid", "row_id", "content_type", "path"),
[
("abcd", None, "json", "/resource/abcd.json"),
("abcd", 123, "json", "/resource/abcd/123.json"),
],
)
def test_format_new_api_request(dataid, row_id, content_type, path):
assert (
utils.format_new_api_request(
dataid=dataid, row_id=row_id, content_type=content_type
)
== path
)
def test_format_new_api_request_exception():
with pytest.raises(Exception):
utils.format_new_api_request()
@pytest.mark.parametrize(
("username", "password", "token"),
[("me", None, "123456"), (None, "pass", "123456"), ("me", "pass", "123456")],
)
def test_authentication_validation_exceptions(username, password, token):
with pytest.raises(Exception):
utils.authentication_validation(username, password, token)
@pytest.mark.parametrize(
("username", "password", "token"), [("me", "pass", None), (None, None, "93738")],
)
def test_authentication_validation(username, password, token):
utils.authentication_validation(username, password, token)
def test_download_file(tmp_path):
path = tmp_path / "myfile.txt"
url = "http://fileserver.dev/file"
text = "the response data"
with requests_mock.Mocker() as mock:
mock.get(url, text=text)
utils.download_file(url, str(path))
assert path.read_text() == text
|