Skip to content

Commit

Permalink
new pytest raises syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
nllong committed Oct 2, 2024
1 parent 0c8747c commit 7246439
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 109 deletions.
41 changes: 20 additions & 21 deletions pyseed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,27 @@ def read_map_file(mapfile_path):
if not mapfile_path.exists():
raise ValueError(f"Mapping file {mapfile_path} does not exist")

map_reader = None
with open(mapfile_path) as f:
map_reader = csv.reader(f)
map_reader.__next__() # Skip the header

# Open the mapping file and fill list
maplist = []
for rowitem in map_reader:
data = {
"from_field": rowitem[0],
"from_units": rowitem[1],
"to_table_name": rowitem[2],
"to_field": rowitem[3],
}
try:
if rowitem[4].lower().strip() == "true":
data["is_omitted"] = True
else:
False
except IndexError:
data["is_omitted"] = False

maplist.append(data)
map_reader.__next__() # Skip the header

# Open the mapping file and fill list
maplist = []
for rowitem in map_reader:
data = {
"from_field": rowitem[0],
"from_units": rowitem[1],
"to_table_name": rowitem[2],
"to_field": rowitem[3],
}
try:
if rowitem[4].lower().strip() == "true":
data["is_omitted"] = True
else:
False
except IndexError:
data["is_omitted"] = False

maplist.append(data)

return maplist
99 changes: 29 additions & 70 deletions tests/test_apibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from pyseed.exceptions import APIClientError
from pyseed.seed_client_base import _get_urls, _set_default

NO_URL_ERROR = "APIClientError: No url set"
SSL_ERROR = "APIClientError: use_ssl is true but url does not starts with https"
SSL_ERROR2 = "APIClientError: use_ssl is false but url starts with https"
NO_URL_ERROR = "No url set"
SSL_ERROR = "use_ssl is true but url does not starts with https"
SSL_ERROR2 = "use_ssl is false but url starts with https"

# Constants
SERVICES_DICT = {"urls": {"test1": "test1", "test2": "/test2"}}
Expand Down Expand Up @@ -68,16 +68,12 @@ def test_ssl_verification(self, mock_requests):
# ensure error is raised if http is supplied and use_ssl is true
with pytest.raises(APIClientError) as conm:
JSONAPI("http://example.org")
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

# ensure error is raised if https is supplied and use_ssl is false
with pytest.raises(APIClientError) as conm:
JSONAPI("https://example.org", use_ssl=False)
exception = conm.exception
expected = SSL_ERROR2
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR2, conm.value.error)

# test defaults to https
api = JSONAPI("example.org")
Expand Down Expand Up @@ -148,8 +144,7 @@ def __init__(self):
api = TestAPI()
with pytest.raises(APIClientError) as conm:
api._get()
exception = conm.exception
self.assertEqual("APIClientError: id is a compulsory field", str(exception))
self.assertEqual("id is a compulsory field", conm.value.error)
api._get(id=1)
self.assertTrue(mock_requests.get.called)

Expand All @@ -169,16 +164,12 @@ def test_construct_url(self, mock_requests):
# ensure error is raised if no url is supplied
with pytest.raises(APIClientError) as conm:
api._construct_url(None)
exception = conm.exception
expected = NO_URL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(NO_URL_ERROR, conm.value.error)

# ensure error is raised if https is supplied and use_ssl is false
with pytest.raises(APIClientError) as conm:
api._construct_url("https://www.example.org", use_ssl=False)
exception = conm.exception
expected = SSL_ERROR2
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR2, conm.value.error)

def test_construct_url_ssl_explicit(self, mock_requests):
"""Test _construct_url method."""
Expand All @@ -187,16 +178,12 @@ def test_construct_url_ssl_explicit(self, mock_requests):
# ensure error is raised if http is supplied and use_ssl is true
with pytest.raises(APIClientError) as conm:
api._construct_url("http://example.org", use_ssl=True)
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

# ensure error is raised if http is supplied and use_ssl is default
with pytest.raises(APIClientError) as conm:
api._construct_url("http://example.org")
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

