// Upsert upserts a form into the store. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (formHandle) Upsert(c *web.Context) error { var form form.Form if err := json.NewDecoder(c.Request.Body).Decode(&form); err != nil { return err } // perform the upsert operation err := ask.UpsertForm(c.SessionID, c.Ctx["DB"].(*db.DB), &form) if err != nil { return err } c.Respond(form, 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) } } }