Ejemplo n.º 1
0
// 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
}
Ejemplo n.º 2
0
// Add inserts forms for testing.
func Add(context interface{}, db *db.DB, fms []form.Form) error {
	for _, fm := range fms {
		if err := form.Upsert(context, db, &fm); err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 3
0
// Add inserts forms for testing.
func Add(context interface{}, db *db.DB, fm *form.Form, subs []submission.Submission) error {
	if err := form.Upsert(context, db, fm); err != nil {
		return err
	}

	for _, sub := range subs {
		if err := submission.Create(context, db, fm.ID.Hex(), &sub); err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 4
0
func TestUpsertDelete(t *testing.T) {
	fms, db := setupForms(t, "form")
	defer teardownForms(t, db)

	t.Log("Given the need to upsert and delete forms.")
	{
		t.Log("\tWhen starting from an empty forms collection")
		{
			//----------------------------------------------------------------------
			// Upsert the form.

			if err := form.Upsert(tests.Context, db, &fms[0]); err != nil {
				t.Fatalf("\t%s\tShould be able to upsert a form : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to upsert a form.", tests.Success)

			//----------------------------------------------------------------------
			// Get the form.

			fm, err := form.Retrieve(tests.Context, db, fms[0].ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the form by id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the form by id.", tests.Success)

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

			if fms[0].ID.Hex() != fm.ID.Hex() {
				t.Fatalf("\t%s\tShould be able to get back the same form.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same form.", tests.Success)

			//----------------------------------------------------------------------
			// Delete the form.

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

			//----------------------------------------------------------------------
			// Get the form.

			_, err = form.Retrieve(tests.Context, db, fms[0].ID.Hex())
			if err == nil {
				t.Fatalf("\t%s\tShould generate an error when getting a form with the deleted id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting a form with the deleted id.", tests.Success)

			//----------------------------------------------------------------------
			// Create a new fresh form.

			fms[0].ID = ""

			if err := form.Upsert(tests.Context, db, &fms[0]); err != nil {
				t.Fatalf("\t%s\tShould be able to upsert a form : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to upsert a form.", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that an ID was set.

			if fms[0].ID == "" {
				t.Fatalf("\t%s\tShould be able to add an ID when upserting a new form : ID was not assigned", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to add an ID when upserting a new form.", tests.Success)

			//----------------------------------------------------------------------
			// Get the form.

			fm, err = form.Retrieve(tests.Context, db, fms[0].ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get the form by id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get the form by id.", tests.Success)

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

			if fms[0].ID.Hex() != fm.ID.Hex() {
				t.Fatalf("\t%s\tShould be able to get back the same form.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same form.", tests.Success)

			//----------------------------------------------------------------------
			// Delete the form.

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

			//----------------------------------------------------------------------
			// Get the form.

			_, err = form.Retrieve(tests.Context, db, fms[0].ID.Hex())
			if err == nil {
				t.Fatalf("\t%s\tShould generate an error when getting a form with the deleted id : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould generate an error when getting a form with the deleted id.", tests.Success)
		}
	}
}
Ejemplo n.º 5
0
func TestUpdateStatus(t *testing.T) {
	fms, db := setupForms(t, "form")
	defer teardownForms(t, db)

	t.Log("Given the need to upsert and delete forms.")
	{
		t.Log("\tWhen starting from an empty forms collection")
		{

			//----------------------------------------------------------------------
			// Upsert the form.

			if err := form.Upsert(tests.Context, db, &fms[0]); err != nil {
				t.Fatalf("\t%s\tShould be able to upsert a form : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to upsert a form.", tests.Success)

			//----------------------------------------------------------------------
			// Update it's status.

			newStatus := "updated_" + time.Now().String()

			fm, err := form.UpdateStatus(tests.Context, db, fms[0].ID.Hex(), newStatus)
			if err != nil {
				t.Logf("\t%s\tShould be able to update a forms status without error : %s", tests.Success, err.Error())
			}
			t.Logf("\t%s\tShould be able to update a forms status without error.", tests.Success)

			//----------------------------------------------------------------------
			// Check we got the right form.

			if fm.ID.Hex() != fms[0].ID.Hex() {
				t.Fatalf("\t%s\tShould be able to retrieve a form given it's id : ID's of retrieved forms do not match", tests.Success)
			}
			t.Logf("\t%s\tShould be able to retrieve a form given it's id.", tests.Success)

			//----------------------------------------------------------------------
			// Check that it's status is changed.

			if fm.Status != newStatus {
				t.Fatalf("\t%s\tShould be able to set the status on the returned form : Expected %s, got %s", tests.Success, newStatus, fm.Status)
			}
			t.Logf("\t%s\tShould be able to set the status on the returned form.", tests.Success)

			//----------------------------------------------------------------------
			// Get a copy from the DB.

			rfm, err := form.Retrieve(tests.Context, db, fm.ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve a form given it's id : %s", tests.Success, err.Error())
			}
			t.Logf("\t%s\tShould be able to retrieve a form given it's id.", tests.Success)

			//----------------------------------------------------------------------
			// Check that the DB copy has it's status changed.

			if rfm.Status != newStatus {
				t.Fatalf("\t%s\tShould be able to update a form's status in the database : Expected %s, got %s", tests.Failed, newStatus, rfm.Status)
			}
			t.Logf("\t%s\tShould be able to update a form's status in the database.", tests.Success)
		}
	}
}
Ejemplo n.º 6
0
func TestList(t *testing.T) {
	fms, db := setupForms(t, "form")
	defer teardownForms(t, db)

	t.Log("Given the need to upsert and delete forms.")
	{
		t.Log("\tWhen starting from an empty forms collection")
		{

			//----------------------------------------------------------------------
			// Upsert the forms.

			if err := form.Upsert(tests.Context, db, &fms[0]); err != nil {
				t.Fatalf("\t%s\tShould be able to upsert a form : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to upsert forms.", tests.Success)

			// This particular looping logic is required at the moment due to issues
			// with the current CI environment.

			limit := 10
			offset := 0

			for {
				lfms, err := form.List(tests.Context, db, limit, offset)
				if err != nil {
					t.Fatalf("\t%s\tShould be able to list forms : %s", tests.Failed, err)
				}
				t.Logf("\t%s\tShould be able to list forms", tests.Success)

				if len(lfms) > limit {
					t.Fatalf("\t%s\tShould only return a maximum of the provided limit : Expected less than %d, got %d", tests.Failed, limit, len(lfms))
				}
				t.Logf("\t%s\tShould only return a maximum of the provided limit.", tests.Success)

				// If we are another page of data (we have itterated at least once) and
				// the length of the results is zero, then we have no more results to
				// paginate over. At this point, we know that we couldn't find all the
				// upserted records.
				if offset > 0 && len(lfms) == 0 {
					t.Fatalf("\t%s\tShould be able to find a form that was upserted : Could not an upserted form in result set", tests.Failed)
				}

				found := false
				for _, fm := range lfms {
					if fm.ID.Hex() == fms[0].ID.Hex() {
						found = true
						break
					}
				}

				// If not all the elements have been found yet, we need to continue the
				// itteration process.
				if !found {
					// Increase the offset by the limit amount.
					offset = offset + limit

					// Continue itteration.
					continue
				}

				// Exit the itteration in the event that all the upserted documents have
				// been found.
				break
			}
			t.Logf("\t%s\tShould be able to find a form that was upserted", tests.Success)
		}
	}
}