def test_construct_url_ssl_implicit(self, mock_requests):
"""Test _construct_url method."""
Expand All @@ -205,16 +192,12 @@ def test_construct_url_ssl_implicit(self, mock_requests):
# ensure error is raised if http is supplied and use_ssl is true
with pytest.raises(APIClientError) as conm:
api._construct_url("http://example.org", use_ssl=True)
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

# ensure error is raised if http is supplied and use_ssl is default
with pytest.raises(APIClientError) as conm:
api._construct_url("http://example.org")
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)


@mock.patch("pyseed.apibase.requests")
Expand All @@ -239,23 +222,17 @@ def test_get(self, mock_requests):
api = BaseAPI("example.org", use_ssl=False)
with pytest.raises(APIClientError) as conm:
api._get(url="https://www.example.org", use_ssl=False)
exception = conm.exception
expected = SSL_ERROR2
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR2, conm.value.error)

# ensure error is raised if http is supplied and use_ssl is true
with pytest.raises(APIClientError) as conm:
self.api._get(url="http://example.org")
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

# ensure error is raised if no url is supplied
with pytest.raises(APIClientError) as conm:
self.api._get()
exception = conm.exception
expected = NO_URL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(NO_URL_ERROR, conm.value.error)

# test defaults to http
self.api._get(url=self.url)
Expand All @@ -282,24 +259,18 @@ def test_post(self, mock_requests):
# ensure error is raised if no url is supplied
with pytest.raises(APIClientError) as conm:
self.api._post(params=params, files=files, foo="bar", test="test")
exception = conm.exception
expected = NO_URL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(NO_URL_ERROR, conm.value.error)

# ensure error is raised if https is supplied and use_ssl is false
api = BaseAPI("example.org", use_ssl=False)
with pytest.raises(APIClientError) as conm:
api._post(url="https://example.org", use_ssl=False, params=params, files=files, foo="bar", test="test")
exception = conm.exception
expected = SSL_ERROR2
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR2, conm.value.error)

# ensure error is raised if http is supplied and use_ssl is true
with pytest.raises(APIClientError) as conm:
self.api._post(url="http://example.org", use_ssl=True, params=params, files=files, foo="bar", test="test")
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

def test_patch(self, mock_requests):
"""Test _get_patch."""
Expand All @@ -317,24 +288,18 @@ def test_patch(self, mock_requests):
# ensure error is raised if no url is supplied
with pytest.raises(APIClientError) as conm:
self.api._patch(params=params, files=files, foo="bar", test="test")
exception = conm.exception
expected = NO_URL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(NO_URL_ERROR, conm.value.error)

# ensure error is raised if https is supplied and use_ssl is false
api = BaseAPI("example.org", use_ssl=False)
with pytest.raises(APIClientError) as conm:
api._patch(url="https://example.org", use_ssl=False, params=params, files=files, foo="bar", test="test")
exception = conm.exception
expected = SSL_ERROR2
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR2, conm.value.error)

# ensure error is raised if http is supplied and use_ssl is true
with pytest.raises(APIClientError) as conm:
self.api._patch(url="http://example.org", use_ssl=True, params=params, files=files, foo="bar", test="test")
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

def test_delete(self, mock_requests):
"""Test _delete method."""
Expand All @@ -345,23 +310,17 @@ def test_delete(self, mock_requests):
api = BaseAPI("example.org", use_ssl=False)
with pytest.raises(APIClientError) as conm:
api._delete(url="https://www.example.org")
exception = conm.exception
expected = SSL_ERROR2
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR2, conm.value.error)

# ensure error is raised if http is supplied and use_ssl is true
with pytest.raises(APIClientError) as conm:
self.api._delete(url="http://example.org")
exception = conm.exception
expected = SSL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(SSL_ERROR, conm.value.error)

# ensure error is raised if no url is supplied
with pytest.raises(APIClientError) as conm:
self.api._delete()
exception = conm.exception
expected = NO_URL_ERROR
self.assertEqual(expected, str(exception))
self.assertEqual(NO_URL_ERROR, conm.value.error)

# test defaults to http
self.api._delete(url=self.url)
Expand All @@ -379,19 +338,19 @@ def testadd_pk(self):
# Error checks
with pytest.raises(APIClientError) as conm:
add_pk("url", None)
self.assertEqual("APIClientError: id/pk must be supplied", str(conm.exception))
self.assertEqual("id/pk must be supplied", conm.value.error)

