github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/tools/soda/fizz/create_table_test.go (about) 1 package fizz 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func Test_Create_Table(t *testing.T) { 9 ac := createTable{} 10 t.Run("no table name", func(t *testing.T) { 11 _, _, err := ac.GenerateFizz("create_table_", []string{}) 12 if err == nil { 13 t.Error("should error but got nil") 14 } 15 }) 16 17 t.Run("with table name and no args", func(t *testing.T) { 18 up, down, err := ac.GenerateFizz("create_table_users", []string{}) 19 if err != nil { 20 t.Error("should not be nil but got err") 21 } 22 23 expectedUP := `create_table("users")` 24 expectedDown := `drop_table("users")` 25 26 if !strings.Contains(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("with table name and args", func(t *testing.T) { 36 up, down, err := ac.GenerateFizz("create_table_users", []string{"email"}) 37 if err != nil { 38 t.Error("should not be nil but got err") 39 } 40 41 expectedUP1 := `create_table("users")` 42 expectedUP2 := `t.Column("email", "string", {})` 43 expectedDown := `drop_table("users")` 44 45 if !strings.Contains(up, expectedUP1) { 46 t.Errorf("expected %v but got %v", expectedUP1, up) 47 } 48 49 if !strings.Contains(up, expectedUP2) { 50 t.Errorf("expected %v but got %v", expectedUP2, up) 51 } 52 53 if down != expectedDown { 54 t.Errorf("expected %v but got %v", expectedDown, down) 55 } 56 }) 57 } 58 59 func Test_Create_Table_Matches(t *testing.T) { 60 ac := createTable{} 61 62 cases := []struct { 63 name string 64 expected bool 65 }{ 66 {name: "create_table_", expected: true}, 67 {name: "create_table", expected: false}, 68 {name: "create_table_users", expected: true}, 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 }