// List returns all the existing patterns in the system. // 200 Success, 404 Not Found, 500 Internal func (patternHandle) List(c *web.Context) error { ps, err := pattern.GetAll(c.SessionID, c.Ctx["DB"].(*db.DB)) if err != nil { if err == pattern.ErrNotFound { err = web.ErrNotFound } return err } c.Respond(ps, http.StatusOK) return nil }
// TestGetAll tests if we can get all patterns from the db. func TestGetAll(t *testing.T) { patterns, db := setup(t) defer teardown(t, db) t.Log("Given the need to get all the patterns in the database.") { t.Log("\tWhen starting from an empty patterns collection") { for _, pat := range patterns { if err := pattern.Upsert(tests.Context, db, &pat); err != nil { t.Fatalf("\t%s\tShould be able to upsert patterns : %s", tests.Failed, err) } } t.Logf("\t%s\tShould be able to upsert patterns.", tests.Success) patternsBack, err := pattern.GetAll(tests.Context, db) if err != nil { t.Fatalf("\t%s\tShould be able to get all patterns : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to get all patterns.", tests.Success) var filteredPats []pattern.Pattern for _, pat := range patternsBack { if pat.Type[0:len(prefix)] == prefix { filteredPats = append(filteredPats, pat) } } if !reflect.DeepEqual(patterns, filteredPats) { t.Logf("\t%+v", patterns) t.Logf("\t%+v", filteredPats) t.Fatalf("\t%s\tShould be able to get back the same patterns.", tests.Failed) } t.Logf("\t%s\tShould be able to get back the same patterns.", tests.Success) } } }