with pytest.raises(TypeError) as conm:
add_pk("url", "a")
self.assertEqual("id/pk must be a positive integer", str(conm.exception))
self.assertEqual("id/pk must be a positive integer", conm.value.args[0])

with pytest.raises(TypeError) as conm:
add_pk("url", 1.2)
self.assertEqual("id/pk must be a positive integer", str(conm.exception))
self.assertEqual("id/pk must be a positive integer", conm.value.args[0])

with pytest.raises(TypeError) as conm:
add_pk("url", -1)
self.assertEqual("id/pk must be a positive integer", str(conm.exception))
self.assertEqual("id/pk must be a positive integer", conm.value.args[0])

# adds ints
result = add_pk("url", 1)
Expand Down Expand Up @@ -423,7 +382,7 @@ def test_set_default(self):
# raises error if attribute not set and val is none
with pytest.raises(AttributeError) as conm:
_set_default(obj, "nokey", None)
self.assertEqual("nokey is not set", str(conm.exception))
self.assertEqual("nokey is not set", conm.value.args[0])

result = _set_default(obj, "key", None)
self.assertNotEqual(result, None)
Expand Down
36 changes: 18 additions & 18 deletions tests/test_seed_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,36 @@ def test_check_response_inheritance(self, mock_requests):
with pytest.raises(SEEDError) as conm:
self.client.get(1)

self.assertEqual(conm.exception.error, "No llama!")
self.assertEqual(conm.exception.service, "SEED")
self.assertEqual(conm.exception.url, url)
self.assertEqual(conm.exception.caller, "MySeedClient.get")
self.assertEqual(conm.exception.verb.upper(), "GET")
self.assertEqual(conm.exception.status_code, 200)
self.assertEqual(conm.value.error, "No llama!")
self.assertEqual(conm.value.service, "SEED")
self.assertEqual(conm.value.url, url)
self.assertEqual(conm.value.caller, "MySeedClient.get")
self.assertEqual(conm.value.verb.upper(), "GET")
self.assertEqual(conm.value.status_code, 200)

# newer/correct using status codes (no message)
mock_requests.get.return_value = get_mock_response(status_code=404, data="No llama!", error=True, content=False)
with pytest.raises(SEEDError) as conm:
self.client.get(1)

self.assertEqual(conm.exception.error, "SEED returned status code: 404")
self.assertEqual(conm.exception.service, "SEED")
self.assertEqual(conm.exception.url, url)
self.assertEqual(conm.exception.caller, "MySeedClient.get")
self.assertEqual(conm.exception.verb.upper(), "GET")
self.assertEqual(conm.exception.status_code, 404)
self.assertEqual(conm.value.error, "SEED returned status code: 404")
self.assertEqual(conm.value.service, "SEED")
self.assertEqual(conm.value.url, url)
self.assertEqual(conm.value.caller, "MySeedClient.get")
self.assertEqual(conm.value.verb.upper(), "GET")
self.assertEqual(conm.value.status_code, 404)

# newer/correct using status codes (with message)
mock_requests.get.return_value = get_mock_response(status_code=404, data="No llama!", error=True, content=True)
with pytest.raises(SEEDError) as conm:
self.client.get(1)

self.assertEqual(conm.exception.error, "No llama!")
self.assertEqual(conm.exception.service, "SEED")
self.assertEqual(conm.exception.url, url)
self.assertEqual(conm.exception.caller, "MySeedClient.get")
self.assertEqual(conm.exception.verb.upper(), "GET")
self.assertEqual(conm.exception.status_code, 404)
self.assertEqual(conm.value.error, "No llama!")
self.assertEqual(conm.value.service, "SEED")
self.assertEqual(conm.value.url, url)
self.assertEqual(conm.value.caller, "MySeedClient.get")
self.assertEqual(conm.value.verb.upper(), "GET")
self.assertEqual(conm.value.status_code, 404)


class SEEDClientMethodTests(unittest.TestCase):
Expand Down

0 comments on commit 7246439

Please sign in to comment.