// TestInvalidRegex tests that bad regex expressions cause errors. func TestInvalidRegex(t *testing.T) { tests.ResetLog() defer tests.DisplayLog() const fixture1 = "invalid.json" const fixture2 = "badpattern.json" // Load and validate a Regex with too short of a pattern. rgx1, err := rfix.Get(fixture1) if err != nil { t.Fatalf("\t%s\tShould load regex record from file : %v", tests.Failed, err) } t.Logf("\t%s\tShould load regex record from file.", tests.Success) // Load a Regex with a pattern that doesn't compile. rgx2, err := rfix.Get(fixture2) if err != nil { t.Fatalf("\t%s\tShould load regex record from file : %v", tests.Failed, err) } t.Logf("\t%s\tShould load regex record from file.", tests.Success) t.Log("Given the need to reject an invalid or too short a pattern regex from the database.") { t.Log("\tWhen using fixture", fixture1) { err = rgx1.Validate() if err == nil { t.Errorf("\t%s\tShould error on Regex with too short a pattern.", tests.Failed) } t.Logf("\t%s\tShould error on Regex with too short a pattern.", tests.Success) } t.Log("\tWhen using fixture", fixture2) { err = rgx2.Validate() if err == nil { t.Errorf("\t%s\tShould error on Regex with an invalid pattern.", tests.Failed) } t.Logf("\t%s\tShould error on Regex with an invalid pattern.", tests.Success) } } }
// loadRegex adds regex to run tests. func loadRegex(db *db.DB, file string) error { rg, err := rfix.Get(file) if err != nil { return err } if err := rfix.Add(db, rg); err != nil { return err } return nil }
// setup initializes for each indivdual test. func setup(t *testing.T, fixture string) (regex.Regex, *db.DB) { tests.ResetLog() rgx, err := rfix.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 rgx, db }