diff options
author | Michael McVady <femtonaut@gmail.com> | 2023-02-18 20:47:48 -0600 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2023-02-18 20:47:48 -0600 |
commit | 5996a8a37729b0e5a801215aee212706688b4e73 (patch) | |
tree | ac92c832d7e7f8bed6ec5f84994d0cee8c0fe485 | |
parent | 36909982aae7fb6297ba8a4aaf8dc08fdbbd922b (diff) |
Refactor tests
-rwxr-xr-x | tests.py | 36 |
1 files changed, 15 insertions, 21 deletions
@@ -1,7 +1,7 @@ #!/usr/bin/env python3 -import logging import json +import logging import sys import uuid @@ -13,8 +13,10 @@ POST_UUID = "2dc42f26-884e-4b10-abfb-4fd394aa9d62" BAD_UUID = "00000000-0000-1111-0000-000000000000" INVALID_UUID = "0" -log = logging.getLogger(__name__) +JSON_ACCEPT_HEADER = {"Accept": "application/json"} +log = logging.getLogger(__name__) +log.setLevel(logging.INFO) def test_redirect(): url = f"{BASE_URL}/asdf" @@ -75,11 +77,10 @@ def test_html_get_post_not_found(): def test_json_get_post(): url = f"{BASE_URL}/posts/{POST_UUID}" - r = requests.get(url, headers={"Accept": "application/json"}) + r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() post = j[0] - assert len(j) == 1 assert "A Solid Breakdown of the Linux Font Rendering Stack" == post["title"] assert "hinting and anti-aliasing" in post["body"] @@ -87,15 +88,13 @@ def test_json_get_post(): def test_json_get_posts(): url = f"{BASE_URL}/posts" - r = requests.get(url, headers={"Accept": "application/json"}) + r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() - assert len(j) >= 1 text = json.dumps(j) assert "A Solid Breakdown of the Linux Font Rendering Stack" in text assert "hinting and anti-aliasing" in text - assert "Debugging CGI / CGit" in text @@ -103,58 +102,51 @@ def test_json_post_posts(): url = f"{BASE_URL}/posts" uuid_ = str(uuid.uuid4()) data = {"id": uuid_, "title": "title", "body": "body"} - r = requests.post(url, headers={"Accept": "application/json"}, json=data) - + r = requests.post(url, headers=JSON_ACCEPT_HEADER, json=data) assert r.status_code == 201 - j = r.json() - assert "Resource created successfully." == j url = f"{BASE_URL}/posts/{uuid_}" - r = requests.get(url, headers={"Accept": "application/json"}) + r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() - assert len(j) == 1 assert "title" == j[0]["title"] assert "body" == j[0]["body"] def test_json_delete_posts(): - headers = {"Accept": "application/json"} url = f"{BASE_URL}/posts" uuid_ = DELETE_UUID data = {"id": uuid_, "title": "title", "body": "body"} # Create post copy-pasta. - r = requests.post(url, headers=headers, json=data) + r = requests.post(url, headers=JSON_ACCEPT_HEADER, json=data) assert r.status_code == 201 j = r.json() url = f"{BASE_URL}/posts/{uuid_}" - r = requests.delete(url, headers=headers) + r = requests.delete(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 200 j = r.json() - r = requests.get(url, headers=headers) + r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 404 j = r.json() def test_json_get_post_not_found(): url = f"{BASE_URL}/posts/{BAD_UUID}" - r = requests.get(url, headers={"Accept": "application/json"}) + r = requests.get(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 404 j = r.json() - assert "Resource not found" in j url = f"{BASE_URL}/posts/{INVALID_UUID}" - r = requests.delete(url, headers={"Accept": "application/json"}) + r = requests.delete(url, headers=JSON_ACCEPT_HEADER) assert r.status_code == 404 j = r.json() - assert "Resource not found" in j @@ -167,9 +159,11 @@ def main(): except Exception: winner = False log.exception(f"🚫 {func} failed 🚫🏆") + print(f"🚫 {func} failed 🚫🏆") if winner: log.info("🏆 You're winner !") + print("🏆 You're winner !") if __name__ == "__main__": |