def put(self, question_id=None, answer_id=None): data = request.get_json(force=True) data['question_id'] = question_id data['answer_id'] = answer_id data['user_id'] = session.get('user_id') response = Answer(data).update() if response.get('errors'): response_object = {'message': response.get('errors')} return make_response(jsonify(response_object)), 400 response_object = {'message': 'Update successful'} return make_response(jsonify(response_object)), 200
def get(self, answer_id=None): data = dict() data['answer_id'] = answer_id data['user_id'] = session.get('user_id') if answer_id: results = Answer(data).filter_by() if len(results) < 1: response_object = {'results': 'Answer not found'} return make_response(jsonify(response_object)), 404 response_object = {'results': results} return (jsonify(response_object)), 200 response_object = {'results': Answer(data).query()} return (jsonify(response_object)), 200
def post(self, question_id=None): data = request.get_json(force=True) data['question_id'], data['user_id'] = question_id, session.get( 'user_id') answer = Answer(data) response = answer.save() if response: response_object = {'message': response} return make_response(jsonify(response_object)), 201 response_object = { 'message': 'Unknown question id. Try a different id.' } return make_response(jsonify(response_object)), 400
def delete(self, question_id=None, answer_id=None): data = dict() data['question_id'] = question_id data['answer_id'] = answer_id data['user_id'] = session.get('user_id') answer = Answer(data) # check permission if len(answer.answer_author()) < 1: response_object = {'message': 'Unauthorized'} return make_response(jsonify(response_object)), 401 response = answer.delete() if not response: response_object = {'message': 'Answer id does not exist'} return make_response(jsonify(response_object)), 400 response_object = {'message': 'Answer deleted successful'} return make_response(jsonify(response_object)), 200
import unittest from .base import BaseTestCase from app.answers.models import Answer from app.questions.models import Question answer = Answer() question = Question() class AnswersModelTestCase(BaseTestCase): def test_answer_query_normal(self): """ Test retrieve all answers""" query = answer.query() self.assertIsInstance(query, type([])) def test_answer_model_filter_unexpected(self): """ Example: answer_id 'None' """ answer.answer_id = None query = answer.filter_by() self.assertEqual(query, []) def test_answer_model_filter_edgecase(self): """ Example: answer_id '[]' """ answer.answer_id = None query = answer.filter_by() self.assertEqual(query, []) def test_model_save_unexpected(self): """ Pass wrong json payload """ question.title, question.body = "Question title", "question body" question.user_id = self.user_id
test_case = TestCasePass.objects.filter(lesson=lesson, user=self.request.user, test=case) if test_case: test_case.update(passed=matched) else: TestCasePass(lesson=lesson, user=self.request.user, test=case, passed=matched).save() answer_data = { 'lesson': lesson, 'user': self.request.user, 'cpp_file': cpp_file, 'test_pass': test_passed } answer = Answer.objects.filter(lesson=lesson, user=self.request.user) if answer: answer.update(test_pass=test_passed) else: Answer(**answer_data).save() return super(UploadAnswerFormView, self).form_valid(form) def get_context_data(self, **kwargs): kwargs['test'] = TestCaseExample.objects.get( lesson=self.kwargs['lesson_id']) return super(UploadAnswerFormView, self).get_context_data(**kwargs) def get_success_url(self): return reverse_lazy('results:test_result', kwargs={'lesson_id': self.kwargs['lesson_id']})