Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • zesje/zesje
  • jbweston/grader_app
  • dj2k/zesje
  • MrHug/zesje
  • okaaij/zesje
  • tsoud/zesje
  • pimotte/zesje
  • works-on-my-machine/zesje
  • labay11/zesje
  • reouvenassouly/zesje
  • t.v.aerts/zesje
  • giuseppe.deininger/zesje
12 results
Show changes
Commits on Source (20)
import pytest
from flask import json
from zesje.database import db, Exam, Problem, ProblemWidget
@pytest.fixture
def add_test_data(app):
with app.app_context():
exam1 = Exam(id=1, name='exam 1', finalized=False)
db.session.add(exam1)
db.session.commit()
problem1 = Problem(id=1, name='Problem 1', exam_id=1)
db.session.add(problem1)
db.session.commit()
problem_widget_1 = ProblemWidget(id=1, name='problem widget', problem_id=1, page=2,
width=100, height=150, x=40, y=200, type='problem_widget')
db.session.add(problem_widget_1)
db.session.commit()
def test_get_exams(test_client, add_test_data):
mc_option_1 = {
'x': 100,
'y': 40,
'problem_id': 1,
'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': 1,
'page': 1,
'label': 'a',
'name': 'test'
}
test_client.put('/api/mult-choice/', data=mc_option_2)
response = test_client.get('/api/exams/1')
data = json.loads(response.data)
assert len(data['problems'][0]['mc_options']) == 2
import pytest
from flask import json
from zesje.database import db, Exam, Problem, ProblemWidget
@pytest.fixture
def add_test_data(app):
with app.app_context():
exam1 = Exam(id=1, name='exam 1', finalized=False)
exam2 = Exam(id=2, name='exam 2', finalized=True)
exam3 = Exam(id=3, name='exam 3', finalized=False)
db.session.add(exam1)
db.session.add(exam2)
db.session.add(exam3)
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 3', exam_id=3)
db.session.add(problem1)
db.session.add(problem2)
db.session.add(problem3)
problem_widget_1 = ProblemWidget(id=1, name='problem widget', problem_id=1, page=2,
width=100, height=150, x=40, y=200, type='problem_widget')
db.session.add(problem_widget_1)
db.session.commit()
def mco_json():
return {
'x': 100,
'y': 40,
'problem_id': 1,
'page': 1,
'label': 'a',
'name': 'test'
}
'''
ACTUAL TESTS
'''
def test_not_present(test_client, add_test_data):
result = test_client.get('/api/mult-choice/1')
data = json.loads(result.data)
assert data['status'] == 404
def test_add(test_client, add_test_data):
req = mco_json()
response = test_client.put('/api/mult-choice/', data=req)
data = json.loads(response.data)
assert data['message'] == 'New multiple choice question with id 2 inserted. ' \
+ 'New feedback option with id 1 inserted.'
assert data['mult_choice_id'] == 2
assert data['status'] == 200
def test_add_get(test_client, add_test_data):
req = mco_json()
response = test_client.put('/api/mult-choice/', data=req)
data = json.loads(response.data)
id = data['mult_choice_id']
result = test_client.get(f'/api/mult-choice/{id}')
data = json.loads(result.data)
exp_resp = {
'id': 2,
'name': 'test',
'x': 100,
'y': 40,
'type': 'mcq_widget',
'feedback_id': 1,
'label': 'a',
}
assert exp_resp == data
def test_update_put(test_client, add_test_data):
req = mco_json()
response = test_client.put('/api/mult-choice/', data=req)
data = json.loads(response.data)
id = data['mult_choice_id']
req2 = {
'x': 120,
'y': 50,
'problem_id': 4,
'page': 1,
'label': 'b',
'name': 'test'
}
result = test_client.patch(f'/api/mult-choice/{id}', data=req2)
data = json.loads(result.data)
assert data['status'] == 200
def test_delete(test_client, add_test_data):
req = mco_json()
response = test_client.put('/api/mult-choice/', data=req)
data = json.loads(response.data)
id = data['mult_choice_id']
response = test_client.delete(f'/api/mult-choice/{id}')
data = json.loads(response.data)
assert data['status'] == 200
def test_delete_not_present(test_client, add_test_data):
id = 100
response = test_client.delete(f'/api/mult-choice/{id}')
data = json.loads(response.data)
assert data['status'] == 404
def test_delete_finalized_exam(test_client, add_test_data):
mc_option_json = {
'x': 100,
'y': 40,
'problem_id': 2,
'page': 1,
'label': 'a',
'name': 'test'
}
response = test_client.put('/api/mult-choice/', data=mc_option_json)
data = json.loads(response.data)
mc_id = data['mult_choice_id']
response = test_client.delete(f'/api/mult-choice/{mc_id}')
data = json.loads(response.data)
assert data['status'] == 401
......@@ -2,8 +2,41 @@ import os
import pytest
from flask import Flask
from zesje.api import api_bp
from zesje.database import db
# Adapted from https://stackoverflow.com/a/46062148/1062698
@pytest.fixture
def datadir():
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
@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()
app.register_blueprint(api_bp, url_prefix='/api')
return app
@pytest.fixture
def test_client(app):
client = app.test_client()
yield client
with app.app_context():
db.drop_all()
db.create_all()
......@@ -36,21 +36,13 @@ class MultipleChoice(Resource):
put_parser.add_argument('label', type=str, required=False)
put_parser.add_argument('problem_id', type=int, required=True) # Used for FeedbackOption
def put(self, id=None):
"""Adds or updates a multiple choice option to the database
If the parameter id is not present, a new multiple choice question
will be inserted with the data provided in the request body.
def put(self):
"""Adds a multiple choice option to the database
For each new multiple choice option, a feedback option that links to
the multiple choice option is inserted into the database. The new
feedback option also refers to same problem as the MultipleChoiceOption
Parameters
----------
id: The id of the multiple choice option
"""
args = self.put_parser.parse_args()
# Get request arguments
......@@ -60,36 +52,23 @@ class MultipleChoice(Resource):
label = args['label']
problem_id = args['problem_id']
# TODO: Set type here or add to request?
mc_type = 'mcq_widget'
if not id:
# Insert new empty feedback option that links to the same problem, with the label as name
new_feedback_option = FeedbackOption(problem_id=problem_id, text=label)
db.session.add(new_feedback_option)
db.session.commit()
# Insert new entry into the database
mc_entry = MultipleChoiceOption()
set_mc_data(mc_entry, name, x, y, mc_type, new_feedback_option.id, label)
db.session.add(mc_entry)
db.session.commit()
return dict(status=200, mult_choice_id=mc_entry.id, feedback_id=new_feedback_option.id,
message=f'New multiple choice question with id {mc_entry.id} inserted. '
+ f'New feedback option with id {new_feedback_option.id} inserted.'), 200
# Update existing entry otherwise
mc_entry = MultipleChoiceOption.query.get(id)
# Insert new empty feedback option that links to the same problem
new_feedback_option = FeedbackOption(problem_id=problem_id, text='')
db.session.add(new_feedback_option)
db.session.commit()
if not mc_entry:
return dict(status=404, message=f"Multiple choice question with id {id} does not exist"), 404
# Insert new entry into the database
mc_entry = MultipleChoiceOption()
set_mc_data(mc_entry, name, x, y, mc_type, new_feedback_option.id, label)
set_mc_data(mc_entry, name, x, y, mc_type, label)
db.session.add(mc_entry)
db.session.commit()
return dict(status=200, message=f'Multiple choice question with id {id} updated'), 200
return dict(status=200, mult_choice_id=mc_entry.id, feedback_id=new_feedback_option.id,
message=f'New multiple choice question with id {mc_entry.id} inserted. '
+ f'New feedback option with id {new_feedback_option.id} inserted.'), 200
def get(self, id):
"""Fetches multiple choice option from the database
......@@ -122,6 +101,32 @@ class MultipleChoice(Resource):
return json
def patch(self, id):
"""
Updates a multiple choice option
Parameters
----------
id: The id of the multiple choice option in the database.s
"""
args = self.put_parser.parse_args()
name = args['name']
x = args['x']
y = args['y']
label = args['label']
mc_type = 'mcq_widget'
mc_entry = MultipleChoiceOption.query.get(id)
if not mc_entry:
return dict(status=404, message=f"Multiple choice question with id {id} does not exist"), 404
set_mc_data(mc_entry, name, x, y, mc_type, mc_entry.feedback_id, label)
db.session.commit()
return dict(status=200, message=f'Multiple choice question with id {id} updated'), 200
def delete(self, id):
"""Deletes a multiple choice option from the database.
Also deletes the associated feedback option with this multiple choice option.
......