Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing #118

Merged
merged 2 commits into from
Oct 14, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
api tests
changes:
- tests for movement_controller fixed
- added pytest to requirements
- changed paths in server.main.py
- changed all str for run_time, start_run, and submission_time to datetime
- changed SubRunInfoBase to SubmissionRunWRun in submission_schema
- few minor changes

added:
- test file for server.main.py: test_main.py
MoodyMan04 committed Oct 14, 2023
commit 30a50c267740f84f12d910ffb1af0ffdbb2e5091
Binary file renamed server/byte_server.db → byte_server.db
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -22,37 +22,35 @@ class TestMovementControllerIfOccupiableStationIsOccupiable(unittest.TestCase):
def setUp(self) -> None:
self.movement_controller = MovementController()

self.avatar = Avatar(None, 1)

# (1, 0), (2, 0), (0, 1), (0, 2), (1, 3), (2, 3), (3, 1), (3, 2)
self.locations: dict = {
(Vector(1, 0), Vector(2, 0), Vector(0, 1), Vector(0, 2)): [OccupiableStation(None, None),
OccupiableStation(None, None),
OccupiableStation(None, None),
OccupiableStation(None, None)]}
OccupiableStation(None, None)],
(Vector(1, 1),): [self.avatar, ],
}
self.game_board = GameBoard(0, Vector(3, 3), self.locations, False)
self.occ_station = OccupiableStation()
self.occ_station = OccupiableStation()
# self.wall = Wall()
# test movements up, down, left and right by starting with default 3,3 then know if it changes from there \/
self.avatar = Avatar(None, Vector(1, 1), [], 1)
self.client = Player(None, None, [], self.avatar)
self.game_board.generate_map()

def test_move_up(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 0)))

def test_move_up(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 0)))


def test_move_down(self):
self.movement_controller.handle_actions(ActionType.MOVE_DOWN, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 2)))


def test_move_left(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(0, 1)))
def test_move_down(self):
self.movement_controller.handle_actions(ActionType.MOVE_DOWN, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 2)))

def test_move_left(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(0, 1)))

def test_move_right(self):
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 1)))
def test_move_right(self):
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 1)))
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ class TestMovementControllerIfOccupiableStations(unittest.TestCase):
def setUp(self) -> None:
self.movement_controller = MovementController()

self.avatar = Avatar(None, 10)

# (1, 0), (2, 0), (0, 1), (0, 2), (1, 3), (2, 3), (3, 1), (3, 2)
self.locations: dict = {(Vector(1, 0), Vector(2, 0), Vector(0, 1), Vector(0, 2), Vector(1, 3), Vector(2, 3),
Vector(3, 1), Vector(3, 2)): [OccupiableStation(None, Wall()),
@@ -31,58 +33,59 @@ def setUp(self) -> None:
OccupiableStation(None, Wall()),
OccupiableStation(None, Wall()),
OccupiableStation(None, Wall()),
OccupiableStation(None, Station())]}
OccupiableStation(None, Station())],
(Vector(2, 2),): [self.avatar, ]}
self.game_board = GameBoard(0, Vector(4, 4), self.locations, False)
self.occ_station = OccupiableStation()
self.occ_station = OccupiableStation()
# self.wall = Wall()
# test movements up, down, left and right by starting with default 3,3 then know if it changes from there \/
self.avatar = Avatar(None, Vector(2, 2), [], 1)

self.position = Vector(2, 2)
self.client = Player(None, None, [], self.avatar)
self.game_board.generate_map()

# it is not occupied, so you can move there


def test_move_up(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 1)))
def test_move_up(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 1)))


def test_move_up_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 1)))
def test_move_up_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.assertEqual(str(self.client.avatar.position), str(Vector(2, 1)))


def test_move_down(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_DOWN, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))
def test_move_down(self):
self.movement_controller.handle_actions(ActionType.MOVE_UP, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_DOWN, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))


def test_move_down_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_DOWN, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))
def test_move_down_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_DOWN, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))


def test_move_left(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 2)))
def test_move_left(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 2)))


def test_move_left_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 2)))
def test_move_left_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(1, 2)))


def test_move_right(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))
def test_move_right(self):
self.movement_controller.handle_actions(ActionType.MOVE_LEFT, self.client, self.game_board)
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))


