Skip to content

Commit

Permalink
Merge pull request #89 from computas/SP24-702-testing-workflows
Browse files Browse the repository at this point in the history
Sp24 702 testing workflows
  • Loading branch information
Ivan-Computas authored Aug 19, 2024
2 parents c1018cd + 922a037 commit 0dcd27b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy-dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = src/test
pythonpath = src
3 changes: 2 additions & 1 deletion src/runTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 29 additions & 24 deletions src/test/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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(
Expand All @@ -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
)


Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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


Expand All @@ -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():
Expand All @@ -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
11 changes: 9 additions & 2 deletions src/test/websocket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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


Expand Down Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions src/webapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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]
Expand Down

0 comments on commit 0dcd27b

Please sign in to comment.