From ebc9b784b8865ef4e5bfa4c8332aeeede9dc39de Mon Sep 17 00:00:00 2001 From: ivaj Date: Tue, 20 Aug 2024 12:35:52 +0200 Subject: [PATCH 1/4] Add difficulty id to websockets tests --- src/test/websocket_test.py | 29 +++++++++++++++-------------- src/webapp/api.py | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/test/websocket_test.py b/src/test/websocket_test.py index d930a29..7372997 100644 --- a/src/test/websocket_test.py +++ b/src/test/websocket_test.py @@ -70,6 +70,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() @@ -94,8 +95,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) @@ -146,8 +147,8 @@ 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] @@ -187,8 +188,8 @@ 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] @@ -237,8 +238,8 @@ 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] @@ -282,8 +283,8 @@ 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] @@ -327,8 +328,8 @@ 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] @@ -396,8 +397,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"] diff --git a/src/webapp/api.py b/src/webapp/api.py index 71a396e..5fca089 100644 --- a/src/webapp/api.py +++ b/src/webapp/api.py @@ -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) From 5de739b6e549e5dbc7899f322e328adc20dc862f Mon Sep 17 00:00:00 2001 From: ivaj Date: Tue, 20 Aug 2024 13:06:57 +0200 Subject: [PATCH 2/4] Add language to websockets tests --- src/test/websocket_test.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/test/websocket_test.py b/src/test/websocket_test.py index 7372997..372918b 100644 --- a/src/test/websocket_test.py +++ b/src/test/websocket_test.py @@ -11,15 +11,14 @@ 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) +current_path = os.getcwd() +parent_path = os.path.dirname(current_path) -from webapp.api import app, socketio +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")) @@ -153,8 +152,7 @@ def test_classification_only_client1_correct(test_clients): 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, @@ -194,7 +192,7 @@ def test_classification_both_correct(test_clients): 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", @@ -245,7 +243,7 @@ def test_classification_client1_timeout_and_client2_correct(test_clients): 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, @@ -255,7 +253,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, @@ -290,7 +288,7 @@ def test_classification_client1_correct_and_client2_timeout(test_clients): 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, @@ -306,7 +304,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, @@ -335,7 +333,7 @@ def test_classification_both_timeout(test_clients): 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, @@ -345,7 +343,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, @@ -366,8 +364,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() @@ -377,14 +375,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)) From e4a6c07e0ee59c43b9618e7bfaaddcef93420fb6 Mon Sep 17 00:00:00 2001 From: ivaj Date: Tue, 20 Aug 2024 13:31:31 +0200 Subject: [PATCH 3/4] clean up after testing is done --- src/test/websocket_test.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test/websocket_test.py b/src/test/websocket_test.py index 372918b..75c14df 100644 --- a/src/test/websocket_test.py +++ b/src/test/websocket_test.py @@ -16,7 +16,6 @@ current_path = os.getcwd() parent_path = os.path.dirname(current_path) - HARAMBE_PATH = os.path.join(parent_path, 'data/harambe.png') mock_classifier = MagicMock() @@ -39,6 +38,8 @@ def test_clients(): yield flask_client, test_client1, test_client2 + test_client1.disconnect() + test_client2.disconnect() @pytest.fixture def four_test_clients(): @@ -59,6 +60,10 @@ 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', [ (''), From 41ba6f8af8f52d406f827f3c6dbec196ff886f12 Mon Sep 17 00:00:00 2001 From: ivaj Date: Tue, 20 Aug 2024 13:47:49 +0200 Subject: [PATCH 4/4] clean linter faults --- src/test/test_db.py | 9 ++++----- src/test/websocket_test.py | 3 ++- src/webapp/api.py | 3 +++ src/webapp/models.py | 4 +--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/test/test_db.py b/src/test/test_db.py index 65d612d..88c798f 100644 --- a/src/test/test_db.py +++ b/src/test/test_db.py @@ -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 @@ -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. @@ -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. @@ -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 \ No newline at end of file + assert iteration_name == TestValues.CV_ITERATION_NAME diff --git a/src/test/websocket_test.py b/src/test/websocket_test.py index 75c14df..81494e9 100644 --- a/src/test/websocket_test.py +++ b/src/test/websocket_test.py @@ -35,12 +35,12 @@ 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(): app.config["TESTING"] = True @@ -65,6 +65,7 @@ def four_test_clients(): test_client3.disconnect() test_client4.disconnect() + @pytest.mark.parametrize('data', [ (''), ('{"pair_id": "same_pair_id"}')]) diff --git a/src/webapp/api.py b/src/webapp/api.py index 5fca089..7506263 100644 --- a/src/webapp/api.py +++ b/src/webapp/api.py @@ -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): """ diff --git a/src/webapp/models.py b/src/webapp/models.py index 71066fe..2de6df3 100644 --- a/src/webapp/models.py +++ b/src/webapp/models.py @@ -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 @@ -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