From 0e0a61d710d7283b36a5ff4efce97eb69ccd89a2 Mon Sep 17 00:00:00 2001 From: joel Date: Sat, 16 Jan 2021 03:09:31 +0100 Subject: [PATCH] finished api client implementation added tests for api (compapi should run in the background or be installed) --- .idea/.gitignore | 5 + .idea/compLIB.iml | 7 +- .../inspectionProfiles/profiles_settings.xml | 3 +- .idea/misc.xml | 2 +- .idea/vcs.xml | 1 - compLib/Api.py | 28 ++-- test.py | 132 ++++++++++++++++++ 7 files changed, 162 insertions(+), 16 deletions(-) create mode 100644 test.py diff --git a/.idea/.gitignore b/.idea/.gitignore index 26d3352..73f69e0 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -1,3 +1,8 @@ # Default ignored files /shelf/ /workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compLIB.iml b/.idea/compLIB.iml index 79ef678..e2d46e8 100644 --- a/.idea/compLIB.iml +++ b/.idea/compLIB.iml @@ -2,13 +2,12 @@ + - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml index 105ce2d..0eefe32 100644 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -1,6 +1,5 @@ - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index abfc5f6..a2e120d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index be2ec28..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,6 +2,5 @@ - \ No newline at end of file diff --git a/compLib/Api.py b/compLib/Api.py index bd5fe7e..2492733 100644 --- a/compLib/Api.py +++ b/compLib/Api.py @@ -8,26 +8,38 @@ API_URL_GET_OP = API_URL + "getOp" API_URL_GET_GOAL = API_URL + "getGoal" API_URL_GET_ITEMS = API_URL + "getItems" +import json +import os + +API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/" +API_URL_GET_POS = API_URL + "getPos" +API_URL_GET_OP = API_URL + "getOp" +API_URL_GET_GOAL = API_URL + "getGoal" +API_URL_GET_ITEMS = API_URL + "getItems" +API_URL_GET_PARK = API_URL + "getPark" +API_URL_PAY_PARK = API_URL + "payPark" +API_URL_SIMON_SAYS = API_URL + "simonSays" + class Seeding: @staticmethod - def get_park(): - pass + def get_park() -> int: + return json.loads(requests.get(API_URL_GET_PARK).content)["id"] @staticmethod - def pay_park(): - pass + def pay_park() -> bool: + return requests.get(API_URL_PAY_PARK).status_code == 200 @staticmethod - def simon_says(): - pass + def simon_says() -> int: + return json.loads(requests.get(API_URL_SIMON_SAYS).content)["id"] class Position: - def __init__(self, x, y, rotation): + def __init__(self, x, y, degrees): self.x = x self.y = y - self.rotation = rotation + self.degrees = degrees class DoubleElim: diff --git a/test.py b/test.py new file mode 100644 index 0000000..a5de56b --- /dev/null +++ b/test.py @@ -0,0 +1,132 @@ +import json +import os +import time +import unittest +from threading import Thread + +import requests + +# compapi is a dependency for this test +try: + from compapi import server + + START_SERVER = True +except ImportError: + print("[!] error could not import server module from compapi, assuming server is running") + START_SERVER = False + +from multiprocessing import Process + +from compLib import Api + + +class MyTestCase(unittest.TestCase): + def test_get_park(self): + ret = Api.Seeding.get_park() + assert type(ret) is int + got = [] + while len(got) != 4: + ret = Api.Seeding.get_park() + assert 0 <= ret < 4 + if ret not in got: + got.append(ret) + + assert True + + def test_pay_park(self): + ret = Api.Seeding.pay_park() + assert type(ret) is bool + + def test_simon_says(self): + ret = Api.Seeding.simon_says() + assert type(ret) is int + got = [] + while len(got) != 4: + ret = Api.Seeding.simon_says() + if ret != -1: + assert 0 <= ret < 4 + if ret not in got: + got.append(ret) + else: + self.resetApi() + + assert True + + def test_simon_says_iterations(self): + for i in range(0, 4): + ret = Api.Seeding.simon_says() + assert type(ret) is int + assert 0 <= ret < 4 + + # after 4 iterations the api should only return -1 from now + for i in range(0, 10): + assert Api.Seeding.simon_says() == -1 + + # after api reset this test should work again + self.resetApi() + + for i in range(0, 4): + ret = Api.Seeding.simon_says() + assert type(ret) is int + assert 0 <= ret < 4 + + for i in range(0, 10): + assert Api.Seeding.simon_says() == -1 + + def test_simon_says_non_reapet(self): + """ + Checks if simons says does not send the robot to the + same position again. + """ + + last_pos = Api.Seeding.simon_says() + for i in range(0, 100): + next_pos = Api.Seeding.simon_says() + if next_pos == -1: + last_pos = -1 # state is reset, so reset here as well + self.resetApi() + continue + assert last_pos != next_pos + last_pos = next_pos + + def test_get_position(self): + response = Api.DoubleElim.get_position() + assert type(response) == Api.Position + assert 0 <= response.x + assert 0 <= response.y + assert 0 <= response.degrees <= 360 + + def test_get_opponent(self): + response = Api.DoubleElim.get_opponent() + assert type(response) == Api.Position + + def test_get_goal(self): + response = Api.DoubleElim.get_goal() + assert type(response) == Api.Position + assert 0 <= response.x + assert 0 <= response.y + assert response.degrees == -1 + + def test_get_items(self): + response = Api.DoubleElim.get_items() + assert type(response) == list + + def setUp(self) -> None: + if START_SERVER: + self.server = Process(target=server.app.run, kwargs={"host": "0.0.0.0", "port": "5000"}) + self.server.start() + time.sleep(0.25) + else: + self.resetApi() + + def tearDown(self) -> None: + if START_SERVER: + self.server.terminate() + self.server.join() + + def resetApi(self): + assert requests.get(Api.API_URL + "resetState").status_code == 200 + + +if __name__ == '__main__': + unittest.main()