Beispiel #1
0
func Test_UpdateStatus(t *testing.T) {
	subs, db := setup(t, "submission")
	defer teardown(t, db)

	t.Log("Given the need to update the status of a submission.")
	{
		t.Log("\tWhen starting from an empty submissions collection")
		{

			//----------------------------------------------------------------------
			// Create the submission.

			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)

			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)

			newStatus := time.Now().String()

			//----------------------------------------------------------------------
			// Update the submission's status.

			nsub, err := submission.UpdateStatus(tests.Context, db, sub.ID.Hex(), newStatus)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to update the submission status : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to update the submission status", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the status was updated on the returned submission.

			if nsub.Status != newStatus {
				t.Fatalf("\t%s\tShould be able to update the submission status : Expected %s, got %s", tests.Failed, newStatus, nsub.Status)
			}
			t.Logf("\t%s\tShould be able to update the submission status", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the status 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.Status != newStatus {
				t.Fatalf("\t%s\tShould be able to update the submission status in the store : Expected %s, got %s", tests.Failed, newStatus, nsub.Status)
			}
			t.Logf("\t%s\tShould be able to update the submission status in the store", tests.Success)
		}
	}
}
Beispiel #2
0
func Test_CreateDelete(t *testing.T) {
	subs, db := setup(t, "submission")
	defer teardown(t, db)

	t.Log("Given the need to create and delete submissions.")
	{
		t.Log("\tWhen starting from an empty submissions collection")
		{
			//----------------------------------------------------------------------
			// Create the submission.

			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)

			//----------------------------------------------------------------------
			// Get the submission.

			sub, err := submission.Retrieve(tests.Context, db, subs[0].ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the submission by id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the submission by id.", tests.Success)

			//----------------------------------------------------------------------
			// Check that we got the submission we expected.

			if subs[0].ID.Hex() != sub.ID.Hex() {
				t.Fatalf("\t%s\tShould be able to get back the same submission.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same submission.", tests.Success)

			//----------------------------------------------------------------------
			// Delete the submission.

			if err := submission.Delete(tests.Context, db, subs[0].ID.Hex()); err != nil {
				t.Fatalf("\t%s\tShould be able to delete the submission : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to delete the submission.", tests.Success)

			//----------------------------------------------------------------------
			// Get the submission.

			_, err = submission.Retrieve(tests.Context, db, subs[0].ID.Hex())
			if err == nil {
				t.Fatalf("\t%s\tShould generate an error when getting a submission with the deleted id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting a submission with the deleted id.", tests.Success)
		}
	}
}
Beispiel #3
0
// Retrieves a given FormSubmission based on the route params.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) Retrieve(c *web.Context) error {
	id := c.Params["id"]

	s, err := submission.Retrieve(c.SessionID, c.Ctx["DB"].(*db.DB), id)
	if err != nil {
		return err
	}

	c.Respond(s, http.StatusOK)

	return nil
}
Beispiel #4
0
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)
		}
	}
}
Beispiel #5
0
func Test_Flags(t *testing.T) {
	subs, db := setup(t, "submission")
	defer teardown(t, db)

	t.Log("Given the need to add and remove flags.")
	{
		t.Log("\tWhen starting from an empty submissions collection")
		{

			//----------------------------------------------------------------------
			// Create the submission.

			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 the submission has been added to the database.

			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)

			newFlag := time.Now().String()

			//----------------------------------------------------------------------
			// Ensure that the new flag is not already on the submission.

			// Ensure that the new flag is not in the current flags.
			for _, flag := range sub.Flags {
				if flag == newFlag {
					t.Fatalf("\t%s\tShould not have test flag already : Flag already exists", tests.Failed)
				}
			}
			t.Logf("\t%s\tShould not have test flag already.", tests.Success)

			//----------------------------------------------------------------------
			// Add the flag to the submission.

			nsub, err := submission.AddFlag(tests.Context, db, sub.ID.Hex(), newFlag)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to add the flag : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to add the flag", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was added to the submission returned.

			var found bool
			for _, flag := range nsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if !found {
				t.Fatalf("\t%s\tShould have test flag on returned object : Flag does not exist", tests.Failed)
			}
			t.Logf("\t%s\tShould have test flag on returned object.", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was added to the store's 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)

			found = false
			for _, flag := range rsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if !found {
				t.Fatalf("\t%s\tShould have test flag on database object : Flag does not exist", tests.Failed)
			}
			t.Logf("\t%s\tShould have test flag on database object.", tests.Success)

			//----------------------------------------------------------------------
			// Remove the new flag from the submission.

			nsub, err = submission.RemoveFlag(tests.Context, db, sub.ID.Hex(), newFlag)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to remove the flag : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to remove the flag", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was removed from the submission returned.

			found = false
			for _, flag := range nsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if found {
				t.Fatalf("\t%s\tShould not have test flag on returned object : Flag found", tests.Failed)
			}
			t.Logf("\t%s\tShould not have test flag on returned object.", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was removed from the submission in the store.

			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)

			found = false
			for _, flag := range rsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if found {
				t.Fatalf("\t%s\tShould not have test flag on database object : Flag found", tests.Failed)
			}
			t.Logf("\t%s\tShould not have test flag on database object.", tests.Success)
		}
	}
}
Beispiel #6
0
func Test_CreateDeleteSubmission(t *testing.T) {
	db := setup(t)
	defer teardown(t, db)

	// CreateSubmission(context interface{}, db *db.DB, formID string, answers []submission.AnswerInput) (*submission.Submission, error)

	t.Log("Given the need to add a submission.")
	{

		//----------------------------------------------------------------------
		// Get the form fixture.

		fms, err := formfix.Get("ask_form")
		if err != nil {
			t.Fatalf("%s\tShould be able to get the form fixture : %v", tests.Failed, err)
		}
		t.Logf("%s\tShould be able to get the form fixture", tests.Success)

		if err := formfix.Add(tests.Context, db, fms); err != nil {
			t.Fatalf("%s\tShould be able to add the form fixture : %v", tests.Failed, err)
		}
		t.Logf("%s\tShould be able to add the form fixture", tests.Success)

		fm := fms[0]

		t.Log("\tWhen starting from an empty submission collection")
		{

			var answers []submission.AnswerInput

			// Create the answers based on the form layout.

			answer := time.Now().Unix()

			for _, step := range fm.Steps {
				for _, widget := range step.Widgets {
					answers = append(answers, submission.AnswerInput{
						WidgetID: widget.ID,
						Answer:   answer,
					})
				}
			}

			// Create the submission.

			sub, err := ask.CreateSubmission(tests.Context, db, fm.ID.Hex(), answers)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to create a submission : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a submission.", tests.Success)

			// Ensure that the answers match.

			matchSubmissionsAndAnswers(t, sub, fm, answers)

			// Get the submission from the database.

			rsub, err := submission.Retrieve(tests.Context, db, sub.ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve a created submission : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve a created submission.", tests.Success)

			// Ensure that their answers match.

			matchSubmissionsAndAnswers(t, rsub, fm, answers)

			// Ensure that the form's stats were updated.

			rfm, err := form.Retrieve(tests.Context, db, fm.ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve a form : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve a form.", tests.Success)

			if rfm.Stats.Responses != 1 {
				t.Fatalf("\t%s\tShould be able to update the stats on a form : Expected %d, got %d", tests.Failed, 1, rfm.Stats.Responses)
			}
			t.Logf("\t%s\tShould be able to update the stats on a form", tests.Success)

			// Delete the submission.

			if err := ask.DeleteSubmission(tests.Context, db, sub.ID.Hex(), fm.ID.Hex()); err != nil {
				t.Fatalf("\t%s\tShould be able to delete a submission : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to delete a submission.", tests.Success)

			// Ensure that it is deleted.

			if _, err := submission.Retrieve(tests.Context, db, sub.ID.Hex()); err == nil {
				t.Fatalf("\t%s\tShould return not found when trying to retrieve a deleted submission : No error", tests.Failed)
			} else if err != mgo.ErrNotFound {
				t.Fatalf("\t%s\tShould return not found when trying to retrieve a deleted submission : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould return not found when trying to retrieve a deleted submission.", tests.Success)

			// Ensure that the form's stats were updated.

			rfm, err = form.Retrieve(tests.Context, db, fm.ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve a form : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve a form.", tests.Success)

			if rfm.Stats.Responses != 0 {
				t.Fatalf("\t%s\tShould be able to update the stats on a form : Expected %d, got %d", tests.Failed, 0, rfm.Stats.Responses)
			}
			t.Logf("\t%s\tShould be able to update the stats on a form", tests.Success)
		}
	}
}