// loadMasks adds masks to run tests. func loadMasks(db *db.DB, file string) error { masks, err := mfix.Get(file) if err != nil { return err } for _, msk := range masks { if err := mfix.Add(db, msk); err != nil { return err } } return nil }
// setup initializes for each indivdual test. func setup(t *testing.T, fixture string) ([]mask.Mask, *db.DB) { tests.ResetLog() masks, err := mfix.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 masks, 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) } } }
// 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) } } }