// DeleteSubmission deletes a submission as well as updating a form's stats. func DeleteSubmission(context interface{}, db *db.DB, id, formID string) error { log.Dev(context, "DeleteSubmission", "Started : Submission[%s]", id) if !bson.IsObjectIdHex(id) { log.Error(context, "DeleteSubmission", ErrInvalidID, "Completed") return ErrInvalidID } if !bson.IsObjectIdHex(formID) { log.Error(context, "Delete", ErrInvalidID, "Completed") return ErrInvalidID } if err := submission.Delete(context, db, id); err != nil { log.Error(context, "DeleteSubmission", err, "Completed") return err } if _, err := form.UpdateStats(context, db, formID); err != nil { log.Error(context, "DeleteSubmission", err, "Completed") return err } log.Dev(context, "DeleteSubmission", "Started") return nil }
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) } } }
func Test_Answers(t *testing.T) { gs, db := setup(t, "gallery") defer teardown(t, db) t.Log("Given the need to add and remove answers from galleries.") { t.Log("\tWhen starting from an empty galleries collection but saturated submissions collection") { //---------------------------------------------------------------------- // Starting with a single gallery. g := gs[0] //---------------------------------------------------------------------- // Create the gallery. if err := gallery.Create(tests.Context, db, &g); err != nil { t.Fatalf("\t%s\tShould be able to upsert a gallery : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to upsert a gallery.", tests.Success) //---------------------------------------------------------------------- // We need to get/fill in real form submissions. subs, err := submissionfix.GetMany("gallery_submissions.json") if err != nil { t.Fatalf("Should be able to fetch submission fixtures : %v", err) } // Set the form ID on these new submissions. for i := range subs { // We need to assign a new submission ID to ensure that we don't collide // with existing submissions. subs[i].ID = bson.NewObjectId() subs[i].FormID = g.FormID } // Add all the submission fixtures. if err := submissionfix.Add(tests.Context, db, subs); err != nil { t.Fatalf("Should be able to add submission fixtures : %v", err) } // Remove the new fixtures after the tests are completed. defer func() { for _, sub := range subs { if err := submission.Delete(tests.Context, db, sub.ID.Hex()); err != nil { t.Fatalf("%s\tShould be able to remove submission fixtures : %v", tests.Failed, err) } } t.Logf("%s\tShould be able to remove submission fixtures.", tests.Success) }() //---------------------------------------------------------------------- // Add an answer to the gallery. crg, err := gallery.AddAnswer(tests.Context, db, g.ID.Hex(), subs[0].ID.Hex(), subs[0].Answers[0].WidgetID) if err != nil { t.Fatalf("\t%s\tShould be able to add an answer to a gallery : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to add an answer to a gallery.", tests.Success) //---------------------------------------------------------------------- // Check that the returned gallery has the correct answers. if len(crg.Answers) != 1 { t.Fatalf("\t%s\tShould have at least one answer on the returned gallery : Expected 1, got %d", tests.Failed, len(crg.Answers)) } t.Logf("\t%s\tShould have at least one answer on the returned gallery.", tests.Success) matchAnswers(t, crg.Answers[0], subs[0], subs[0].Answers[0]) //---------------------------------------------------------------------- // Check that the retrieved gallery has the correct answers. rg, err := gallery.Retrieve(tests.Context, db, g.ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve a gallery : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve a gallery.", tests.Success) if len(rg.Answers) != 1 { t.Fatalf("\t%s\tShould have at least one answer on the returned gallery : Expected 1, got %d", tests.Failed, len(rg.Answers)) } t.Logf("\t%s\tShould have at least one answer on the returned gallery.", tests.Success) matchAnswers(t, rg.Answers[0], subs[0], subs[0].Answers[0]) //---------------------------------------------------------------------- // Remove the answer. drg, err := gallery.RemoveAnswer(tests.Context, db, g.ID.Hex(), subs[0].ID.Hex(), subs[0].Answers[0].WidgetID) if err != nil { t.Fatalf("\t%s\tShould be able to remove an answer from a gallery : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to remove an answer from a gallery.", tests.Success) //---------------------------------------------------------------------- // Check that the returned gallery has the correct answers. if len(drg.Answers) != 0 { t.Fatalf("\t%s\tShould have at no answers on the returned gallery : Expected 0, got %d", tests.Failed, len(drg.Answers)) } t.Logf("\t%s\tShould have at no answers on the returned gallery.", tests.Success) //---------------------------------------------------------------------- // Check that the retrieved gallery has the correct answers. rg, err = gallery.Retrieve(tests.Context, db, g.ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve a gallery : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve a gallery.", tests.Success) if len(rg.Answers) != 0 { t.Fatalf("\t%s\tShould have at no answers on the returned gallery : Expected 0, got %d", tests.Failed, len(drg.Answers)) } t.Logf("\t%s\tShould have at no answers on the returned gallery.", tests.Success) } } }