changed api client according to specification

bug fixes in VisionDaemon
This commit is contained in:
Joel 2021-02-10 03:22:55 +01:00
parent ce6b06544a
commit 5e46e8068a
No known key found for this signature in database
GPG key ID: BDDDBECD0808290E
6 changed files with 143 additions and 63 deletions

145
test.py
View file

@ -20,68 +20,141 @@ from multiprocessing import Process
from compLib import LogstashLogging
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 change_api_state(park_id: int = 0, in_get_park: bool = False, was_in_park: bool = False,
simon_says_ids: list = [0, 0, 0, 0, 0],
in_simon_zone: bool = False):
data = {
"parkId": park_id,
"inGetPark": in_get_park,
"wasInPark": was_in_park,
"simonSaysIDs": simon_says_ids,
"inSimonZone": in_simon_zone
}
requests.post("http://localhost:5000/api/api/updateState", json=data)
class TestApiServer(unittest.TestCase):
def test_get_park(self):
ret, code = Api.Seeding.get_park()
id = ret["id"]
assert type(ret) is dict
assert type(id) is int
assert id == -1
assert type(code) is int
assert code == 403
change_api_state(in_get_park=True)
ret, code = Api.Seeding.get_park()
id = ret["id"]
assert type(ret) is dict
assert type(id) is int
assert 0 <= id < 4
assert type(code) is int
assert code == 200
def test_pay_park(self):
ret = Api.Seeding.pay_park()
assert type(ret) is bool
assert type(ret) is int
assert ret == 402
change_api_state(was_in_park=True)
ret = Api.Seeding.pay_park()
assert type(ret) is int
assert ret == 402
change_api_state(was_in_park=True, in_get_park=True)
ret = Api.Seeding.pay_park()
assert type(ret) is int
assert ret == 201
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()
change_api_state(in_simon_zone=False)
ret, code = Api.Seeding.simon_says()
id = ret["id"]
assert type(ret) is dict
assert type(id) is int
assert type(code) is int
assert code == 403
assert id == -1
assert True
got = []
while len(got) != 5:
change_api_state(in_simon_zone=True)
ret, code = Api.Seeding.simon_says()
id = ret["id"]
assert type(ret) is dict
assert type(id) is int
assert type(code) is int
assert code == 200
assert id <= 0 < 4
got.append(ret)
print(got)
change_api_state(in_simon_zone=True)
ret, code = Api.Seeding.simon_says()
id = ret["id"]
assert type(ret) is dict
assert type(id) is int
assert type(code) is int
assert code == 200
assert id == -1
change_api_state(in_simon_zone=False)
ret, code = Api.Seeding.simon_says()
id = ret["id"]
assert type(ret) is dict
assert type(id) is int
assert type(code) is int
assert code == 403
assert id == -1
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
for i in range(0, 5):
change_api_state(in_simon_zone=True)
ret, status = Api.Seeding.simon_says()
assert type(ret) is dict
assert type(status) is int
assert type(ret["id"]) is int
assert 0 <= ret["id"] < 4
# after 4 iterations the api should only return -1 from now
for i in range(0, 10):
assert Api.Seeding.simon_says() == -1
assert Api.Seeding.simon_says()[0]["id"] == -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
change_api_state(in_simon_zone=True)
ret, status = Api.Seeding.simon_says()
assert type(ret) is dict
assert type(status) is int
assert type(ret["id"]) is int
assert 0 <= ret["id"] < 4
for i in range(0, 10):
assert Api.Seeding.simon_says() == -1
assert Api.Seeding.simon_says()[0]["id"] == -1
def test_simon_says_non_reapet(self):
def test_simon_says_non_repeat(self):
"""
Note: this test will fail if testing against the local api as the
configuration is to return zeros if not otherwise specified.
Checks if simons says does not send the robot to the
same position again.
"""
last_pos = Api.Seeding.simon_says()
last_pos, status = Api.Seeding.simon_says()
last_pos = last_pos["id"]
for i in range(0, 100):
next_pos = Api.Seeding.simon_says()
change_api_state(in_simon_zone=True)
next_pos, status = Api.Seeding.simon_says()
next_pos = next_pos["id"]
if next_pos == -1:
last_pos = -1 # state is reset, so reset here as well
self.resetApi()