示例#1
0
文件: ask.go 项目: coralproject/xenia
// UpsertForm upserts the provided form into the MongoDB database collection and
// creates a gallery based on it.
func UpsertForm(context interface{}, db *db.DB, f *form.Form) error {
	log.Dev(context, "UpsertForm", "Started : Form[%s]", f.ID.Hex())

	var isNewForm bool

	// If there was no ID provided, we should set one. UpsertForm might optionally add
	// a form ID to ensure that we don't duplicate the FormGallery.
	if f.ID.Hex() == "" {
		isNewForm = true
	}

	if err := form.Upsert(context, db, f); err != nil {
		log.Error(context, "UpsertForm", err, "Completed")
		return err
	}

	if isNewForm {

		// Create the new gallery that we will create that is based on the current
		// form ID.
		g := gallery.Gallery{
			FormID: f.ID,
		}

		if err := gallery.Create(context, db, &g); err != nil {
			log.Error(context, "UpsertForm", err, "Completed")
			return err
		}
	}

	log.Dev(context, "UpsertForm", "Completed")
	return nil
}
示例#2
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)
		}
	}
}
示例#3
0
// Add inserts gallerys for testing.
func Add(context interface{}, db *db.DB, gs []gallery.Gallery) error {
	for i := range gs {
		// The gallery.Create function will add/update fields so we need to pass
		// the correct reference.
		if err := gallery.Create(context, db, &gs[i]); err != nil {
			return err
		}
	}

	return nil
}
示例#4
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)
		}
	}
}
示例#5
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)
		}
	}
}