// UpdateAnswer updates an answer based on the payload submitted to the // endpoint. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formSubmissionHandle) UpdateAnswer(c *web.Context) error { var editedAnswer struct { Edited string } if err := json.NewDecoder(c.Request.Body).Decode(&editedAnswer); err != nil { return err } id := c.Params["id"] answerID := c.Params["answer_id"] s, err := submission.UpdateAnswer(c.SessionID, c.Ctx["DB"].(*db.DB), id, submission.AnswerInput{ WidgetID: answerID, Answer: editedAnswer.Edited, }) if err != nil { return err } c.Respond(s, http.StatusOK) return nil }
func Test_UpdateAnswer(t *testing.T) { subs, db := setup(t, "submission") defer teardown(t, db) t.Log("Given the need to update an answer on a submission.") { t.Log("\tWhen starting from an empty submissions collection") { //---------------------------------------------------------------------- // Create the submisison. if err := submission.Create(tests.Context, db, subs[0].FormID.Hex(), &subs[0]); err != nil { t.Fatalf("\t%s\tShould be able to create a submission : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a submission.", tests.Success) //---------------------------------------------------------------------- // Ensure that we have created it. sub, err := submission.Retrieve(tests.Context, db, subs[0].ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the submission : %s", tests.Failed, err.Error()) } t.Logf("\t%s\tShould be able to retrieve the submission", tests.Success) newAnswer := time.Now().String() //---------------------------------------------------------------------- // Update the question's answer. nsub, err := submission.UpdateAnswer(tests.Context, db, sub.ID.Hex(), submission.AnswerInput{ WidgetID: sub.Answers[0].WidgetID, Answer: newAnswer, }) if err != nil { t.Fatalf("\t%s\tShould be able to update the submission answer : %s", tests.Failed, err.Error()) } t.Logf("\t%s\tShould be able to update the submission answer", tests.Success) //---------------------------------------------------------------------- // Ensure that the answer was updated on the returned submission. if nsub.Answers[0].EditedAnswer != newAnswer { t.Fatalf("\t%s\tShould be able to update the submission answer : Expected %s, got %s", tests.Failed, newAnswer, nsub.Answers[0].EditedAnswer) } t.Logf("\t%s\tShould be able to update the submission answer", tests.Success) //---------------------------------------------------------------------- // Ensure that the answer was updated on the stored submission. rsub, err := submission.Retrieve(tests.Context, db, sub.ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the submission : %s", tests.Failed, err.Error()) } t.Logf("\t%s\tShould be able to retrieve the submission", tests.Success) if rsub.Answers[0].EditedAnswer != newAnswer { t.Fatalf("\t%s\tShould be able to update the submission answer in the store : Expected %s, got %s", tests.Failed, newAnswer, nsub.Answers[0].EditedAnswer) } t.Logf("\t%s\tShould be able to update the submission answer in the store", tests.Success) } } }