Пример #1
0
func setup(t *testing.T, fixture string) ([]submission.Submission, *db.DB) {
	tests.ResetLog()

	subs, err := submissionfix.GetMany("submission.json")
	if err != nil {
		t.Fatalf("%s\tShould be able retrieve submission fixture : %s", tests.Failed, err)
	}
	t.Logf("%s\tShould be able retrieve submission fixture.", tests.Success)

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("Should be able to get a Mongo session : %v", err)
	}

	return subs, db
}
Пример #2
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)
		}
	}
}