github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/soda/generator_test.go (about) 1 package soda 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/wawandco/ox/plugins/core" 11 "github.com/wawandco/ox/plugins/tools/soda/fizz" 12 "github.com/wawandco/ox/plugins/tools/soda/sql" 13 ) 14 15 func Test_Generate(t *testing.T) { 16 g := Generator{} 17 g.Receive([]core.Plugin{&fizz.Creator{}, &sql.Creator{}}) 18 19 t.Run("generate fizz migration", func(t *testing.T) { 20 dir := t.TempDir() 21 args := []string{"generate", "migration", "users", "--type=fizz"} 22 23 g.ParseFlags(args) 24 if err := g.Generate(context.Background(), dir, args); err != nil { 25 t.Errorf("should not be error, but got %v", err) 26 } 27 28 // Validating Files existence 29 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*users.*.fizz")) 30 if err != nil { 31 t.Errorf("searching for files should not error, but got %v", err) 32 } 33 34 if len(match) == 0 { 35 t.Error("migration files does not exists on the path") 36 } 37 38 if !strings.HasSuffix(match[0], "_users.down.fizz") { 39 t.Error("'users.up.fizz' file does not exists on the path") 40 } 41 42 if !strings.HasSuffix(match[1], "_users.up.fizz") { 43 t.Error("'users.down.fizz' file does not exists on the path") 44 } 45 }) 46 47 t.Run("generate fizz migration with flag in another position", func(t *testing.T) { 48 dir := t.TempDir() 49 args := []string{"generate", "migration", "--type=fizz", "users"} 50 51 g.ParseFlags(args) 52 if err := g.Generate(context.Background(), dir, args); err != nil { 53 t.Errorf("should not be error, but got %v", err) 54 } 55 56 // Validating Files existence 57 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*users.*.fizz")) 58 if err != nil { 59 t.Errorf("searching for files should not error, but got %v", err) 60 } 61 62 if len(match) == 0 { 63 t.Error("migration files does not exists on the path") 64 } 65 66 if !strings.HasSuffix(match[0], "_users.down.fizz") { 67 t.Error("'users.up.fizz' file does not exists on the path") 68 } 69 70 if !strings.HasSuffix(match[1], "_users.up.fizz") { 71 t.Error("'users.down.fizz' file does not exists on the path") 72 } 73 }) 74 75 t.Run("generate sql migration", func(t *testing.T) { 76 dir := t.TempDir() 77 args := []string{"generate", "migration", "company", "--type=sql"} 78 g.ParseFlags(args) 79 80 if err := g.Generate(context.Background(), dir, args); err != nil { 81 t.Errorf("should not be error, but got %v", err) 82 } 83 84 // Validating Files existence 85 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*companies.*.sql")) 86 if err != nil { 87 t.Errorf("searching for files should not error, but got %v", err) 88 } 89 90 if len(match) == 0 { 91 t.Error("migration files does not exists on the path") 92 } 93 94 if !strings.HasSuffix(match[0], "_companies.down.sql") { 95 t.Error("'companies.up.sql' file does not exists on the path") 96 } 97 98 if !strings.HasSuffix(match[1], "_companies.up.sql") { 99 t.Error("'companies.down.sql' file does not exists on the path") 100 } 101 }) 102 103 t.Run("generate sql migration with flag in another position", func(t *testing.T) { 104 dir := t.TempDir() 105 args := []string{"generate", "migration", "--type=sql", "company"} 106 g.ParseFlags(args) 107 108 if err := g.Generate(context.Background(), dir, args); err != nil { 109 t.Errorf("should not be error, but got %v", err) 110 } 111 112 // Validating Files existence 113 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*companies.*.sql")) 114 if err != nil { 115 t.Errorf("searching for files should not error, but got %v", err) 116 } 117 118 if len(match) == 0 { 119 t.Error("migration files does not exists on the path") 120 } 121 122 if !strings.HasSuffix(match[0], "_companies.down.sql") { 123 t.Error("'companies.up.sql' file does not exists on the path") 124 } 125 126 if !strings.HasSuffix(match[1], "_companies.up.sql") { 127 t.Error("'companies.down.sql' file does not exists on the path") 128 } 129 }) 130 131 t.Run("generate invalid migration should error", func(t *testing.T) { 132 args := []string{"generate", "migration", "company", "--type=invalid"} 133 g.ParseFlags(args) 134 135 if err := g.Generate(context.Background(), t.TempDir(), args); err == nil { 136 t.Errorf("should be error, but got %v", err) 137 } 138 }) 139 140 t.Run("generate migration without type should generate fizz", func(t *testing.T) { 141 dir := t.TempDir() 142 args := []string{"generate", "migration", "templates"} 143 g.ParseFlags(args) 144 145 if err := g.Generate(context.Background(), dir, args); err != nil { 146 t.Errorf("should not be error, but got %v", err) 147 } 148 149 // Validating Files existence 150 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*templates.*.fizz")) 151 if err != nil { 152 t.Errorf("searching for files should not error, but got %v", err) 153 } 154 155 if len(match) == 0 { 156 t.Error("migration files does not exists on the path") 157 } 158 159 if !strings.HasSuffix(match[0], "_templates.down.fizz") { 160 t.Error("'templates.up.fizz' file does not exists on the path") 161 } 162 163 if !strings.HasSuffix(match[1], "_templates.up.fizz") { 164 t.Error("'templates.down.fizz' file does not exists on the path") 165 } 166 }) 167 168 t.Run("generate migration with empty type should generate fizz", func(t *testing.T) { 169 dir := t.TempDir() 170 args := []string{"generate", "migration", "invoices", "--type"} 171 g.ParseFlags(args) 172 173 if err := g.Generate(context.Background(), dir, args); err != nil { 174 t.Errorf("should not be error, but got %v", err) 175 } 176 177 // Validating Files existence 178 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*invoices.*.fizz")) 179 if err != nil { 180 t.Errorf("searching for files should not error, but got %v", err) 181 } 182 183 if len(match) == 0 { 184 t.Error("migration files does not exists on the path") 185 } 186 187 if !strings.HasSuffix(match[0], "_invoices.down.fizz") { 188 t.Error("'invoices.up.fizz' file does not exists on the path") 189 } 190 191 if !strings.HasSuffix(match[1], "_invoices.up.fizz") { 192 t.Error("'invoices.down.fizz' file does not exists on the path") 193 } 194 }) 195 196 t.Run("generate fizz migration with args", func(t *testing.T) { 197 dir := t.TempDir() 198 args := []string{"generate", "migration", "create_table_users", "description:string", "quantity:int"} 199 g.ParseFlags(args) 200 201 if err := g.Generate(context.Background(), dir, args); err != nil { 202 t.Errorf("should not be error, but got %v", err) 203 } 204 205 // Validating Files existence 206 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*users.*.fizz")) 207 if err != nil { 208 t.Errorf("searching for files should not error, but got %v", err) 209 } 210 211 if len(match) == 0 { 212 t.Error("migration files does not exists on the path") 213 } 214 215 if !strings.HasSuffix(match[0], "_users.down.fizz") { 216 t.Error("'users.up.fizz' file does not exists on the path") 217 } 218 219 if !strings.HasSuffix(match[1], "_users.up.fizz") { 220 t.Error("'users.down.fizz' file does not exists on the path") 221 } 222 223 // Validating existence of the attributes 224 dropFile, err := os.ReadFile(match[0]) 225 if err != nil { 226 t.Error("reading migration down file error") 227 } 228 229 if string(dropFile) != `drop_table("users")` { 230 t.Error(`unexpected content, file should contain 'drop_table("users")'`) 231 } 232 233 createFile, err := os.ReadFile(match[1]) 234 if err != nil { 235 t.Error("reading migration down file error") 236 } 237 238 upData := string(createFile) 239 shouldContain := []string{`create_table("users")`, "t.Column", "description", "quantity", "string", "integer"} 240 for _, contain := range shouldContain { 241 if !strings.Contains(upData, contain) { 242 t.Errorf("unexpected content, file should contain '%s'", contain) 243 } 244 } 245 }) 246 }