// List retrieves a list of forms based on the query parameters from the // store. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formHandle) List(c *web.Context) error { limit, err := strconv.Atoi(c.Request.URL.Query().Get("limit")) if err != nil { limit = 0 } skip, err := strconv.Atoi(c.Request.URL.Query().Get("skip")) if err != nil { skip = 0 } forms, err := form.List(c.SessionID, c.Ctx["DB"].(*db.DB), limit, skip) if err != nil { return err } c.Respond(forms, http.StatusOK) return nil }
func TestList(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 forms. 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 forms.", tests.Success) // This particular looping logic is required at the moment due to issues // with the current CI environment. limit := 10 offset := 0 for { lfms, err := form.List(tests.Context, db, limit, offset) if err != nil { t.Fatalf("\t%s\tShould be able to list forms : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to list forms", tests.Success) if len(lfms) > limit { t.Fatalf("\t%s\tShould only return a maximum of the provided limit : Expected less than %d, got %d", tests.Failed, limit, len(lfms)) } t.Logf("\t%s\tShould only return a maximum of the provided limit.", tests.Success) // If we are another page of data (we have itterated at least once) and // the length of the results is zero, then we have no more results to // paginate over. At this point, we know that we couldn't find all the // upserted records. if offset > 0 && len(lfms) == 0 { t.Fatalf("\t%s\tShould be able to find a form that was upserted : Could not an upserted form in result set", tests.Failed) } found := false for _, fm := range lfms { if fm.ID.Hex() == fms[0].ID.Hex() { found = true break } } // If not all the elements have been found yet, we need to continue the // itteration process. if !found { // Increase the offset by the limit amount. offset = offset + limit // Continue itteration. continue } // Exit the itteration in the event that all the upserted documents have // been found. break } t.Logf("\t%s\tShould be able to find a form that was upserted", tests.Success) } } }