Ejemplo n.º 1
0
func Test_CreateDelete(t *testing.T) {
	gs, db := setup(t, "gallery")
	defer teardown(t, db)

	t.Log("Given the need to create and delete galleries.")
	{
		t.Log("\tWhen starting from an empty galleries collection")
		{
			//----------------------------------------------------------------------
			// Starting with a single gallery.
			g := gs[0]

			//----------------------------------------------------------------------
			// Create the gallery.

			if err := gallery.Create(tests.Context, db, &g); err != nil {
				t.Fatalf("\t%s\tShould be able to upsert a gallery : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to upsert a gallery.", tests.Success)

			//----------------------------------------------------------------------
			// Get the gallery.

			rg, err := gallery.Retrieve(tests.Context, db, g.ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the gallery by id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the gallery by id.", tests.Success)

			//----------------------------------------------------------------------
			// Check that we got the gallery we expected.

			if rg.ID.Hex() != g.ID.Hex() {
				t.Fatalf("\t%s\tShould be able to get back the same gallery.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same gallery.", tests.Success)

			//----------------------------------------------------------------------
			// Delete the gallery.

			if err := gallery.Delete(tests.Context, db, g.ID.Hex()); err != nil {
				t.Fatalf("\t%s\tShould be able to delete the gallery : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to delete the gallery.", tests.Success)

			//----------------------------------------------------------------------
			// Get the gallery.

			_, err = gallery.Retrieve(tests.Context, db, g.ID.Hex())
			if err == nil {
				t.Fatalf("\t%s\tShould generate an error when getting a gallery with the deleted id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting a gallery with the deleted id.", tests.Success)
		}
	}
}
Ejemplo n.º 2
0
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)
		}
	}
}