Ejemplo n.º 1
0
// TestUpsertCreateSet tests if we can create a Set record in the db.
func TestUpsertCreateSet(t *testing.T) {
	const fixture = "basic_fldsub.json"
	set1, db := setup(t, fixture)
	defer teardown(t, db)

	t.Log("Given the need to save a query set into the database.")
	{
		t.Log("\tWhen using fixture", fixture)
		{
			if err := query.Upsert(tests.Context, db, set1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a query set : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a query set.", tests.Success)

			if _, err := query.GetLastHistoryByName(tests.Context, db, set1.Name); err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the query set from history: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the query set from history.", tests.Success)

			set2, err := query.GetByName(tests.Context, db, set1.Name)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the query set : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the query set.", tests.Success)

			if !reflect.DeepEqual(*set1, *set2) {
				t.Logf("\t%+v", set1)
				t.Logf("\t%+v", set2)
				t.Errorf("\t%s\tShould be able to get back the same query values.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the same query values.", tests.Success)
			}
		}
	}
}
Ejemplo n.º 2
0
// TestUnknownName validates the behaviour of the query API when using a invalid/
// unknown query name.
func TestUnknownName(t *testing.T) {
	const fixture = "basic.json"
	set1, db := setup(t, fixture)
	defer teardown(t, db)

	qsName := prefix + "_unknown"

	t.Log("Given the need to validate bad query name response.")
	{
		t.Log("\tWhen using fixture", fixture)
		{
			if err := query.Upsert(tests.Context, db, set1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a query set : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a query set.", tests.Success)

			if _, err := query.GetByName(tests.Context, db, qsName); err == nil {
				t.Fatalf("\t%s\tShould be able to validate query set with Name[%s] does not exists: %s", tests.Failed, qsName, errors.New("Record Exists"))
			}
			t.Logf("\t%s\tShould be able to validate query set with Name[%s] does not exists.", tests.Success, qsName)

			if err := query.Delete(tests.Context, db, qsName); err == nil {
				t.Fatalf("\t%s\tShould be able to validate query set with Name[%s] can not be deleted: %s", tests.Failed, qsName, errors.New("Record Exists"))
			}
			t.Logf("\t%s\tShould be able to validate query set with Name[%s] can not be deleted.", tests.Success, qsName)
		}
	}
}
Ejemplo n.º 3
0
// TestDeleteSet validates the removal of a query from the database.
func TestDeleteSet(t *testing.T) {
	const fixture = "basic.json"
	set1, db := setup(t, fixture)
	defer teardown(t, db)

	qsName := prefix + "_basic"
	qsBadName := prefix + "_basic_advice"

	t.Log("Given the need to delete a query set in the database.")
	{
		t.Log("\tWhen using fixture", fixture)
		{
			if err := query.Upsert(tests.Context, db, set1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a query set : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a query set.", tests.Success)

			if err := query.Delete(tests.Context, db, qsName); err != nil {
				t.Fatalf("\t%s\tShould be able to delete a query set using its name[%s]: %s", tests.Failed, qsName, err)
			}
			t.Logf("\t%s\tShould be able to delete a query set using its name[%s]:", tests.Success, qsName)

			if err := query.Delete(tests.Context, db, qsBadName); err == nil {
				t.Fatalf("\t%s\tShould not be able to delete a query set using wrong name name[%s]", tests.Failed, qsBadName)
			}
			t.Logf("\t%s\tShould not be able to delete a query set using wrong name name[%s]", tests.Success, qsBadName)

			if _, err := query.GetByName(tests.Context, db, qsName); err == nil {
				t.Fatalf("\t%s\tShould be able to validate query set with Name[%s] does not exists: %s", tests.Failed, qsName, errors.New("Record Exists"))
			}
			t.Logf("\t%s\tShould be able to validate query set with Name[%s] does not exists:", tests.Success, qsName)
		}
	}
}
Ejemplo n.º 4
0
// TestUpsertUpdateQuery validates update operation of a given query Set.
func TestUpsertUpdateQuery(t *testing.T) {
	const fixture = "basic.json"
	set1, db := setup(t, fixture)
	defer teardown(t, db)

	t.Log("Given the need to update a query set into the database.")
	{
		t.Log("\tWhen using fixture", fixture)
		{
			if err := query.Upsert(tests.Context, db, set1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a query set : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a query set.", tests.Success)

			set2 := *set1
			set2.Params = append(set2.Params, query.Param{
				Name:    "group",
				Default: "1",
				Desc:    "provides the group number for the query script",
			})

			if err := query.Upsert(tests.Context, db, &set2); err != nil {
				t.Fatalf("\t%s\tShould be able to update a query set record: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to update a query set record.", tests.Success)

			if _, err := query.GetLastHistoryByName(tests.Context, db, set1.Name); err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the query set from history: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the query set from history.", tests.Success)

			updSet, err := query.GetByName(tests.Context, db, set2.Name)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve a query set record: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve a query set record.", tests.Success)

			if updSet.Name != set1.Name {
				t.Errorf("\t%s\tShould be able to get back the same query set name.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the same query set name.", tests.Success)
			}

			if lendiff := len(updSet.Params) - len(set1.Params); lendiff != 1 {
				t.Errorf("\t%s\tShould have one more parameter in set record: %d", tests.Failed, lendiff)
			} else {
				t.Logf("\t%s\tShould have one more parameter in set record.", tests.Success)
			}

			if !reflect.DeepEqual(set2.Params[0], updSet.Params[0]) {
				t.Logf("\t%+v", set2.Params[0])
				t.Logf("\t%+v", updSet.Params[0])
				t.Errorf("\t%s\tShould be abe to validate the query param values in db.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be abe to validate the query param values in db.", tests.Success)
			}
		}
	}
}
Ejemplo n.º 5
0
// TestAPIFailureSet validates the failure of the api using a nil session.
func TestAPIFailureSet(t *testing.T) {
	const fixture = "basic.json"
	set1, db := setup(t, fixture)
	defer teardown(t, db)

	qsName := prefix + "_unknown"

	t.Log("Given the need to validate failure of API with bad session.")
	{
		t.Log("When giving a nil session")
		{
			err := query.EnsureIndexes(tests.Context, nil, set1)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused create by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused create by api with bad session: %s", tests.Success, err)

			err = query.Upsert(tests.Context, nil, set1)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused create by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused create by api with bad session: %s", tests.Success, err)

			_, err = query.GetNames(tests.Context, nil)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			_, err = query.GetAll(tests.Context, nil, nil)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			_, err = query.GetByName(tests.Context, nil, qsName)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			_, err = query.GetLastHistoryByName(tests.Context, nil, qsName)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			err = query.Delete(tests.Context, nil, qsName)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused delete by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused delete by api with bad session: %s", tests.Success, err)
		}
	}
}
Ejemplo n.º 6
0
// Retrieve returns the specified Set from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (queryHandle) Retrieve(c *web.Context) error {
	set, err := query.GetByName(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"])
	if err != nil {
		if err == query.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(set, http.StatusOK)
	return nil
}
Ejemplo n.º 7
0
// Name runs the specified Set and returns results.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (execHandle) Name(c *web.Context) error {
	set, err := query.GetByName(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"])
	if err != nil {
		if err == query.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	var vars map[string]string

	return execute(c, set, vars)
}
Ejemplo n.º 8
0
// EnsureIndexes makes sure indexes for the specified set exist.
// 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal
func (queryHandle) EnsureIndexes(c *web.Context) error {
	db := c.Ctx["DB"].(*db.DB)

	set, err := query.GetByName(c.SessionID, db, c.Params["name"])
	if err != nil {
		if err == query.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	if err := query.EnsureIndexes(c.SessionID, db, set); err != nil {
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}