// TestUpsertDelete tests if we can add/remove a pattern to/from the db. func TestUpsertDelete(t *testing.T) { patterns, db := setup(t) defer teardown(t, db) t.Log("Given the need to upsert and delete patterns.") { t.Log("\tWhen starting from an empty patterns collection") { //---------------------------------------------------------------------- // Upsert the pattern. if err := pattern.Upsert(tests.Context, db, &patterns[0]); err != nil { t.Fatalf("\t%s\tShould be able to upsert a pattern : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to upsert a pattern.", tests.Success) //---------------------------------------------------------------------- // Get the pattern. pat, err := pattern.GetByType(tests.Context, db, patterns[0].Type) if err != nil { t.Fatalf("\t%s\tShould be able to get the pattern by type : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to get the pattern by type.", tests.Success) //---------------------------------------------------------------------- // Check that we got the relationship we expected. if !reflect.DeepEqual(patterns[0], *pat) { t.Logf("\t%+v", patterns[0]) t.Logf("\t%+v", pat) t.Fatalf("\t%s\tShould be able to get back the same pattern.", tests.Failed) } t.Logf("\t%s\tShould be able to get back the same pattern.", tests.Success) //---------------------------------------------------------------------- // Delete the pattern. if err := pattern.Delete(tests.Context, db, patterns[0].Type); err != nil { t.Fatalf("\t%s\tShould be able to delete the pattern : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to delete the pattern.", tests.Success) //---------------------------------------------------------------------- // Get the pattern. _, err = pattern.GetByType(tests.Context, db, patterns[0].Type) if err == nil { t.Fatalf("\t%s\tShould generate an error when getting a pattern with the deleted type : %s", tests.Failed, err) } t.Logf("\t%s\tShould generate an error when getting a pattern with the deleted type.", tests.Success) } } }
// Delete removes the specified Pattern from the system. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (patternHandle) Delete(c *web.Context) error { if err := pattern.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["type"]); err != nil { if err == pattern.ErrNotFound { err = web.ErrNotFound } return err } c.Respond(nil, http.StatusNoContent) return nil }