Skip to content

Commit

Permalink
Merge pull request #90 from computas/SP24_714_rewrite_tests_websockets
Browse files Browse the repository at this point in the history
Sp24 714 rewrite tests websockets
  • Loading branch information
Ivan-Computas authored Aug 20, 2024
2 parents 0dcd27b + 41ba6f8 commit 9dd5aa2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
9 changes: 4 additions & 5 deletions src/test/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from webapp import models
from utilities.exceptions import UserError


class TestValues:
PLAYER_ID = uuid.uuid4().hex
PLAYER_2 = uuid.uuid4().hex
Expand All @@ -45,9 +46,9 @@ def test_insert_into_games():
result = models.insert_into_games(
TestValues.GAME_ID, TestValues.LABELS, TestValues.TODAY, DifficultyId.Easy
)

assert result


def test_insert_into_players():
"""
Check that record exists in Players table after inserting.
Expand All @@ -65,14 +66,12 @@ def test_insert_into_scores():
Check that records exists in Scores table after inserting.
"""
with api.app.app_context():

#models.insert_into_players(TestValues.PLAYER_ID, TestValues.GAME_ID, TestValues.STATE)

result = models.insert_into_scores(
TestValues.PLAYER_ID, 500, TestValues.TODAY, DifficultyId.Easy)

assert result


def test_insert_into_multiplayer():
"""
Check that record exists in MultiPlayer after inserting.
Expand Down Expand Up @@ -226,4 +225,4 @@ def test_get_iteration_name_length():
with api.app.app_context():
iteration_name = models.get_iteration_name()

assert iteration_name == TestValues.CV_ITERATION_NAME
assert iteration_name == TestValues.CV_ITERATION_NAME
75 changes: 40 additions & 35 deletions src/test/websocket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import json
import tempfile
import werkzeug
import sys
from webapp.api import app, socketio
import os

utilities_directory = sys.path[0].replace("/test", "")
sys.path.insert(0, utilities_directory)

from webapp.api import app, socketio
current_path = os.getcwd()
parent_path = os.path.dirname(current_path)
HARAMBE_PATH = os.path.join(parent_path, 'data/harambe.png')

HARAMBE_PATH = "data/harambe.png"
mock_classifier = MagicMock()
mock_classifier.predict_image_by_post = MagicMock(
return_value=({"angel": 1}, "angel"))
Expand All @@ -37,9 +35,11 @@ def test_clients():
)
""" response = flask_client.get("/")
assert response.status_code == 200 """

yield flask_client, test_client1, test_client2

test_client1.disconnect()
test_client2.disconnect()


@pytest.fixture
def four_test_clients():
Expand All @@ -60,6 +60,11 @@ def four_test_clients():

yield flask_client, test_client1, test_client2, test_client3, test_client4

test_client1.disconnect()
test_client2.disconnect()
test_client3.disconnect()
test_client4.disconnect()


@pytest.mark.parametrize('data', [
(''),
Expand All @@ -70,6 +75,7 @@ def test_join_game_same_pair_id(test_clients, data, ):
"""
_, ws_client1, ws_client2 = test_clients

data = '{"difficulty_id": 1}'
ws_client1.emit("joinGame", data)

