// TestNamedOnView tests the execution of a specific named query on a view. func TestNamedOnView(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to execute a specific query.") { url := "/v1/exec/" + qPrefix + "_basic_view/view/VTEST_thread/ITEST_c1b2bbfe-af9f-4903-8777-bd47c4d5b20a?item_of_interest=ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82" r := httptest.NewRequest("GET", url, nil) 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) } } }
// TestExec tests the execution of a specific query. func TestExec(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to execute a specific query.") { url := "/v1/exec/" + qPrefix + "_basic?station_id=42021" r := httptest.NewRequest("GET", url, nil) 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) } } }
// 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) } } }
// 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) } } }
// TestScriptUpsert tests the insert and update of a script. func TestScriptUpsert(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to insert and then update a script.") { //---------------------------------------------------------------------- // Get the fixture. scr, err := sfix.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) scrStrData, err := json.Marshal(&scr) 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 Script. url := "/v1/script" r := httptest.NewRequest("PUT", url, bytes.NewBuffer(scrStrData)) 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 script : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to insert the script.", tests.Success) } //---------------------------------------------------------------------- // Retrieve the Script. url = "/v1/script/" + sPrefix + "_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 script : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the script.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"name":"` + sPrefix + `_upsert","commands":[{"command.one":1},{"command":2},{"command":3}]}`) 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 Script. scr.Commands = append(scr.Commands, map[string]interface{}{"command": 4}) scrStrData, err = json.Marshal(&scr) 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/script" r = httptest.NewRequest("PUT", url, bytes.NewBuffer(scrStrData)) 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 script : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to update the script.", tests.Success) } //---------------------------------------------------------------------- // Retrieve the Script. url = "/v1/script/" + sPrefix + "_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 script : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the script.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"name":"` + sPrefix + `_upsert","commands":[{"command.one":1},{"command":2},{"command":3},{"command":4}]}`) 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) } } }
// 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) } } }
// TestMaskUpsert tests the insert and update of a mask. func TestMaskUpsert(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() t.Log("Given the need to insert and then update a mask.") { //---------------------------------------------------------------------- // Get the fixture. masks, err := mfix.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) masks[0].Field = "test_insert" masks[0].Type = "left" mskStrData, err := json.Marshal(masks[0]) 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 Mask. url := "/v1/mask" r := httptest.NewRequest("PUT", url, bytes.NewBuffer(mskStrData)) 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 mask : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to insert the mask.", tests.Success) } //---------------------------------------------------------------------- // Retrieve the Mask. url = "/v1/mask/" + mCollection + "/test_insert" 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 mask : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the mask.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"collection":"` + mCollection + `","field":"test_insert","type":"left"}`) 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 Mask. masks[0].Type = "right" mskStrData, err = json.Marshal(masks[0]) 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/mask" r = httptest.NewRequest("PUT", url, bytes.NewBuffer(mskStrData)) 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 mask : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to update the mask.", tests.Success) } //---------------------------------------------------------------------- // Retrieve the Mask. url = "/v1/mask/" + mCollection + "/test_insert" 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 mask : %v", tests.Failed, w.Code) } t.Logf("\t%s\tShould be able to retrieve the mask.", tests.Success) recv := tests.IndentJSON(w.Body.String()) resp := tests.IndentJSON(`{"collection":"` + mCollection + `","field":"test_insert","type":"right"}`) 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) } } }