예제 #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)
		}
	}
}
예제 #2
0
// Retrieve retrieves a FormGallery based on it's id.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formGalleryHandle) Retrieve(c *web.Context) error {
	id := c.Params["id"]

	gallery, err := gallery.Retrieve(c.SessionID, c.Ctx["DB"].(*db.DB), id)
	if err != nil {
		return err
	}

	c.Respond(gallery, http.StatusOK)
	return nil
}
예제 #3
0
func Test_Update(t *testing.T) {
	gs, db := setup(t, "gallery")
	defer teardown(t, db)

	t.Log("Given the need to list 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)

			//----------------------------------------------------------------------
			// Update the gallery.

			newHeadline := "my new headline"

			g.Headline = newHeadline

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

			if g.Headline != newHeadline {
				t.Fatalf("\t%s\tShould update the headline on the returned gallery : Expected \"%s\", got \"%s\"", tests.Failed, newHeadline, g.Headline)
			}
			t.Logf("\t%s\tShould update the headline on the returned gallery.", tests.Success)

			rg, err := gallery.Retrieve(tests.Context, db, g.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)

			if rg.Headline != newHeadline {
				t.Fatalf("\t%s\tShould update the headline on the retrieved gallery : Expected \"%s\", got \"%s\"", tests.Failed, newHeadline, rg.Headline)
			}
			t.Logf("\t%s\tShould update the headline on the retrieved gallery.", tests.Success)
		}
	}
}
예제 #4
0
func Test_Answers(t *testing.T) {
	gs, db := setup(t, "gallery")
	defer teardown(t, db)

	t.Log("Given the need to add and remove answers from galleries.")
	{
		t.Log("\tWhen starting from an empty galleries collection but saturated submissions 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)

			//----------------------------------------------------------------------
			// We need to get/fill in real form submissions.

			subs, err := submissionfix.GetMany("gallery_submissions.json")
			if err != nil {
				t.Fatalf("Should be able to fetch submission fixtures : %v", err)
			}

			// Set the form ID on these new submissions.
			for i := range subs {
				// We need to assign a new submission ID to ensure that we don't collide
				// with existing submissions.
				subs[i].ID = bson.NewObjectId()
				subs[i].FormID = g.FormID
			}

			// Add all the submission fixtures.
			if err := submissionfix.Add(tests.Context, db, subs); err != nil {
				t.Fatalf("Should be able to add submission fixtures : %v", err)
			}

			// Remove the new fixtures after the tests are completed.
			defer func() {
				for _, sub := range subs {
					if err := submission.Delete(tests.Context, db, sub.ID.Hex()); err != nil {
						t.Fatalf("%s\tShould be able to remove submission fixtures : %v", tests.Failed, err)
					}
				}
				t.Logf("%s\tShould be able to remove submission fixtures.", tests.Success)
			}()

			//----------------------------------------------------------------------
			// Add an answer to the gallery.

			crg, err := gallery.AddAnswer(tests.Context, db, g.ID.Hex(), subs[0].ID.Hex(), subs[0].Answers[0].WidgetID)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to add an answer to a gallery : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to add an answer to a gallery.", tests.Success)

			//----------------------------------------------------------------------
			// Check that the returned gallery has the correct answers.

			if len(crg.Answers) != 1 {
				t.Fatalf("\t%s\tShould have at least one answer on the returned gallery : Expected 1, got %d", tests.Failed, len(crg.Answers))
			}
			t.Logf("\t%s\tShould have at least one answer on the returned gallery.", tests.Success)

			matchAnswers(t, crg.Answers[0], subs[0], subs[0].Answers[0])

			//----------------------------------------------------------------------
			// Check that the retrieved gallery has the correct answers.

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

			if len(rg.Answers) != 1 {
				t.Fatalf("\t%s\tShould have at least one answer on the returned gallery : Expected 1, got %d", tests.Failed, len(rg.Answers))
			}
			t.Logf("\t%s\tShould have at least one answer on the returned gallery.", tests.Success)

			matchAnswers(t, rg.Answers[0], subs[0], subs[0].Answers[0])

			//----------------------------------------------------------------------
			// Remove the answer.

			drg, err := gallery.RemoveAnswer(tests.Context, db, g.ID.Hex(), subs[0].ID.Hex(), subs[0].Answers[0].WidgetID)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to remove an answer from a gallery : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to remove an answer from a gallery.", tests.Success)

			//----------------------------------------------------------------------
			// Check that the returned gallery has the correct answers.

			if len(drg.Answers) != 0 {
				t.Fatalf("\t%s\tShould have at no answers on the returned gallery : Expected 0, got %d", tests.Failed, len(drg.Answers))
			}
			t.Logf("\t%s\tShould have at no answers on the returned gallery.", tests.Success)

			//----------------------------------------------------------------------
			// Check that the retrieved gallery has the correct answers.

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

			if len(rg.Answers) != 0 {
				t.Fatalf("\t%s\tShould have at no answers on the returned gallery : Expected 0, got %d", tests.Failed, len(drg.Answers))
			}
			t.Logf("\t%s\tShould have at no answers on the returned gallery.", tests.Success)
		}
	}
}