def test_save_answer():
    create_tables()
    create_user()
    new_question = QuestionModel('My simple title', 'My simple question')
    new_question.save(1)
    samp_answer = AnswerModel("This is my sample answer")
    samp_answer.add_answer(1, 'lazarus')
    teardown()
def test_delete():
    create_tables()
    create_user()
    new_question = QuestionModel('My simple title', 'My simple question')
    new_question.save(1)
    samp_answer = AnswerModel("This is my sample answer")
    samp_answer.add_answer(1, 'lazarus')
    answer = AnswerModel.find_by_id(1, 1)
    assert answer.delete(1) == True
    teardown()
Example #3
0
 def put(self, questionID, answerID):
     # parser = reqparse.RequestParser()
     # parser.add_argument('downvote',
     #     type = str,
     #     required = True,
     #     help = "The upvote field is required"
     # )
     # data = parser.parse_args()
     question = QuestionModel.find_by_id(questionID)
     if question:
         answer = AnswerModel.find_by_id(questionID, answerID)
         if answer:
             answer.downvote(questionID)
             return {
                 "message": "Answer downvoted successfully"
             }, 201  #Created
         else:
             return {
                 "message": "Cannot downvote for a non-existing answer."
             }, 422  #Unprocessable entity
     else:
         return {
             "message":
             "Cannot downvote answer for a non-existing question."
         }, 422  #Unprocessable entity
Example #4
0
 def get(self, questionID, answerID):
     #Check if the question really exists
     #If True check for the answer and return
     #else, return a error message
     if QuestionModel.find_by_id(questionID):
         #Check for answer, returns an object
         answer = AnswerModel.find_by_id(questionID, answerID)
         if answer:
             return AnswerModel.find_descriptive_single_answer(
                 answerID), 200
         else:
             return {"message": "Answer not found."}, 422
     else:
         return {
             "message": "Cannot get answer for a non-existing question"
         }, 422
Example #5
0
    def post(cls, questionID):
        #Check if the question exists
        #if True create an answer dictionary and pass it
        #to add_answer method in answermodel(return the response)
        #else return error messages
        data = cls.parser.parse_args()

        identity = 0
        if current_identity.id:
            identity = current_identity.username

        if QuestionModel.find_by_id(questionID):
            if not AnswerModel.find_by_answer(questionID, data['answer']):
                answer = AnswerModel(data["answer"])
                if answer.add_answer(questionID, identity):
                    return {"message": "Answer inserted successfully"}, 201
            return {"message": "The answer already exists"}, 409

        return {"message": "You cannot answer a non-existing question"}, 422
def test_new_answer_item():
    """
    GIVEN the Answer model
    WHEN a new Answer is created
    THEN check the answer field if defined
    """
    answer = AnswerModel("This is my answer")
    assert answer.id == None
    assert answer.answer != 'This is a wrong answer'
    assert answer.answer == 'This is my answer'
Example #7
0
 def get(self, questionID):
     #Check if the question exists
     #if True, return the answers
     #else, return an error message
     if QuestionModel.find_by_id(questionID):
         answers = AnswerModel.get_answers(questionID)
         if answers:
             return answers, 200
         return {
             "message": "There are no answers for this question"
         }, 422  #Unprocessable entity
     return {
         "message": "You can't find answers for a non existing question"
     }, 422  # Unp0rocessable entity
Example #8
0
 def delete(self, questionID, answerID):
     question = QuestionModel.find_by_id(questionID)
     if question:
         answer = AnswerModel.find_by_id(questionID, answerID)
         if answer:
             answer.delete(questionID)
             return {"message": "Answer deleted successfully"}, 201
         else:
             return {
                 "message": "Answer already deleted or does not exist."
             }, 422
     else:
         return {
             "message": "Cannot delete answer for a non-existing question."
         }, 422
Example #9
0
 def put(self, questionID, answerID):
     question = QuestionModel.find_by_id(questionID)
     if question:
         answer = AnswerModel.find_by_id(questionID, answerID)
         if answer:
             answer.upvote(questionID)
             return {
                 "message": "Answer upvoted successfully"
             }, 201  #Created
         else:
             return {
                 "message": "Cannot upvote for a non-existing answer."
             }, 422  #Unprocessable entity
     else:
         return {
             "message": "Cannot upvote answer for a non-existing question."
         }, 422  #Unprocessable entity
Example #10
0
 def put(cls, questionID, answerID):
     data = cls.parser.parse_args()
     #check if the question exists
     #if exists, check for the answer
     #create a new answer object, update and return it
     #else return an error message
     if QuestionModel.find_by_id(questionID):
         answer = AnswerModel.find_by_id(questionID, answerID)
         if answer:
             answer.answer = data['answer']
             if answer.update(questionID):
                 return {"message": "Your answer updated successfully"}, 201
         else:
             return {"message": "Cannot update a non-existing answer."}, 422
     else:
         return {
             "message": "Cannot update answer for a non-existing question."
         }, 422
Example #11
0
    def put(self, questionID, answerID):

        question = QuestionModel.find_by_id(questionID)
        if question:
            answer = AnswerModel.find_by_id(questionID, answerID)
            if answer:
                answer.solve(questionID)
                return {
                    "message": "Answer marked as solution successfully"
                }, 201  #Created
            else:
                return {
                    "message":
                    "Cannot mark a non-existing answer as a solution."
                }, 422  #Unprocessable entity
        else:
            return {
                "message": "Cannot solve a non-existing question."
            }, 422  #Unprocessable entity
def test_answer_json():
    answer = AnswerModel("Return my answer in Json")
    assert answer.json() == {"id": None, "answer": "Return my answer in Json"}