// TestDeleteSet validates the removal of a query from the database. func TestDeleteSet(t *testing.T) { const fixture = "basic.json" set1, db := setup(t, fixture) defer teardown(t, db) qsName := prefix + "_basic" qsBadName := prefix + "_basic_advice" t.Log("Given the need to delete a query set in the database.") { t.Log("\tWhen using fixture", fixture) { if err := query.Upsert(tests.Context, db, set1); err != nil { t.Fatalf("\t%s\tShould be able to create a query set : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a query set.", tests.Success) if err := query.Delete(tests.Context, db, qsName); err != nil { t.Fatalf("\t%s\tShould be able to delete a query set using its name[%s]: %s", tests.Failed, qsName, err) } t.Logf("\t%s\tShould be able to delete a query set using its name[%s]:", tests.Success, qsName) if err := query.Delete(tests.Context, db, qsBadName); err == nil { t.Fatalf("\t%s\tShould not be able to delete a query set using wrong name name[%s]", tests.Failed, qsBadName) } t.Logf("\t%s\tShould not be able to delete a query set using wrong name name[%s]", tests.Success, qsBadName) if _, err := query.GetByName(tests.Context, db, qsName); err == nil { t.Fatalf("\t%s\tShould be able to validate query set with Name[%s] does not exists: %s", tests.Failed, qsName, errors.New("Record Exists")) } t.Logf("\t%s\tShould be able to validate query set with Name[%s] does not exists:", tests.Success, qsName) } } }
// TestUnknownName validates the behaviour of the query API when using a invalid/ // unknown query name. func TestUnknownName(t *testing.T) { const fixture = "basic.json" set1, db := setup(t, fixture) defer teardown(t, db) qsName := prefix + "_unknown" t.Log("Given the need to validate bad query name response.") { t.Log("\tWhen using fixture", fixture) { if err := query.Upsert(tests.Context, db, set1); err != nil { t.Fatalf("\t%s\tShould be able to create a query set : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a query set.", tests.Success) if _, err := query.GetByName(tests.Context, db, qsName); err == nil { t.Fatalf("\t%s\tShould be able to validate query set with Name[%s] does not exists: %s", tests.Failed, qsName, errors.New("Record Exists")) } t.Logf("\t%s\tShould be able to validate query set with Name[%s] does not exists.", tests.Success, qsName) if err := query.Delete(tests.Context, db, qsName); err == nil { t.Fatalf("\t%s\tShould be able to validate query set with Name[%s] can not be deleted: %s", tests.Failed, qsName, errors.New("Record Exists")) } t.Logf("\t%s\tShould be able to validate query set with Name[%s] can not be deleted.", tests.Success, qsName) } } }
// TestAPIFailureSet validates the failure of the api using a nil session. func TestAPIFailureSet(t *testing.T) { const fixture = "basic.json" set1, db := setup(t, fixture) defer teardown(t, db) qsName := prefix + "_unknown" t.Log("Given the need to validate failure of API with bad session.") { t.Log("When giving a nil session") { err := query.EnsureIndexes(tests.Context, nil, set1) if err == nil { t.Fatalf("\t%s\tShould be refused create by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused create by api with bad session: %s", tests.Success, err) err = query.Upsert(tests.Context, nil, set1) if err == nil { t.Fatalf("\t%s\tShould be refused create by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused create by api with bad session: %s", tests.Success, err) _, err = query.GetNames(tests.Context, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = query.GetAll(tests.Context, nil, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = query.GetByName(tests.Context, nil, qsName) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = query.GetLastHistoryByName(tests.Context, nil, qsName) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) err = query.Delete(tests.Context, nil, qsName) if err == nil { t.Fatalf("\t%s\tShould be refused delete by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused delete by api with bad session: %s", tests.Success, err) } } }
// Delete removes the specified Set from the system. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (queryHandle) Delete(c *web.Context) error { if err := query.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"]); err != nil { if err == query.ErrNotFound { err = web.ErrNotFound } return err } c.Respond(nil, http.StatusNoContent) return nil }