finished api client implementation
added tests for api (compapi should run in the background or be installed)
This commit is contained in:
parent
c25715de40
commit
0e0a61d710
7 changed files with 162 additions and 16 deletions
132
test.py
Normal file
132
test.py
Normal 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()
|
Reference in a new issue