func Test_List(t *testing.T) { gs, db := setup(t, "gallery_list") defer teardown(t, db) t.Log("Given the need to list galleries.") { t.Log("\tWhen starting from an empty galleries collection.") { lgs, err := gallery.List(tests.Context, db, gs[0].FormID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to list no galleries : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to list no galleries.", tests.Success) if len(lgs) != 0 { t.Fatalf("\t%s\tShould be able to list the correct amount of galleries: Expected 0, found %d", tests.Failed, len(lgs)) } t.Logf("\t%s\tShould be able to list the correct amount of galleries.", tests.Success) if err := galleryfix.Add(tests.Context, db, gs); err != nil { t.Fatalf("\t%s\tShould be able to load gallery fixtures : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to load gallery fixtures.", tests.Success) lgs, err = gallery.List(tests.Context, db, gs[0].FormID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to load gallery fixtures : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to load gallery fixtures.", tests.Success) if len(lgs) != len(gs) { t.Fatalf("\t%s\tShould be able to list the correct amount of galleries: Expected %d, found %d", tests.Failed, len(gs), len(lgs)) } t.Logf("\t%s\tShould be able to list the correct amount of galleries.", tests.Success) for _, g := range lgs { if g.FormID.Hex() != gs[0].FormID.Hex() { t.Fatalf("\t%s\tShould have the correct form id : Expected %s, got %s", tests.Failed, gs[0].FormID.Hex(), g.FormID.Hex()) } } t.Logf("\t%s\tShould have the correct form id.", tests.Success) matches := 0 for _, fg := range gs { for _, lg := range lgs { if lg.ID.Hex() == fg.ID.Hex() { matches++ } } } if matches != len(lgs) { t.Fatalf("\t%s\tShould contain all the fixtures in the listed contents : Not all fixtures found", tests.Failed) } t.Logf("\t%s\tShould contain all the fixtures in the listed contents.", tests.Success) } } }
// RetrieveForForm retrieves a collection of galleries based on a specific form // id. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formGalleryHandle) RetrieveForForm(c *web.Context) error { formID := c.Params["form_id"] galleries, err := gallery.List(c.SessionID, c.Ctx["DB"].(*db.DB), formID) if err != nil { return err } c.Respond(galleries, http.StatusOK) return nil }
func Test_UpsertForm(t *testing.T) { db := setup(t) defer teardown(t, db) t.Log("Given the need to upsert a form.") { //---------------------------------------------------------------------- // Get the form fixture. fms, err := formfix.Get("ask_form") if err != nil { t.Fatalf("%s\tShould be able to get the form fixture : %v", tests.Failed, err) } t.Logf("%s\tShould be able to get the form fixture", tests.Success) //---------------------------------------------------------------------- // Select a specific form. fm := fms[0] //---------------------------------------------------------------------- // Update it's ID to a new one to ensure we aren't updating. fm.ID = bson.ObjectId("") t.Log("\tWhen starting from an empty forms collection") { //---------------------------------------------------------------------- // Upsert the form. if err := ask.UpsertForm(tests.Context, db, &fm); err != nil { t.Fatalf("\t%s\tShould be able to upsert the form : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to upsert the form", tests.Success) if fm.ID.Hex() == "" { t.Fatalf("\t%s\tShould be able to update the ID when upserted as a new record : ID was not updated", tests.Failed) } t.Logf("\t%s\tShould be able to update the ID when upserted as a new record", tests.Success) //---------------------------------------------------------------------- // Retrieve the form to ensure it was created. if _, err := form.Retrieve(tests.Context, db, fm.ID.Hex()); err != nil { t.Fatalf("\t%s\tShould be able to retrieve the form : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the form", tests.Success) //---------------------------------------------------------------------- // Retrieve the gallery to ensure it was created. gs, err := gallery.List(tests.Context, db, fm.ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the gallery : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the gallery", tests.Success) //---------------------------------------------------------------------- // Cleanup the galleries created. defer func(gs []gallery.Gallery) { for _, g := range gs { if err := gallery.Delete(tests.Context, db, g.ID.Hex()); err != nil { t.Fatalf("\t%s\tShould be able to remove the created galleries : %v", tests.Failed, err) } } t.Logf("\t%s\tShould be able to remove the created galleries.", tests.Success) }(gs) } t.Log("\tWhen starting from an non-empty forms collection") { //---------------------------------------------------------------------- // Update the form. newFooter := bson.M{"key": "value"} fm.Footer = newFooter //---------------------------------------------------------------------- // Upsert the form. if err := ask.UpsertForm(tests.Context, db, &fm); err != nil { t.Fatalf("\t%s\tShould be able to upsert the form : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to upsert the form", tests.Success) if fm.ID.Hex() == "" { t.Fatalf("\t%s\tShould be able to update the ID when upserted as a new record : ID was not updated", tests.Failed) } t.Logf("\t%s\tShould be able to update the ID when upserted as a new record", tests.Success) //---------------------------------------------------------------------- // Retrieve the form to ensure it was created. rf, err := form.Retrieve(tests.Context, db, fm.ID.Hex()) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the form : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the form", tests.Success) rFooter, ok := rf.Footer.(bson.M) if !ok { t.Fatalf("\t%s\tShould have a bson document in the footer value : Does not", tests.Failed) } t.Logf("\t%s\tShould have a bson document in the footer value", tests.Success) value, ok := rFooter["key"] if !ok { t.Fatalf("\t%s\tShould have a bson key in the footer value : Does not", tests.Failed) } t.Logf("\t%s\tShould have a bson key in the footer value", tests.Success) if value != "value" { t.Fatalf("\t%s\tShould have expected value : Expected \"%s\", got \"%v\"", tests.Failed, "value", value) } t.Logf("\t%s\tShould have expected value", tests.Success) } } }