// Delete removes a single form from the store. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formHandle) Delete(c *web.Context) error { id := c.Params["id"] err := form.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), id) if err != nil { return err } c.Respond(nil, http.StatusOK) return nil }
func TestUpsertDelete(t *testing.T) { fms, db := setupForms(t, "form") defer teardownForms(t, db) t.Log("Given the need to upsert and delete forms.") { t.Log("\tWhen starting from an empty forms collection") { //---------------------------------------------------------------------- // Upsert the form. if err := form.Upsert(tests.Context, db, &fms[0]); err != nil { t.Fatalf("\t%s\tShould be able to upsert a form : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to upsert a form.", tests.Success) //---------------------------------------------------------------------- // Get the form. fm, err := form.Retrieve(tests.Context, db, fms[0].ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to get the form by id : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to get the form by id.", tests.Success) //---------------------------------------------------------------------- // Check that we got the form we expected. if fms[0].ID.Hex() != fm.ID.Hex() { t.Fatalf("\t%s\tShould be able to get back the same form.", tests.Failed) } t.Logf("\t%s\tShould be able to get back the same form.", tests.Success) //---------------------------------------------------------------------- // Delete the form. if err := form.Delete(tests.Context, db, fms[0].ID.Hex()); err != nil { t.Fatalf("\t%s\tShould be able to delete the form : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to delete the form.", tests.Success) //---------------------------------------------------------------------- // Get the form. _, err = form.Retrieve(tests.Context, db, fms[0].ID.Hex()) if err == nil { t.Fatalf("\t%s\tShould generate an error when getting a form with the deleted id : %s", tests.Failed, err) } t.Logf("\t%s\tShould generate an error when getting a form with the deleted id.", tests.Success) //---------------------------------------------------------------------- // Create a new fresh form. fms[0].ID = "" if err := form.Upsert(tests.Context, db, &fms[0]); err != nil { t.Fatalf("\t%s\tShould be able to upsert a form : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to upsert a form.", tests.Success) //---------------------------------------------------------------------- // Ensure that an ID was set. if fms[0].ID == "" { t.Fatalf("\t%s\tShould be able to add an ID when upserting a new form : ID was not assigned", tests.Failed) } t.Logf("\t%s\tShould be able to add an ID when upserting a new form.", tests.Success) //---------------------------------------------------------------------- // Get the form. fm, err = form.Retrieve(tests.Context, db, fms[0].ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to get the form by id : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to get the form by id.", tests.Success) //---------------------------------------------------------------------- // Check that we got the form we expected. if fms[0].ID.Hex() != fm.ID.Hex() { t.Fatalf("\t%s\tShould be able to get back the same form.", tests.Failed) } t.Logf("\t%s\tShould be able to get back the same form.", tests.Success) //---------------------------------------------------------------------- // Delete the form. if err := form.Delete(tests.Context, db, fms[0].ID.Hex()); err != nil { t.Fatalf("\t%s\tShould be able to delete the form : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to delete the form.", tests.Success) //---------------------------------------------------------------------- // Get the form. _, err = form.Retrieve(tests.Context, db, fms[0].ID.Hex()) if err == nil { t.Fatalf("\t%s\tShould generate an error when getting a form with the deleted id : %s", tests.Failed, err) } t.Logf("\t%s\tShould generate an error when getting a form with the deleted id.", tests.Success) } } }