Пример #1
0
// TestGetScriptByNames validates retrieval of Script records by a set of names.
func TestGetScriptByNames(t *testing.T) {
	const fixture = "basic.json"
	scr1, db := setup(t, fixture)
	defer teardown(t, db)

	t.Log("Given the need to retrieve a list of script values.")
	{
		t.Log("\tWhen using two scripts")
		{
			if err := script.Upsert(tests.Context, db, scr1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a script.", tests.Success)

			scr2 := scr1
			scr2.Name += "2"
			if err := script.Upsert(tests.Context, db, scr2); err != nil {
				t.Fatalf("\t%s\tShould be able to create a second script : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a second script.", tests.Success)

			scripts, err := script.GetByNames(tests.Context, db, []string{scr1.Name, scr2.Name})
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the scripts by names : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the scripts by names", tests.Success)

			var count int
			for _, scr := range scripts {
				if len(scr.Name) > len(prefix) && scr.Name[0:len(prefix)] == prefix {
					count++
				}
			}

			// When tests are running in parallel with the query and exec package, we could
			// have more scripts.

			if count < 2 {
				t.Fatalf("\t%s\tShould have at least two scripts : %d : %v", tests.Failed, len(scripts), scripts)
			}
			t.Logf("\t%s\tShould have at least two scripts.", tests.Success)

			var found int
			for _, s := range scripts {
				if s.Name == scr1.Name || s.Name == scr2.Name {
					found++
				}
			}

			if found != 2 {
				t.Errorf("\t%s\tShould have retrieve the correct scripts : found[%d]", tests.Failed, found)
			} else {
				t.Logf("\t%s\tShould have retrieve the correct scripts.", tests.Success)
			}
		}
	}
}
Пример #2
0
// TestAPIFailureScripts validates the failure of the api using a nil session.
func TestAPIFailureScripts(t *testing.T) {
	const fixture = "basic.json"
	scr1, db := setup(t, fixture)
	defer teardown(t, db)

	scrName := prefix + "_unknown"

	t.Log("Given the need to validate failure of API with bad session.")
	{
		t.Log("When giving a nil session")
		{
			err := script.Upsert(tests.Context, nil, scr1)
			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 = script.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 = script.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 = script.GetByName(tests.Context, nil, scrName)
			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 = script.GetByNames(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 = script.GetLastHistoryByName(tests.Context, nil, scrName)
			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 = script.Delete(tests.Context, nil, scrName)
			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)
		}
	}
}
Пример #3
0
// loadPrePostScripts updates each query script slice with pre/post commands.
func loadPrePostScripts(context interface{}, db *db.DB, set *query.Set) error {
	if set.PreScript == "" && set.PstScript == "" {
		return nil
	}

	// Load the set of scripts we need to fetch.
	fetchScripts := make([]string, 2)

	if set.PreScript != "" {
		fetchScripts[0] = set.PreScript
	}

	if set.PstScript != "" {
		fetchScripts[1] = set.PstScript
	}

	// Pull all the script documents we need.
	scripts, err := script.GetByNames(context, db, fetchScripts)
	if err != nil {
		return err
	}

	// Add the commands to the query scripts. Since order of the
	// pre/post scripts is maintained, this is simplified.
	for i := range set.Queries {
		if set.PreScript != "" {
			scripts[0].Commands = append(scripts[0].Commands, set.Queries[i].Commands...)
			set.Queries[i].Commands = scripts[0].Commands
		}

		if set.PstScript != "" {
			set.Queries[i].Commands = append(set.Queries[i].Commands, scripts[1].Commands...)
		}
	}

	return nil
}