def delete(self, questionID):
        #Check if the question really exists
        #if True call the delete method and return a success message
        #else, return an error message

        #Get the identity of the currently logged in user
        identity = 0
        if current_identity.id:
            identity = current_identity.username
        question = QuestionModel.find_by_id(questionID)
        validate_owner = QuestionModel.find_descriptive_single_question(
            question.id)
        if question:
            if validate_owner.get('author') == identity:
                if question.delete():
                    return {"message": "Question deleted successfully."}, 201
                else:
                    return {
                        "message": "Question not deleted."
                    }, 409  #Confilict
            else:
                return {
                    "message":
                    "You do not have permission to delete the question."
                }
        return {
            "message":
            "The question is not available or it was already deleted."
        }, 422  #Unprocessable entity
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()
def test_json():
    create_tables()
    create_user()
    my_question = QuestionModel('Json title', 'Json description')
    my_question.save(1)

    question_query = QuestionModel.find_by_id(1)
    assert question_query.json() == {
        "title": 'Json title',
        "description": 'Json description'
    }
    teardown()
def test_delete():
    """
    GIVEN a new question saved
    WHEN a new query is passed for deletion
    THEN it should return True
    """
    create_tables()
    create_user()
    new_question = QuestionModel('This is a sample title',
                                 'This is a sample description')
    new_question.save(1)

    question = QuestionModel.find_by_id(1)
    assert question.delete() == True
    teardown()
def test_question_find_by_description():
    """
    Given a new question saved
    When a query is called by description
    Then a boolean is returned, check if the fields are correct
    """
    create_tables()
    create_user()
    new_question = QuestionModel('This is a sample 1 title',
                                 'This is a sample 1 description')
    new_question.save(1)
    #It's the first to be stored hence id == 1
    question = QuestionModel.find_by_description(
        'This is a sample 1 description')
    assert question == True
    teardown()
Exemple #7
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
 def get(self, questionID):
     #Check if question exists
     #if true(returns an object) return it
     #else, return an error message
     question = QuestionModel.find_descriptive_single_question(questionID)
     if question:
         return question, 200
     return {"message": "Question not available."}, 422
    def put(cls, questionID):
        #Check if the question description already exists
        #if it exists return an error message
        #if not, check if the question identified by id exists
        #if true, update the question and return
        #else, return an error message

        #Get the identity of the currently logged in user
        identity = 0
        if current_identity.id:
            identity = current_identity.username
        data = cls.parser.parse_args()
        if not QuestionModel.find_by_description(data['description']):
            question = QuestionModel.find_by_id(questionID)
            validate_owner = QuestionModel.find_descriptive_single_question(
                question.id)
            if question:
                if validate_owner.get('author') == identity:
                    #create a new object with updated details
                    updated_question = QuestionModel(data['title'],
                                                     data['description'],
                                                     question.id)
                    #Update and return json data
                    if updated_question.update():
                        return QuestionModel.find_descriptive_single_question(
                            questionID), 200
                    return {"message": "Question could not be updated."}, 409
                return {
                    "message":
                    "You do not have permission to edit the question."
                }
            return {"message": "You can't edit a non-existing question."}, 422
        return {
            "message": "Question with the same decription already exists."
        }, 422
    def get(self):
        #call the all classmethod of questionModel class to get the response
        #if response exists , return it
        #else, return an error message
        response = QuestionModel.all()
        if response:
            return response, 200

        return {"message": "No questions found!"}, 422  #Unprocessable entity
def test_new_question_item():
    """
    GIVEN a Question model
    WHEN a new Question is created
    THEN check the title, description, and id fields are defined correctly
    """
    new_question = QuestionModel('This is a sample title',
                                 'This is a sample description')

    assert new_question.title == 'This is a sample title'
    assert new_question.description == 'This is a sample description'
    def post(self):
        data = QuestionList.parser.parse_args()
        #Get the identity of the currently logged in user
        identity = 0
        if current_identity.id:
            identity = current_identity.id
        else:
            return {"message": "Login to continue"}
        #Check if a question with the same description exists
        #return an error message if it exists
        #else, save the new question and return a response
        if QuestionModel.find_by_description(data['description']):
            return {
                "message": "The question is already asked"
            }, 409  # Conflict(Duplicate)

        #Create a question object and pass the arguments
        question = QuestionModel(data["title"], data["description"])

        response = question.save(identity)
        return response, 201
def test_question_find_by_id():
    """
    Given a new question saved
    When a query is called by id
    Then a new object should be returned, check if the fields are correct
    """
    create_tables()
    create_user()
    new_question = QuestionModel('This is a sample 1 title',
                                 'This is a sample 1 description')
    new_question.save(1)
    #It's the first to be stored hence id == 1
    question = QuestionModel.find_by_id(1)
    assert question.title == 'This is a sample 1 title'

    new_question1 = QuestionModel('This is a sample 2 title',
                                  'This is a sample 2 description')
    new_question1.save(1)

    question1 = QuestionModel.find_by_id(2)
    assert question1.title == 'This is a sample 2 title'
    teardown()
Exemple #14
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
    def get(self):
        #Get the identity of the currently logged in user
        identity = 0
        if current_identity.id:
            identity = current_identity.id
        else:
            return {"message": "login to continue"}

        #Check if the user has questions
        my_questions = QuestionModel.find_by_user_id(identity)
        if my_questions:
            return my_questions
        else:
            return {"message": "You currently have no questions."}
Exemple #16
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
Exemple #17
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
Exemple #18
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
Exemple #19
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
Exemple #20
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
Exemple #21
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