// loadQuery adds queries to run tests. func loadQuery(db *db.DB, file string) error { set, err := qfix.Get(file) if err != nil { return err } if err := qfix.Add(db, set); err != nil { return err } return nil }
// setup initializes for each indivdual test. func setup(t *testing.T, fixture string) (*query.Set, *db.DB) { tests.ResetLog() set, err := qfix.Get(fixture) if err != nil { t.Fatalf("%s\tShould load query mask record from file : %v", tests.Failed, err) } t.Logf("%s\tShould load query mask record from file.", tests.Success) db, err := db.NewMGO(tests.Context, tests.TestSession) if err != nil { t.Fatalf("%s\tShould be able to get a Mongo session : %v", tests.Failed, err) } return set, db }
// TestExecExplain tests the execution of a custom query with explain. func TestExecExplain(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to execute a custom query with explain.") { qs, err := qfix.Get("basic.json") if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the fixture.", tests.Success) qs.Explain = true qsStrData, err := json.Marshal(&qs) if err != nil { t.Fatalf("\t%s\tShould be able to marshal the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to marshal the fixture.", tests.Success) url := "/v1/exec" r := httptest.NewRequest("POST", url, bytes.NewBuffer(qsStrData)) w := httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url : %s", url) { if w.Code != http.StatusOK { t.Fatalf("\t%s\tShould be able to retrieve the query : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the query.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := `queryPlanner` if !strings.Contains(recv, resp) { t.Log(resp) t.Log(recv) t.Fatalf("\t%s\tShould get the expected result.", tests.Failed) } t.Logf("\t%s\tShould get the expected result.", tests.Success) } } }
// TestCustomOnView tests the execution of a custom query on a view. func TestExecCustomOnView(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to execute a custom query on a view.") { qs, err := qfix.Get("basic_view.json") if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the fixture.", tests.Success) qsStrData, err := json.Marshal(&qs) if err != nil { t.Fatalf("\t%s\tShould be able to marshal the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to marshal the fixture.", tests.Success) url := "/v1/exec/view/VTEST_thread/ITEST_c1b2bbfe-af9f-4903-8777-bd47c4d5b20a?item_of_interest=ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82" r := httptest.NewRequest("POST", url, bytes.NewBuffer(qsStrData)) w := httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url : %s", url) { if w.Code != http.StatusOK { t.Fatalf("\t%s\tShould be able to retrieve the query : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the query.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"results":[{"Name":"BasicView","Docs":[{"item_id":"ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82"}]}]}`) if resp != recv { t.Log(resp) t.Log(recv) t.Fatalf("\t%s\tShould get the expected result.", tests.Failed) } t.Logf("\t%s\tShould get the expected result.", tests.Success) } } }
// TestExecCustom tests the execution of a custom query. func TestExecCustom(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to execute a custom query.") { qs, err := qfix.Get("basic.json") if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the fixture.", tests.Success) qsStrData, err := json.Marshal(&qs) if err != nil { t.Fatalf("\t%s\tShould be able to marshal the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to marshal the fixture.", tests.Success) url := "/v1/exec" r := httptest.NewRequest("POST", url, bytes.NewBuffer(qsStrData)) w := httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url : %s", url) { if w.Code != http.StatusOK { t.Fatalf("\t%s\tShould be able to retrieve the query : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the query.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"results":[{"Name":"Basic","Docs":[{"name":"C14 - Pasco County Buoy, FL"}]}]}`) if resp != recv { t.Log(resp) t.Log(recv) t.Fatalf("\t%s\tShould get the expected result.", tests.Failed) } t.Logf("\t%s\tShould get the expected result.", tests.Success) } } }
// TestQueryUpsert tests the insert and update of a set. func TestQueryUpsert(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to insert and then update a set.") { //---------------------------------------------------------------------- // Get the fixture. qs, err := qfix.Get("upsert.json") if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the fixture.", tests.Success) qsStrData, err := json.Marshal(&qs) if err != nil { t.Fatalf("\t%s\tShould be able to marshal the fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to marshal the fixture.", tests.Success) //---------------------------------------------------------------------- // Insert the Set. url := "/v1/query" r := httptest.NewRequest("PUT", url, bytes.NewBuffer(qsStrData)) w := httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url to insert : %s", url) { if w.Code != 204 { t.Fatalf("\t%s\tShould be able to insert the set : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to insert the set.", tests.Success) } //---------------------------------------------------------------------- // Ensure the indexes. url = "/v1/index/QTEST_O_upsert" r = httptest.NewRequest("PUT", url, nil) w = httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url to ensure indexes : %s", url) { if w.Code != 204 { t.Fatalf("\t%s\tShould be able to ensure indexes for the set : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to ensure indexes for the set.", tests.Success) } //---------------------------------------------------------------------- // Retrieve the Set. url = "/v1/query/" + qPrefix + "_upsert" r = httptest.NewRequest("GET", url, nil) w = httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url to get : %s", url) { if w.Code != 200 { t.Fatalf("\t%s\tShould be able to retrieve the set : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the set.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"name":"` + qPrefix + `_upsert","desc":"","pre_script":"","pst_script":"","params":[],"queries":[{"name":"Upsert","type":"pipeline","collection":"test_xenia_data","commands":[{"$match":{"station.d":"42021"}},{"$project":{"_id":0,"name":1}}],"indexes":[{"key":["station_id"],"unique":true}],"return":true}],"enabled":true,"explain":false}`) if resp != recv { t.Log(resp) t.Log(w.Body.String()) t.Fatalf("\t%s\tShould get the expected result.", tests.Failed) } t.Logf("\t%s\tShould get the expected result.", tests.Success) } //---------------------------------------------------------------------- // Update the Set. qs.Description = "C" qsStrData, err = json.Marshal(&qs) if err != nil { t.Fatalf("\t%s\tShould be able to marshal the changed fixture : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to marshal the changed fixture.", tests.Success) url = "/v1/query" r = httptest.NewRequest("PUT", url, bytes.NewBuffer(qsStrData)) w = httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url to update : %s", url) { if w.Code != 204 { t.Fatalf("\t%s\tShould be able to update the set : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to update the set.", tests.Success) } //---------------------------------------------------------------------- // Retrieve the Set. url = "/v1/query/" + qPrefix + "_upsert" r = httptest.NewRequest("GET", url, nil) w = httptest.NewRecorder() a.ServeHTTP(w, r) t.Logf("\tWhen calling url to get : %s", url) { if w.Code != 200 { t.Fatalf("\t%s\tShould be able to retrieve the set : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the set.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"name":"` + qPrefix + `_upsert","desc":"C","pre_script":"","pst_script":"","params":[],"queries":[{"name":"Upsert","type":"pipeline","collection":"test_xenia_data","commands":[{"$match":{"station.d":"42021"}},{"$project":{"_id":0,"name":1}}],"indexes":[{"key":["station_id"],"unique":true}],"return":true}],"enabled":true,"explain":false}`) if resp != recv { t.Log(resp) t.Log(w.Body.String()) t.Fatalf("\t%s\tShould get the expected result.", tests.Failed) } t.Logf("\t%s\tShould get the expected result.", tests.Success) } } }