finished api client implementation

added tests for api (compapi should run in the background or be installed)
This commit is contained in:
joel 2021-01-16 03:09:31 +01:00
parent c25715de40
commit 0e0a61d710
7 changed files with 162 additions and 16 deletions

5
.idea/.gitignore generated vendored
View file

@ -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/

7
.idea/compLIB.iml generated
View file

@ -2,13 +2,12 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/compLIB" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/compLib" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
<orderEntry type="library" name="R User Library" level="project" />
<orderEntry type="library" name="R Skeletons" level="application" />
</component>
</module>

View file

@ -1,6 +1,5 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
<option name="PROJECT_PROFILE" />
</settings>
</component>

2
.idea/misc.xml generated
View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7 (compLIB)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
</project>

1
.idea/vcs.xml generated
View file

@ -2,6 +2,5 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/docs/gh-pages" vcs="Git" />
</component>
</project>

View file

@ -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:

132
test.py Normal file
View file

@ -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()