r1 = ws_client1.get_received()
Expand All @@ -94,8 +100,8 @@ def test_join_game_diff_pair_id(four_test_clients):
tests wether a player is able to join game
"""
_, ws_client1_1, ws_client1_2, ws_client2_1, ws_client2_2 = four_test_clients
data_1 = '{"pair_id": "pair_id_1"}'
data_2 = '{"pair_id": "pair_id_2"}'
data_1 = '{"pair_id": "pair_id_1","difficulty_id": 1}'
data_2 = '{"pair_id": "pair_id_2","difficulty_id": 1}'

# the first player is added to both games
ws_client1_1.emit("joinGame", data_1)
Expand Down Expand Up @@ -146,14 +152,13 @@ def test_classification_only_client1_correct(test_clients):
correct_label = "angel"
wrong_label = "bicycle"
_, ws_client1, ws_client2 = test_clients
ws_client1.emit("joinGame", '{"pair_id": "classify"}')
ws_client2.emit("joinGame", '{"pair_id": "classify"}')
ws_client1.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
ws_client2.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
args = r1[0]["args"][0]
game_id = args["game_id"]
data = {"game_id": game_id, "time_left": time_left}

data = {"game_id": game_id, "time_left": time_left, "lang": "NO"}
ws_client1.emit(
"classify",
data,
Expand Down Expand Up @@ -187,13 +192,13 @@ def test_classification_both_correct(test_clients):
time_left = 1
correct_label = "angel"
_, ws_client1, ws_client2 = test_clients
ws_client1.emit("joinGame", '{"pair_id": "classify"}')
ws_client2.emit("joinGame", '{"pair_id": "classify"}')
ws_client1.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
ws_client2.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
args = r1[0]["args"][0]
game_id = args["game_id"]
data = {"game_id": game_id, "time_left": time_left}
data = {"game_id": game_id, "time_left": time_left, "lang": "NO"}

ws_client1.emit(
"classify",
Expand Down Expand Up @@ -237,14 +242,14 @@ def test_classification_client1_timeout_and_client2_correct(test_clients):
time_left = 1
correct_label = "angel"
_, ws_client1, ws_client2 = test_clients
ws_client1.emit("joinGame", '{"pair_id": "classify"}')
ws_client2.emit("joinGame", '{"pair_id": "classify"}')
ws_client1.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
ws_client2.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
args = r1[0]["args"][0]
game_id = args["game_id"]

data1 = {"game_id": game_id, "time_left": time_out}
data1 = {"game_id": game_id, "time_left": time_out, "lang": "NO"}
ws_client1.emit(
"classify",
data1,
Expand All @@ -254,7 +259,7 @@ def test_classification_client1_timeout_and_client2_correct(test_clients):
assert ws_client1.get_received() == []
assert ws_client2.get_received() == []

data2 = {"game_id": game_id, "time_left": time_left}
data2 = {"game_id": game_id, "time_left": time_left, "lang": "NO"}
ws_client2.emit(
"classify",
data2,
Expand Down Expand Up @@ -282,14 +287,14 @@ def test_classification_client1_correct_and_client2_timeout(test_clients):
time_left = 1
correct_label = "angel"
_, ws_client1, ws_client2 = test_clients
ws_client1.emit("joinGame", '{"pair_id": "classify"}')
ws_client2.emit("joinGame", '{"pair_id": "classify"}')
ws_client1.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
ws_client2.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
args = r1[0]["args"][0]
game_id = args["game_id"]

data1 = {"game_id": game_id, "time_left": time_left}
data1 = {"game_id": game_id, "time_left": time_left, "lang": "NO"}
ws_client1.emit(
"classify",
data1,
Expand All @@ -305,7 +310,7 @@ def test_classification_client1_correct_and_client2_timeout(test_clients):
assert len(r1) == 1
assert ws_client2.get_received() == []

data2 = {"game_id": game_id, "time_left": time_out}
data2 = {"game_id": game_id, "time_left": time_out, "lang": "NO"}
ws_client2.emit(
"classify",
data2,
Expand All @@ -327,14 +332,14 @@ def test_classification_both_timeout(test_clients):
time_out = 0
correct_label = "angel"
_, ws_client1, ws_client2 = test_clients
ws_client1.emit("joinGame", '{"pair_id": "classify"}')
ws_client2.emit("joinGame", '{"pair_id": "classify"}')
ws_client1.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
ws_client2.emit("joinGame", '{"pair_id": "classify","difficulty_id": 1}')
r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
args = r1[0]["args"][0]
game_id = args["game_id"]

data1 = {"game_id": game_id, "time_left": time_out}
data1 = {"game_id": game_id, "time_left": time_out, "lang": "NO"}
ws_client1.emit(
"classify",
data1,
Expand All @@ -344,7 +349,7 @@ def test_classification_both_timeout(test_clients):
assert ws_client1.get_received() == []
assert ws_client2.get_received() == []

data2 = {"game_id": game_id, "time_left": time_out}
data2 = {"game_id": game_id, "time_left": time_out, "lang": "NO"}
ws_client2.emit(
"classify",
data2,
Expand All @@ -365,8 +370,8 @@ def test_players_not_with_same_playerid(test_clients):
"""TODO: implement me"""
_, ws_client1, ws_client2 = test_clients

ws_client1.emit("joinGame", '')
ws_client2.emit("joinGame", '')
ws_client1.emit("joinGame", '{"difficulty_id": 1}')
ws_client2.emit("joinGame", '{"difficulty_id": 1}')

r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
Expand All @@ -376,14 +381,14 @@ def test_players_not_with_same_playerid(test_clients):

def test_players_can_keep_guessing(test_clients):
_, ws_client1, ws_client2 = test_clients
ws_client1.emit("joinGame", '')
ws_client2.emit("joinGame", '')
ws_client1.emit("joinGame", '{"difficulty_id": 1}')
ws_client2.emit("joinGame", '{"difficulty_id": 1}')
r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
game_id = r1[0]["args"][0]["game_id"]

for i in range(3):
data = {"game_id": game_id, "time_left": 1}
data = {"game_id": game_id, "time_left": 1, "lang": "NO"}

ws_client1.emit("classify", data, _get_image_as_stream(HARAMBE_PATH))
ws_client2.emit("classify", data, _get_image_as_stream(HARAMBE_PATH))
Expand All @@ -396,8 +401,8 @@ def test_players_can_keep_guessing(test_clients):

def test_end_game(test_clients):
_, ws_client1, ws_client2 = test_clients
ws_client1.emit("joinGame", '')
ws_client2.emit("joinGame", '')
ws_client1.emit("joinGame", '{"difficulty_id": 1}')
ws_client2.emit("joinGame", '{"difficulty_id": 1}')
r1 = ws_client1.get_received()
r2 = ws_client2.get_received()
player_1_id = r1[0]["args"][0]["player_id"]
Expand Down
5 changes: 4 additions & 1 deletion src/webapp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def handle_joinGame(json_data):

data = json.loads(json_data or 'null')
try:
pair_id = data["pair_id"]
difficulty_id = data["difficulty_id"]
pair_id = data["pair_id"]
except (KeyError, TypeError):
pair_id = ''
app.logger.error("No pair id for " + request.sid)
Expand Down Expand Up @@ -215,14 +215,17 @@ def get_example_drawings(json_data, emitEndpoint="getExampleDrawings"):
example_drawing_urls)
emit(emitEndpoint, json.dumps(example_drawings), room=game_id)


@socketio.on("getExampleDrawingsP1")
def get_example_drawings_player_1(json_data):
get_example_drawings(json_data, emitEndpoint="getExampleDrawingsP1")


@socketio.on("getExampleDrawingsP2")
def get_example_drawings_player_2(json_data):
get_example_drawings(json_data, emitEndpoint="getExampleDrawingsP2")


@socketio.on("classify")
def handle_classify(data, image, correct_label=None):
"""
Expand Down
4 changes: 1 addition & 3 deletions src/webapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ def insert_into_scores(player_id, score, date, difficulty_id: DifficultyId):
and isinstance(difficulty_id, int)
):
try:
scores = Scores(player_id=player_id, score=score,
date=date, difficulty_id=difficulty_id)
scores = Scores(player_id=player_id, score=score, date=date, difficulty_id=difficulty_id)
db.session.add(scores)
db.session.commit()
return True
Expand Down Expand Up @@ -460,7 +459,6 @@ def get_top_n_high_score_list(top_n, difficulty_id):
Scores.query.filter_by(difficulty_id=difficulty_id).order_by(
Scores.score.desc()).limit(top_n).all()
)

new = [
{"id": score.score_id, "score": score.score}
for score in top_n_list
Expand Down

0 comments on commit 9dd5aa2

Please sign in to comment.