github.com/wawandco/oxplugins@v0.7.11/tools/pop/migration/generator_test.go (about) 1 package migration 2 3 import ( 4 "context" 5 "io/ioutil" 6 "path/filepath" 7 "strings" 8 "testing" 9 ) 10 11 func Test_Generate(t *testing.T) { 12 g := Generator{} 13 14 t.Run("generate fizz migration", func(t *testing.T) { 15 dir := t.TempDir() 16 17 if err := g.Generate(context.Background(), dir, []string{"generate", "migration", "users", "--type=fizz"}); err != nil { 18 t.Errorf("should not be error, but got %v", err) 19 } 20 21 // Validating Files existence 22 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*users.*.fizz")) 23 if err != nil { 24 t.Errorf("searching for files should not error, but got %v", err) 25 } 26 27 if len(match) == 0 { 28 t.Error("migration files does not exists on the path") 29 } 30 31 if !strings.HasSuffix(match[0], "_users.down.fizz") { 32 t.Error("'users.up.fizz' file does not exists on the path") 33 } 34 35 if !strings.HasSuffix(match[1], "_users.up.fizz") { 36 t.Error("'users.down.fizz' file does not exists on the path") 37 } 38 }) 39 40 t.Run("generate sql migration", func(t *testing.T) { 41 dir := t.TempDir() 42 43 if err := g.Generate(context.Background(), dir, []string{"generate", "migration", "company", "--type=sql"}); err != nil { 44 t.Errorf("should not be error, but got %v", err) 45 } 46 47 // Validating Files existence 48 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*companies.*.sql")) 49 if err != nil { 50 t.Errorf("searching for files should not error, but got %v", err) 51 } 52 53 if len(match) == 0 { 54 t.Error("migration files does not exists on the path") 55 } 56 57 if !strings.HasSuffix(match[0], "_companies.down.sql") { 58 t.Error("'companies.up.sql' file does not exists on the path") 59 } 60 61 if !strings.HasSuffix(match[1], "_companies.up.sql") { 62 t.Error("'companies.down.sql' file does not exists on the path") 63 } 64 }) 65 66 t.Run("generate invalid migration should error", func(t *testing.T) { 67 if err := g.Generate(context.Background(), t.TempDir(), []string{"generate", "migration", "company", "--type=invalid"}); err == nil { 68 t.Errorf("should be error, but got %v", err) 69 } 70 }) 71 72 t.Run("generate migration without type should generate fizz", func(t *testing.T) { 73 dir := t.TempDir() 74 75 if err := g.Generate(context.Background(), dir, []string{"generate", "migration", "templates"}); err != nil { 76 t.Errorf("should not be error, but got %v", err) 77 } 78 79 // Validating Files existence 80 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*templates.*.fizz")) 81 if err != nil { 82 t.Errorf("searching for files should not error, but got %v", err) 83 } 84 85 if len(match) == 0 { 86 t.Error("migration files does not exists on the path") 87 } 88 89 if !strings.HasSuffix(match[0], "_templates.down.fizz") { 90 t.Error("'templates.up.fizz' file does not exists on the path") 91 } 92 93 if !strings.HasSuffix(match[1], "_templates.up.fizz") { 94 t.Error("'templates.down.fizz' file does not exists on the path") 95 } 96 }) 97 98 t.Run("generate migration with empty type should generate fizz", func(t *testing.T) { 99 dir := t.TempDir() 100 101 if err := g.Generate(context.Background(), dir, []string{"generate", "migration", "invoices", "--type"}); err != nil { 102 t.Errorf("should not be error, but got %v", err) 103 } 104 105 // Validating Files existence 106 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*invoices.*.fizz")) 107 if err != nil { 108 t.Errorf("searching for files should not error, but got %v", err) 109 } 110 111 if len(match) == 0 { 112 t.Error("migration files does not exists on the path") 113 } 114 115 if !strings.HasSuffix(match[0], "_invoices.down.fizz") { 116 t.Error("'invoices.up.fizz' file does not exists on the path") 117 } 118 119 if !strings.HasSuffix(match[1], "_invoices.up.fizz") { 120 t.Error("'invoices.down.fizz' file does not exists on the path") 121 } 122 }) 123 124 t.Run("generate fizz migration with args", func(t *testing.T) { 125 dir := t.TempDir() 126 127 if err := g.Generate(context.Background(), dir, []string{"generate", "migration", "users", "description:string", "quantity:int"}); err != nil { 128 t.Errorf("should not be error, but got %v", err) 129 } 130 131 // Validating Files existence 132 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*users.*.fizz")) 133 if err != nil { 134 t.Errorf("searching for files should not error, but got %v", err) 135 } 136 137 if len(match) == 0 { 138 t.Error("migration files does not exists on the path") 139 } 140 141 if !strings.HasSuffix(match[0], "_users.down.fizz") { 142 t.Error("'users.up.fizz' file does not exists on the path") 143 } 144 145 if !strings.HasSuffix(match[1], "_users.up.fizz") { 146 t.Error("'users.down.fizz' file does not exists on the path") 147 } 148 149 // Validating existence of the attributes 150 dropFile, err := ioutil.ReadFile(match[0]) 151 if err != nil { 152 t.Error("reading migration down file error") 153 } 154 155 if string(dropFile) != `drop_table("users")` { 156 t.Error(`unexpected content, file should contain 'drop_table("users")'`) 157 } 158 159 createFile, err := ioutil.ReadFile(match[1]) 160 if err != nil { 161 t.Error("reading migration down file error") 162 } 163 164 upData := string(createFile) 165 shouldContain := []string{`create_table("users")`, "t.Column", "description", "quantity", "string", "integer"} 166 for _, contain := range shouldContain { 167 if !strings.Contains(upData, contain) { 168 t.Errorf("unexpected content, file should contain '%s'", contain) 169 } 170 } 171 }) 172 }