Exemple #1
0
// execute takes a context and Set and executes the set returning
// any possible response.
func execute(c *web.Context, set *query.Set, vars map[string]string) error {

	// Parse the vars in the query string.
	if c.Request.URL.RawQuery != "" {
		if m, err := url.ParseQuery(c.Request.URL.RawQuery); err == nil {
			if vars == nil {
				vars = make(map[string]string)
			}
			for k, v := range m {
				vars[k] = v[0]
			}
		}
	}

	// Get the result.
	result := xenia.Exec(c.SessionID, c.Ctx["DB"].(*db.DB), set, vars)

	c.Respond(result, http.StatusOK)
	return nil
}
Exemple #2
0
// TestExecuteSet tests the execution of different Sets that should succeed.
func TestExecuteSet(t *testing.T) {
	db := setup(t)
	defer teardown(t, db)

	// Build our table of the different test sets.
	execSets := []struct {
		typ string
		set []execSet
	}{
		{typ: "Positive", set: getPosExecSet()},
		{typ: "Negative", set: getNegExecSet()},
	}

	// Iterate over all the different test sets.
	for _, execSet := range execSets {

		t.Logf("Given the need to execute %s mongo tests.", execSet.typ)
		{
			for _, es := range execSet.set {

				// Setup a sub-test for each item.
				tf := func(t *testing.T) {
					t.Logf("\tWhen using Execute Set %s", es.set.Name)
					{
						result := xenia.Exec(tests.Context, db, es.set, es.vars)

						data, err := json.Marshal(result)
						if err != nil {
							t.Errorf("\t%s\tShould be able to marshal the result : %s", tests.Failed, err)
							return
						}
						t.Logf("\t%s\tShould be able to marshal the result.", tests.Success)

						var res query.Result
						if err := json.Unmarshal(data, &res); err != nil {
							t.Errorf("\t%s\tShould be able to unmarshal the result : %s", tests.Failed, err)
							return
						}
						t.Logf("\t%s\tShould be able to unmarshal the result.", tests.Success)

						// This support allowing the test to provide multiple documents
						// to check when data value order can be underterminstic.
						var found bool
						for _, rslt := range es.results {

							// We just need to find the string inside the result.
							if strings.HasPrefix(rslt, "#find:") {
								if strings.Contains(string(data), rslt[6:]) {
									found = true
									break
								}
								continue
							}

							// Compare the entire result.
							if string(data) == rslt {
								found = true
								break
							}
						}

						if !found {
							t.Log("Exp:", string(data))
							for _, rslt := range es.results {
								t.Log("Rsl:", rslt)
							}
							t.Errorf("\t%s\tShould have the correct result.", tests.Failed)
							return
						}
						t.Logf("\t%s\tShould have the correct result", tests.Success)
					}
				}

				t.Run(es.set.Name, tf)
			}
		}
	}
}