From 08c33ae11f6cad3e6ae1d48183777806b3988b3b Mon Sep 17 00:00:00 2001 From: Michael Krahl Date: Thu, 4 Nov 2021 17:55:12 -0600 Subject: [PATCH 1/5] Initial commit --- endpoints/User/users.py | 0 endpoints/get_root.py | 0 endpoints/get_user.py | 0 mtest/test_get_root.py | 0 mtest/test_get_user.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 endpoints/User/users.py create mode 100644 endpoints/get_root.py create mode 100644 endpoints/get_user.py create mode 100644 mtest/test_get_root.py create mode 100644 mtest/test_get_user.py diff --git a/endpoints/User/users.py b/endpoints/User/users.py new file mode 100644 index 0000000..e69de29 diff --git a/endpoints/get_root.py b/endpoints/get_root.py new file mode 100644 index 0000000..e69de29 diff --git a/endpoints/get_user.py b/endpoints/get_user.py new file mode 100644 index 0000000..e69de29 diff --git a/mtest/test_get_root.py b/mtest/test_get_root.py new file mode 100644 index 0000000..e69de29 diff --git a/mtest/test_get_user.py b/mtest/test_get_user.py new file mode 100644 index 0000000..e69de29 From e6d0b39cb249e5121a7809876b996dd2db77a29e Mon Sep 17 00:00:00 2001 From: Michael Krahl Date: Thu, 18 Nov 2021 18:01:54 -0700 Subject: [PATCH 2/5] Work from Thursday. More practice with classes and tests --- endpoints/Question/alternatives.py | 13 ++++++ endpoints/Question/question.py | 13 ++++++ mtest/test_get_alternatives.py | 63 ++++++++++++++++++++++++++++++ mtest/test_get_question.py | 59 ++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 endpoints/Question/alternatives.py create mode 100644 endpoints/Question/question.py create mode 100644 mtest/test_get_alternatives.py create mode 100644 mtest/test_get_question.py diff --git a/endpoints/Question/alternatives.py b/endpoints/Question/alternatives.py new file mode 100644 index 0000000..daf6286 --- /dev/null +++ b/endpoints/Question/alternatives.py @@ -0,0 +1,13 @@ +import requests + + +class Alternatives: + + def __init__(self): + self.url = "http://127.0.0.1:8000/alternatives" + self.headers = {"accept": "application/json"} + + def get_alternatives(self, question_id: int): + url = f"{self.url}/{question_id}" + response = requests.get(url, headers=self.headers) + return response diff --git a/endpoints/Question/question.py b/endpoints/Question/question.py new file mode 100644 index 0000000..4c83395 --- /dev/null +++ b/endpoints/Question/question.py @@ -0,0 +1,13 @@ +import requests + + +class Question: + + def __init__(self): + self.url = "http://127.0.0.1:8000/question" + self.headers = {"accept": "application/json"} + + def get_question(self, position: int): + url = f"{self.url}/{position}" + response = requests.get(url, headers=self.headers) + return response diff --git a/mtest/test_get_alternatives.py b/mtest/test_get_alternatives.py new file mode 100644 index 0000000..1f7339c --- /dev/null +++ b/mtest/test_get_alternatives.py @@ -0,0 +1,63 @@ +from endpoints.Question.alternatives import Alternatives + + +def test_get_alternatives_1(): + alternatives = Alternatives() + response = alternatives.get_alternatives(question_id=1) + assert response.status_code == 200 + assert isinstance( + response.json(), list + ), f".get_question() returned unexpected json: {response}" + print(response.json()) + assert response.json()[0]["id"] == 1 and response.json()[0]["alternative"] == "compact" + assert response.json()[0]["question_id"] == 1 + assert response.json()[1]["alternative"] == "utilitary" + assert response.json()[2]["alternative"] == "sporting" + assert response.json()[3]["alternative"] == "suv" + + +def test_get_question_position_2(): + alternatives = Alternatives() + response = alternatives.get_alternatives(question_id=2) + assert response.status_code == 200 + assert isinstance( + response.json(), list + ), f".get_question() returned unexpected json: {response}" + assert response.json()[0]["id"] == 5 and response.json()[0]["alternative"] == "low" + assert response.json()[1]["question_id"] == 2 and response.json()[1]["id"] == 6 + + +def test_get_question_negative(): + alternatives = Alternatives() + response = alternatives.get_alternatives(question_id=-1) + assert response.status_code == 400 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + + +def test_get_question_large(): + alternatives = Alternatives() + response = alternatives.get_alternatives(question_id=1000) + assert response.status_code == 400 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + + +def test_get_question_string(): + alternatives = Alternatives() + response = alternatives.get_alternatives(question_id="foo") + assert response.status_code == 422 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + + +def test_get_question_none(): + alternatives = Alternatives() + response = alternatives.get_alternatives(question_id=None) + assert response.status_code == 422 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" diff --git a/mtest/test_get_question.py b/mtest/test_get_question.py new file mode 100644 index 0000000..7d6bf74 --- /dev/null +++ b/mtest/test_get_question.py @@ -0,0 +1,59 @@ +from endpoints.Question.question import Question + + +def test_get_question_position_1(): + questions = Question() + response = questions.get_question(position=1) + assert response.status_code == 200 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + assert response.json()["id"] == 1 and response.json()["position"] == 1 + assert response.json()["question"] == "Which car model/category are you looking for?" + + +def test_get_question_position_2(): + questions = Question() + response = questions.get_question(position=2) + assert response.status_code == 200 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + assert response.json()["id"] == 3 and response.json()["position"] == 2 + assert response.json()["question"] == "What type of fuel is your ideal car?" + + +def test_get_question_negative(): + questions = Question() + response = questions.get_question(position=-1) + assert response.status_code == 400 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + + +def test_get_question_large(): + questions = Question() + response = questions.get_question(position=1000) + assert response.status_code == 400 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + + +def test_get_question_string(): + questions = Question() + response = questions.get_question(position="foo") + assert response.status_code == 422 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" + + +def test_get_question_none(): + questions = Question() + response = questions.get_question(position=None) + assert response.status_code == 422 + assert isinstance( + response.json(), dict + ), f".get_question() returned unexpected json: {response}" From 475e67fc18cf1ae98ab122019853ef8c12a3de5b Mon Sep 17 00:00:00 2001 From: Michael Krahl Date: Thu, 18 Nov 2021 18:03:25 -0700 Subject: [PATCH 3/5] More files to add. More practice with classes and tests --- endpoints/User/users.py | 12 ++++++++++++ endpoints/get_root.py | 9 +++++++++ endpoints/get_user.py | 10 ++++++++++ mtest/test_get_root.py | 7 +++++++ mtest/test_get_user.py | 12 ++++++++++++ 5 files changed, 50 insertions(+) diff --git a/endpoints/User/users.py b/endpoints/User/users.py index e69de29..758fa9c 100644 --- a/endpoints/User/users.py +++ b/endpoints/User/users.py @@ -0,0 +1,12 @@ +import requests + + +class User: + + def __init__(self): + self.url = "http://127.0.0.1:8000/user" + self.headers = {"accept": "application/json"} + + def get_user(self): + response = requests.get(self.url, headers=self.headers) + return response diff --git a/endpoints/get_root.py b/endpoints/get_root.py index e69de29..0bd7108 100644 --- a/endpoints/get_root.py +++ b/endpoints/get_root.py @@ -0,0 +1,9 @@ +import requests + +URL = "http://127.0.0.1:8000/" +headers = {"accept": "application/json"} + + +def get_root(params: dict = None): + response = requests.get(URL, headers=headers, params=params) + return response diff --git a/endpoints/get_user.py b/endpoints/get_user.py index e69de29..d081623 100644 --- a/endpoints/get_user.py +++ b/endpoints/get_user.py @@ -0,0 +1,10 @@ +import requests + +URL = "http://127.0.0.1:8000/" +headers = {"accept": "application/json"} + + +def get_user(params: dict = None): + url = URL + "user" + response = requests.get(url, headers=headers, params=params) + return response diff --git a/mtest/test_get_root.py b/mtest/test_get_root.py index e69de29..8b13f8d 100644 --- a/mtest/test_get_root.py +++ b/mtest/test_get_root.py @@ -0,0 +1,7 @@ +from endpoints.get_root import get_root + + +def test_get_root(): + response = get_root() + assert response.status_code == 200 + assert response.json()["message"] == "Fast API in Python" diff --git a/mtest/test_get_user.py b/mtest/test_get_user.py index e69de29..585de02 100644 --- a/mtest/test_get_user.py +++ b/mtest/test_get_user.py @@ -0,0 +1,12 @@ +from endpoints.User.users import User + + +def test_get_user(): + user = User() + response = user.get_user() + assert response.status_code == 200 + assert isinstance( + response.json(), list + ), f".get_user() returned unexpected json: {response}" + assert response.json()[0]["id"] == 1 and response.json()[0]["name"] == "Márcio" + assert response.json()[1]["id"] == 2 and response.json()[1]["name"] == "Leandro" From eb56f59cd7ffe74466d9082e0756c40485420aea Mon Sep 17 00:00:00 2001 From: Michael Krahl Date: Sat, 20 Nov 2021 10:52:19 -0700 Subject: [PATCH 4/5] Made some more updates. Some of the tests were wrong..expecting dictionaries when they really should have expected a list --- endpoints/Question/__init__.py | 0 endpoints/User/__init__.py | 0 mtest/__init__.py | 0 mtest/test_get_alternatives.py | 20 ++++++++++---------- 4 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 endpoints/Question/__init__.py create mode 100644 endpoints/User/__init__.py create mode 100644 mtest/__init__.py diff --git a/endpoints/Question/__init__.py b/endpoints/Question/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/endpoints/User/__init__.py b/endpoints/User/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mtest/__init__.py b/mtest/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mtest/test_get_alternatives.py b/mtest/test_get_alternatives.py index 1f7339c..7d57550 100644 --- a/mtest/test_get_alternatives.py +++ b/mtest/test_get_alternatives.py @@ -7,7 +7,7 @@ def test_get_alternatives_1(): assert response.status_code == 200 assert isinstance( response.json(), list - ), f".get_question() returned unexpected json: {response}" + ), f".get_question_alternatives_1() returned unexpected json: {response.json()}" print(response.json()) assert response.json()[0]["id"] == 1 and response.json()[0]["alternative"] == "compact" assert response.json()[0]["question_id"] == 1 @@ -22,7 +22,7 @@ def test_get_question_position_2(): assert response.status_code == 200 assert isinstance( response.json(), list - ), f".get_question() returned unexpected json: {response}" + ), f".get_question_position_2() returned unexpected json: {response.json()}" assert response.json()[0]["id"] == 5 and response.json()[0]["alternative"] == "low" assert response.json()[1]["question_id"] == 2 and response.json()[1]["id"] == 6 @@ -30,19 +30,19 @@ def test_get_question_position_2(): def test_get_question_negative(): alternatives = Alternatives() response = alternatives.get_alternatives(question_id=-1) - assert response.status_code == 400 + assert response.status_code == 200 assert isinstance( - response.json(), dict - ), f".get_question() returned unexpected json: {response}" + response.json(), list + ), f".get_question_negative() returned unexpected json: {response.json()}" def test_get_question_large(): alternatives = Alternatives() response = alternatives.get_alternatives(question_id=1000) - assert response.status_code == 400 + assert response.status_code == 200 assert isinstance( - response.json(), dict - ), f".get_question() returned unexpected json: {response}" + response.json(), list + ), f".get_question_large() returned unexpected json: {response.json()}" def test_get_question_string(): @@ -51,7 +51,7 @@ def test_get_question_string(): assert response.status_code == 422 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question_string() returned unexpected json: {response.json()}" def test_get_question_none(): @@ -60,4 +60,4 @@ def test_get_question_none(): assert response.status_code == 422 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question_none() returned unexpected json: {response.json()}" From 5d73bf9756e3f4c1be87034f2fc66ac508f15d02 Mon Sep 17 00:00:00 2001 From: Michael Krahl Date: Sat, 20 Nov 2021 10:57:56 -0700 Subject: [PATCH 5/5] Cleaned up other tests. Error response should include the propr context instead of just the response code --- mtest/test_get_question.py | 12 ++++++------ mtest/test_get_user.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mtest/test_get_question.py b/mtest/test_get_question.py index 7d6bf74..5069dd7 100644 --- a/mtest/test_get_question.py +++ b/mtest/test_get_question.py @@ -7,7 +7,7 @@ def test_get_question_position_1(): assert response.status_code == 200 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question() returned unexpected json: {response.json()}" assert response.json()["id"] == 1 and response.json()["position"] == 1 assert response.json()["question"] == "Which car model/category are you looking for?" @@ -18,7 +18,7 @@ def test_get_question_position_2(): assert response.status_code == 200 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question() returned unexpected json: {response.json()}" assert response.json()["id"] == 3 and response.json()["position"] == 2 assert response.json()["question"] == "What type of fuel is your ideal car?" @@ -29,7 +29,7 @@ def test_get_question_negative(): assert response.status_code == 400 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question() returned unexpected json: {response.json()}" def test_get_question_large(): @@ -38,7 +38,7 @@ def test_get_question_large(): assert response.status_code == 400 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question() returned unexpected json: {response.json()}" def test_get_question_string(): @@ -47,7 +47,7 @@ def test_get_question_string(): assert response.status_code == 422 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question() returned unexpected json: {response.json()}" def test_get_question_none(): @@ -56,4 +56,4 @@ def test_get_question_none(): assert response.status_code == 422 assert isinstance( response.json(), dict - ), f".get_question() returned unexpected json: {response}" + ), f".get_question() returned unexpected json: {response.json()}" diff --git a/mtest/test_get_user.py b/mtest/test_get_user.py index 585de02..778276f 100644 --- a/mtest/test_get_user.py +++ b/mtest/test_get_user.py @@ -7,6 +7,6 @@ def test_get_user(): assert response.status_code == 200 assert isinstance( response.json(), list - ), f".get_user() returned unexpected json: {response}" + ), f".get_user() returned unexpected json: {response.json()}" assert response.json()[0]["id"] == 1 and response.json()[0]["name"] == "Márcio" assert response.json()[1]["id"] == 2 and response.json()[1]["name"] == "Leandro"