diff --git a/.github/workflows/deploy-dev-environment.yml b/.github/workflows/deploy-dev-environment.yml index e899e78..01ddd63 100644 --- a/.github/workflows/deploy-dev-environment.yml +++ b/.github/workflows/deploy-dev-environment.yml @@ -31,6 +31,8 @@ jobs: run: pip install -r requirements.txt # Optional: Add step to run tests here (PyTest, Django test suites, etc.) + # - name: Run tests + # run: cd src && python3 runTests.py --keys='${{ secrets.TEST_SETTINGS }}' - name: Zip artifact for deployment run: zip release.zip ./* -r diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..e3a7cf2 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +testpaths = src/test +pythonpath = src \ No newline at end of file diff --git a/src/runTests.py b/src/runTests.py index 6f06726..a124a25 100644 --- a/src/runTests.py +++ b/src/runTests.py @@ -18,11 +18,12 @@ ARGUMENTS = parser.parse_args() # export keys KEYLIST = json.loads(ARGUMENTS.keys) +print(KEYLIST) for keydict in KEYLIST: os.environ[keydict["name"]] = keydict["value"] # clear cli arguments as pytest reads them sys.argv = [sys.argv[0]] # run pytets -TEST_RESULT = pytest.main() +TEST_RESULT = pytest.main(["tests/"]) assert TEST_RESULT == 0 diff --git a/src/test/test_db.py b/src/test/test_db.py index 08a1aa3..65d612d 100644 --- a/src/test/test_db.py +++ b/src/test/test_db.py @@ -7,19 +7,24 @@ import uuid import datetime from pytest import raises +import sys +import os + +#current_directory = os.getcwd() +utilities_directory = sys.path[0].replace("/test", "") +sys.path.insert(0, utilities_directory) from utilities.difficulties import DifficultyId from webapp import api from webapp import models from utilities.exceptions import UserError - class TestValues: PLAYER_ID = uuid.uuid4().hex PLAYER_2 = uuid.uuid4().hex GAME_ID = uuid.uuid4().hex TODAY = datetime.datetime.today() - CV_ITERATION_NAME_LENGTH = 36 + CV_ITERATION_NAME = "Iteration5" LABELS = "label1, label2, label3" STATE = "Ready" @@ -38,39 +43,39 @@ def test_insert_into_games(): """ with api.app.app_context(): result = models.insert_into_games( - TestValues.GAME_ID, TestValues.LABELS, TestValues.TODAY + TestValues.GAME_ID, TestValues.LABELS, TestValues.TODAY, DifficultyId.Easy ) assert result - -def test_insert_into_scores(): +def test_insert_into_players(): """ - Check that records exists in Scores table after inserting. + Check that record exists in Players table after inserting. """ - easy_difficulty = DifficultyId.Easy with api.app.app_context(): - result = models.insert_into_scores( - "Test User", 500, TestValues.TODAY, easy_difficulty) + result = models.insert_into_players( + TestValues.PLAYER_ID, TestValues.GAME_ID, TestValues.STATE + ) assert result -def test_insert_into_players(): +def test_insert_into_scores(): """ - Check that record exists in Players table after inserting. + Check that records exists in Scores table after inserting. """ with api.app.app_context(): - result = models.insert_into_players( - TestValues.PLAYER_ID, TestValues.GAME_ID, TestValues.STATE - ) - assert result + #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_mulitplayer(): +def test_insert_into_multiplayer(): """ - Check that record exists in MulitPlayer after inserting. + Check that record exists in MultiPlayer after inserting. """ with api.app.app_context(): result = models.insert_into_mulitplayer( @@ -87,7 +92,7 @@ def test_illegal_parameter_games(): """ with raises(UserError): models.insert_into_games( - 10, ["label1", "label2", "label3"], "date_time" + 10, ["label1", "label2", "label3"], "date_time", DifficultyId.Easy ) @@ -128,7 +133,7 @@ def test_illegal_parameter_mulitplayer(): models.insert_into_mulitplayer(100, 200, 11) -def test_query_euqals_insert_games(): +def test_query_equals_insert_games(): """ Check that inserted record is the same as record catched by query. """ @@ -157,7 +162,7 @@ def test_delete_session_from_game(): game_id = uuid.uuid4().hex player_id = uuid.uuid4().hex with api.app.app_context(): - models.insert_into_games(game_id, TestValues.LABELS, TestValues.TODAY) + models.insert_into_games(game_id, TestValues.LABELS, TestValues.TODAY, DifficultyId.Medium) models.insert_into_players(player_id, game_id, "Waiting") models.insert_into_mulitplayer(game_id, player_id, None) result = models.delete_session_from_game(game_id) @@ -180,7 +185,7 @@ def test_get_n_labels_correct_size(): """ with api.app.app_context(): for i in range(5): - result = models.get_n_labels(i) + result = models.get_n_labels(i, DifficultyId.Hard) assert len(result) == i @@ -197,13 +202,13 @@ def test_to_norwegian_correct_translation(): Test that to_norwegian translates words correctly """ english_words = ["mermaid", "axe", "airplane"] - norwgian_words = ["havfrue", "øks", "fly"] + norwegian_words = ["havfrue", "øks", "fly"] with api.app.app_context(): for i in range(0, len(english_words)): translation = models.to_norwegian(english_words[i]) print(translation) - assert translation == norwgian_words[i] + assert translation == norwegian_words[i] def test_to_norwegian_illegal_parameter(): @@ -221,4 +226,4 @@ def test_get_iteration_name_length(): with api.app.app_context(): iteration_name = models.get_iteration_name() - assert len(iteration_name) == TestValues.CV_ITERATION_NAME_LENGTH + assert iteration_name == TestValues.CV_ITERATION_NAME \ No newline at end of file diff --git a/src/test/websocket_test.py b/src/test/websocket_test.py index 2bc60f1..d930a29 100644 --- a/src/test/websocket_test.py +++ b/src/test/websocket_test.py @@ -11,9 +11,13 @@ import json import tempfile import werkzeug +import sys +import os -from webapp.api import app, socketio +utilities_directory = sys.path[0].replace("/test", "") +sys.path.insert(0, utilities_directory) +from webapp.api import app, socketio HARAMBE_PATH = "data/harambe.png" mock_classifier = MagicMock() @@ -31,6 +35,9 @@ def test_clients(): test_client2 = socketio.test_client( app, flask_test_client=flask_client ) + """ response = flask_client.get("/") + assert response.status_code == 200 """ + yield flask_client, test_client1, test_client2 @@ -59,7 +66,7 @@ def four_test_clients(): ('{"pair_id": "same_pair_id"}')]) def test_join_game_same_pair_id(test_clients, data, ): """ - tests wether a player is able to join game + tests whether a player is able to join game """ _, ws_client1, ws_client2 = test_clients diff --git a/src/webapp/models.py b/src/webapp/models.py index 75e8c56..71066fe 100644 --- a/src/webapp/models.py +++ b/src/webapp/models.py @@ -187,9 +187,9 @@ def insert_into_scores(player_id, score, date, difficulty_id: DifficultyId): and isinstance(difficulty_id, int) ): try: - score = Scores(player_id=player_id, score=score, + scores = Scores(player_id=player_id, score=score, date=date, difficulty_id=difficulty_id) - db.session.add(score) + db.session.add(scores) db.session.commit() return True except Exception as e: @@ -371,7 +371,7 @@ def update_iteration_name(new_name): def delete_session_from_game(game_id): """ - To avoid unecessary data in the database this function is called by + To avoid unnecessary data in the database this function is called by the api after a session is finished. The record in games table, connected to the particular game_id, is deleted. """ @@ -448,7 +448,7 @@ def get_daily_high_score(difficulty_id): def get_top_n_high_score_list(top_n, difficulty_id): """ - Funtion for reading total top n list from database. + Function for reading total top n list from database. Parameter: top_n, number of players in top list. @@ -460,7 +460,7 @@ 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() ) - # strucutre data + new = [ {"id": score.score_id, "score": score.score} for score in top_n_list @@ -475,7 +475,7 @@ def get_top_n_high_score_list(top_n, difficulty_id): def to_norwegian(english_label): """ - Reads the labels tabel and return the norwegian translation of the + Reads the labels-table and return the norwegian translation of the english word. """ try: @@ -490,7 +490,7 @@ def to_norwegian(english_label): def to_english(norwegian_label): """ - Reads the labels table and return the english translation of the norwegian label + Reads the labels-table and return the english translation of the norwegian label """ try: query = Labels.query.filter(Labels.norwegian == norwegian_label)[0]