// loadScript adds scripts to run tests. func loadScript(db *db.DB, file string) error { scr, err := sfix.Get(file) if err != nil { return err } if err := sfix.Add(db, scr); err != nil { return err } return nil }
// setup initializes for each indivdual test. func setup(t *testing.T, fixture string) (script.Script, *db.DB) { tests.ResetLog() scr, err := sfix.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 scr, db }
// loadTestData adds all the test data into the database. func loadTestData(t *testing.T, db *db.DB) { t.Log("\tWhen loading data for the tests") { err := tstdata.Generate(db) if err != nil { t.Fatalf("\t%s\tShould be able to load system with test data : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to load system with test data.", tests.Success) scripts := []string{ "basic_script_pre.json", "basic_script_pst.json", } for _, file := range scripts { scr, err := sfix.Get(file) if err != nil { t.Fatalf("\t%s\tShould load script document from file : %v", tests.Failed, err) } t.Logf("\t%s\tShould load script document from file.", tests.Success) // We need these scripts loaded under another name to allow tests // to run in parallel. scr.Name = strings.Replace(scr.Name, "STEST_O", "STEST_T", 1) if err := script.Upsert(tests.Context, db, scr); 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) } masks, err := mfix.Get("basic.json") if err != nil { t.Fatalf("\t%s\tShould load mask documents from file : %v", tests.Failed, err) } t.Logf("\t%s\tShould load mask documents from file.", tests.Success) for _, msk := range masks { if err := mfix.Add(db, msk); err != nil { t.Fatalf("\t%s\tShould be able to create a mask : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a mask.", 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) } } }