github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/tools/soda/fizz/add_column_test.go (about) 1 package fizz 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func Test_Add_Column(t *testing.T) { 9 ac := addColumn{} 10 t.Run("no column found", func(t *testing.T) { 11 _, _, err := ac.GenerateFizz("add_description_to_templates", []string{}) 12 if err == nil { 13 t.Error("should error but got nil") 14 } 15 }) 16 17 t.Run("1 argument", func(t *testing.T) { 18 up, down, err := ac.GenerateFizz("adding_money_to_bank", []string{"money:int"}) 19 if err != nil { 20 t.Error("should not be nil but got err") 21 } 22 23 expectedUP := `add_column("bank", "money", "integer", {})` 24 expectedDown := `drop_column("bank", "money")` 25 26 if up != expectedUP { 27 t.Errorf("expected %v but got %v", expectedUP, up) 28 } 29 30 if down != expectedDown { 31 t.Errorf("expected %v but got %v", expectedDown, down) 32 } 33 }) 34 35 t.Run("multiple arguments", func(t *testing.T) { 36 up, down, err := ac.GenerateFizz("adding_columns_to_table", []string{"money:int", "name", "email"}) 37 if err != nil { 38 t.Error("should not be nil but got err") 39 } 40 41 upContains := []string{`add_column("table", "name", "string", {})`, `add_column("table", "email", "string", {})`, `add_column("table", "money", "integer", {})`} 42 downContains := []string{`drop_column("table", "money")`, `drop_column("table", "name")`, `drop_column("table", "email")`} 43 44 for _, v := range upContains { 45 if !strings.Contains(up, v) { 46 t.Errorf("expected %v but got %v", v, up) 47 } 48 } 49 50 for _, v := range downContains { 51 if !strings.Contains(down, v) { 52 t.Errorf("expected %v but got %v", v, down) 53 } 54 } 55 }) 56 } 57 58 func Test_Add_Column_Matches(t *testing.T) { 59 ac := addColumn{} 60 61 cases := []struct { 62 name string 63 expected bool 64 }{ 65 {name: "add_description_to_templates", expected: true}, 66 {name: "adding_money_to_bank", expected: true}, 67 {name: "adding_users", expected: false}, 68 {name: "add_companies", expected: false}, 69 } 70 71 for _, c := range cases { 72 matchs := ac.match(c.name) 73 if matchs != c.expected { 74 t.Errorf("expected %v but got %v", c.expected, matchs) 75 } 76 } 77 }