def test_move_right_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))
def test_move_right_fail(self):
self.movement_controller.handle_actions(ActionType.MOVE_RIGHT, self.client, self.game_board)
self.assertEqual((str(self.client.avatar.position)), str(Vector(2, 2)))
1 change: 0 additions & 1 deletion game/test_suite/tests/test_movement_controller_if_wall.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@ def setUp(self) -> None:
self.game_board = GameBoard(0, Vector(4, 4), self.locations, True)
self.station = Station()
self.occupiable_station = OccupiableStation()
self.occupiable_station = OccupiableStation()
self.wall = Wall()
# test movements up, down, left and right by starting with default 3,3 then know if it changes from there \/
self.client = Player(None, None, [], self.avatar)
Binary file added out.mp4
Binary file not shown.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
tqdm~=4.65.0
pygame~=2.3.0
pytest~=7.4.2
numpy~=1.24.2
opencv-python~=4.8.0.76
Sphinx~=7.0.1
9 changes: 6 additions & 3 deletions server/main.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,9 @@
from server.models.submission import Submission
from server.models.turn_table import TurnTable
from server.models.university import University
from server.models.group_teams import GroupTeams
from server.models.submission import Submission
from server.schemas.submission.submission_w_team import SubmissionWTeam

from server.schemas.submission.submission_schema import SubmissionSchema
from server.schemas.submission.submission_w_team import SubmissionWTeam
@@ -37,11 +39,12 @@ def root():
return {"message": "Hello World"}


@app.get('/get_submission/', response_model=SubmissionSchema)
def get_submission(submission: SubmissionWTeam, db: Session = Depends(get_db)):
@app.get('/get_submission/{submission_id}/{team_uuid}', response_model=SubmissionSchema)
def get_submission(submission_id: int, team_uuid: int, db: Session = Depends(get_db)):
# Retrieves a list of submissions where the submission id and uuids match
submission_list: list[Submission] | None = crud_submission.read_all_W_filter(
db, submission_id=submission.submission_id, team_id_uuid=submission.team_id_uuid)
db, submission_id=submission_id, team_uuid=team_uuid)
print('get_submission print')

if submission_list is None:
raise HTTPException(status_code=404, detail="Submission not found!")
2 changes: 1 addition & 1 deletion server/models/group_run.py
Original file line number Diff line number Diff line change
@@ -16,4 +16,4 @@ class GroupRun(Base):
is_finished: Mapped[bool] = mapped_column(Boolean(), default=False, nullable=False)

runs: Mapped[list['Run']] = relationship(back_populates='group_run')
group_teams: Mapped[list['GroupRun']] = relationship(back_populates='group_run')
group_teams: Mapped[list['GroupTeams']] = relationship(back_populates='group_run')
2 changes: 1 addition & 1 deletion server/models/group_teams.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@


class GroupTeams(Base):
__table_name__: str = 'group_teams'
__tablename__: str = 'group_teams'
group_teams_id: Mapped[int] = mapped_column(Integer(), primary_key=True, autoincrement=True)
team_uuid: Mapped[int] = mapped_column(Integer(), ForeignKey('team.team_uuid'))
group_run_id: Mapped[int] = mapped_column(Integer(), ForeignKey('group_run.group_run_id'))
3 changes: 2 additions & 1 deletion server/schemas/group_run/group_run_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pydantic import BaseModel
from datetime import datetime


class GroupRunBase(BaseModel):
group_run_id: int
start_run: str
start_run: datetime
launcher_version: str
runs_per_client: int
is_finished: bool
3 changes: 2 additions & 1 deletion server/schemas/run/run_base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from pydantic import BaseModel
from datetime import datetime


class RunBase(BaseModel):
run_id: int
group_run_id: int
run_time: str
run_time: datetime
seed: int

class Config:
3 changes: 2 additions & 1 deletion server/schemas/submission/submission_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pydantic import BaseModel
from datetime import datetime


class SubmissionBase(BaseModel):
submission_id: int
submission_time: str
submission_time: datetime
file_txt: str

class Config:
4 changes: 2 additions & 2 deletions server/schemas/submission/submission_schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from server.schemas.submission.submission_base import SubmissionBase
from server.schemas.submission_run_info.submission_run_info_base import SubmissionRunInfoBase
from server.schemas.submission_run_info.submission_run_info_w_run import SubmissionRunInfoWRun
from server.schemas.team.team_base import TeamBase


class SubmissionSchema(SubmissionBase):
team: TeamBase
submission_run_infos: list[SubmissionRunInfoBase]
submission_run_infos: list[SubmissionRunInfoWRun]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from server.schemas.run.run_base import RunBase
from submission_run_info_base import SubmissionRunInfoBase
from server.schemas.submission_run_info.submission_run_info_base import SubmissionRunInfoBase


class SubmissionRunInfoWRun(SubmissionRunInfoBase):
Empty file added server/unit_tests/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions server/unit_tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient
from server.main import app

client = TestClient(app=app)


def test_read_root():
response = client.get('/')
assert response.json() == {'message': 'Hello World'}


def test_read_get_submission():
response = client.get('/get_submission/1/1/')
print(response.json())
assert response.json() == {
'submission_id': 1,
'submission_time': '2000-10-31T01:30:00.000-05:00',
'file_txt': 'test',
'team_uuid': 1,
}