diff --git a/tests/api/test_exams.py b/tests/api/test_exams.py deleted file mode 100644 index 66eaf767b72dc8e871340ba3fe2a45f35f048ebd..0000000000000000000000000000000000000000 --- a/tests/api/test_exams.py +++ /dev/null @@ -1,41 +0,0 @@ -from flask import json - - -def mco_json(): - return { - 'x': 100, - 'y': 40, - 'problem_id': 1, - 'page': 1, - 'label': 'a', - 'name': 'test' - } - - -def test_get_exams(test_client): - mc_option_1 = { - 'x': 100, - 'y': 40, - 'problem_id': 3, - 'page': 1, - 'label': 'a', - 'name': 'test' - } - test_client.put('/api/mult-choice/', data=mc_option_1) - - mc_option_2 = { - 'x': 100, - 'y': 40, - 'problem_id': 3, - 'page': 1, - 'label': 'a', - 'name': 'test' - } - test_client.put('/api/mult-choice/', data=mc_option_2) - - response = test_client.get('/api/exams/3') - data = json.loads(response.data) - - print(data) - - assert len(data['problems'][0]['mc_options']) == 2 diff --git a/tests/conftest.py b/tests/conftest.py index e09388a98a14388f9d195f26d8a606e057d49d6d..0c373c16efa7c85627e2ba0efc09afa8b578987b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,63 +2,8 @@ import os import pytest -from zesje.api import api_bp -from zesje.database import db, Exam, Problem, ProblemWidget -from flask import Flask - # Adapted from https://stackoverflow.com/a/46062148/1062698 @pytest.fixture def datadir(): return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') - - -def add_test_data(): - exam1 = Exam(id=1, name='exam 1', finalized=False) - exam2 = Exam(id=2, name='exam 2', finalized=True) - exam3 = Exam(id=3, name='exam 1', finalized=False) - - db.session.add(exam1) - db.session.add(exam2) - db.session.add(exam3) - db.session.commit() - - problem1 = Problem(id=1, name='Problem 1', exam_id=1) - problem2 = Problem(id=2, name='Problem 2', exam_id=2) - problem3 = Problem(id=3, name='Problem 1', exam_id=3) - - db.session.add(problem1) - db.session.add(problem2) - db.session.add(problem3) - db.session.commit() - - problem_widget_1 = ProblemWidget(id=3, name='problem widget', problem_id=3, page=2, - width=100, height=150, x=40, y=200, type='problem_widget') - db.session.add(problem_widget_1) - db.session.commit() - - -@pytest.fixture(scope="module") -def app(): - app = Flask(__name__, static_folder=None) - - app.config.update( - SQLALCHEMY_DATABASE_URI='sqlite:///:memory:', - SQLALCHEMY_TRACK_MODIFICATIONS=False # Suppress future deprecation warning - ) - db.init_app(app) - - with app.app_context(): - db.create_all() - add_test_data() - - app.register_blueprint(api_bp, url_prefix='/api') - - return app - - -@pytest.fixture() -def test_client(app): - client = app.test_client() - - yield client diff --git a/tests/api/test_mult_choice.py b/tests/test_api.py similarity index 54% rename from tests/api/test_mult_choice.py rename to tests/test_api.py index 7b4cba1d0dd66db914be25a306fb1e417649ab49..a4e49eb92ba18508c9d4fc035276651518f5396d 100644 --- a/tests/api/test_mult_choice.py +++ b/tests/test_api.py @@ -1,4 +1,59 @@ -from flask import json +import pytest + +from flask import Flask, json +from zesje.api import api_bp +from zesje.database import db, Exam, Problem, ProblemWidget + + +def add_test_data(): + exam1 = Exam(id=1, name='exam 1', finalized=False) + exam2 = Exam(id=2, name='exam 2', finalized=True) + exam3 = Exam(id=3, name='exam 1', finalized=False) + + db.session.add(exam1) + db.session.add(exam2) + db.session.add(exam3) + db.session.commit() + + problem1 = Problem(id=1, name='Problem 1', exam_id=1) + problem2 = Problem(id=2, name='Problem 2', exam_id=2) + problem3 = Problem(id=3, name='Problem 1', exam_id=3) + + db.session.add(problem1) + db.session.add(problem2) + db.session.add(problem3) + db.session.commit() + + problem_widget_1 = ProblemWidget(id=3, name='problem widget', problem_id=3, page=2, + width=100, height=150, x=40, y=200, type='problem_widget') + db.session.add(problem_widget_1) + db.session.commit() + + +@pytest.fixture() +def app(): + app = Flask(__name__, static_folder=None) + + app.config.update( + SQLALCHEMY_DATABASE_URI='sqlite:///:memory:', + SQLALCHEMY_TRACK_MODIFICATIONS=False # Suppress future deprecation warning + ) + db.init_app(app) + + with app.app_context(): + db.create_all() + add_test_data() + + app.register_blueprint(api_bp, url_prefix='/api') + + return app + + +@pytest.fixture() +def test_client(app): + client = app.test_client() + + yield client def mco_json(): @@ -12,14 +67,19 @@ def mco_json(): } -def test_get_no_exam(test_client): +''' +MULTIPLE CHOICE OPTION TESTS +''' + + +def not_present(test_client): result = test_client.get('/api/mult-choice/1') data = json.loads(result.data) assert data['message'] == "Multiple choice question with id 1 does not exist." -def test_add_exam(test_client): +def test_add(test_client): req = mco_json() response = test_client.put('/api/mult-choice/', data=req) @@ -44,12 +104,12 @@ def test_add_get(test_client): data = json.loads(result.data) exp_resp = { - 'id': 5, + 'id': 4, 'name': 'test', 'x': 100, 'y': 40, 'type': 'mcq_widget', - 'feedback_id': 2, + 'feedback_id': 1, 'label': 'a', } @@ -118,3 +178,35 @@ def test_delete_finalized_exam(test_client): data = json.loads(response.data) assert data['status'] == 401 + + +''' +EXAM API TESTS +''' + + +def test_get_exams(test_client): + mc_option_1 = { + 'x': 100, + 'y': 40, + 'problem_id': 3, + 'page': 1, + 'label': 'a', + 'name': 'test' + } + test_client.put('/api/mult-choice/', data=mc_option_1) + + mc_option_2 = { + 'x': 100, + 'y': 40, + 'problem_id': 3, + 'page': 1, + 'label': 'a', + 'name': 'test' + } + test_client.put('/api/mult-choice/', data=mc_option_2) + + response = test_client.get('/api/exams/3') + data = json.loads(response.data) + + assert len(data['problems'][0]['mc_options']) == 2