github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/model/generator_test.go (about) 1 package model 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 ) 10 11 func Test_Generate(t *testing.T) { 12 g := Generator{} 13 14 t.Run("generate model", func(t *testing.T) { 15 dir := t.TempDir() 16 modelsPath := filepath.Join(dir, "app", "models") 17 if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil { 18 t.Errorf("creating templates folder should not be error, but got %v", err) 19 } 20 21 if err := g.Generate(context.Background(), dir, []string{"generate", "model", "users"}); err != nil { 22 t.Errorf("should not be error, but got %v", err) 23 } 24 25 // Validating Files existence 26 if !g.exists(filepath.Join(modelsPath, "user.go")) { 27 t.Error("'user.go' file does not exists on the path") 28 } 29 30 if !g.exists(filepath.Join(modelsPath, "user_test.go")) { 31 t.Error("'user_test.go' file does not exists on the path") 32 } 33 }) 34 35 t.Run("generate model validating model name with underscore", func(t *testing.T) { 36 dir := t.TempDir() 37 modelsPath := filepath.Join(dir, "app", "models") 38 if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil { 39 t.Errorf("creating templates folder should not be error, but got %v", err) 40 } 41 42 if err := g.Generate(context.Background(), dir, []string{"generate", "model", "organizational_model"}); err != nil { 43 t.Errorf("should not be error, but got %v", err) 44 } 45 46 userFilePath := filepath.Join(modelsPath, "organizational_model.go") 47 data, err := os.ReadFile(userFilePath) 48 if err != nil { 49 t.Error("reading file error") 50 } 51 52 stringData := string(data) 53 shouldContain := []string{"OrganizationalModel", "OrganizationalModels", "[]OrganizationalModel"} 54 for _, contain := range shouldContain { 55 if !strings.Contains(stringData, contain) { 56 t.Errorf("unexpected content, file should contain '%s'", contain) 57 } 58 } 59 }) 60 61 t.Run("generate model with args", func(t *testing.T) { 62 dir := t.TempDir() 63 modelsPath := filepath.Join(dir, "app", "models") 64 if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil { 65 t.Errorf("creating templates folder should not be error, but got %v", err) 66 } 67 68 if err := g.Generate(context.Background(), dir, []string{"generate", "model", "users", "description:string", "quantity:int"}); err != nil { 69 t.Errorf("should not be error, but got %v", err) 70 } 71 72 // Validating Files existence 73 userFilePath := filepath.Join(modelsPath, "user.go") 74 if !g.exists(userFilePath) { 75 t.Error("'user.go' file does not exists on the path") 76 } 77 78 if !g.exists(filepath.Join(modelsPath, "user_test.go")) { 79 t.Error("'user_test.go' file does not exists on the path") 80 } 81 82 // Validating existence of the attributes 83 data, err := os.ReadFile(userFilePath) 84 if err != nil { 85 t.Error("reading file error") 86 } 87 88 stringData := string(data) 89 shouldContain := []string{"ID", "CreatedAt", "UpdatedAt", "Description", "Quantity", "uuid.UUID", "time.Time", "string", "int"} 90 for _, contain := range shouldContain { 91 if !strings.Contains(stringData, contain) { 92 t.Errorf("unexpected content, file should contain '%s'", contain) 93 } 94 } 95 }) 96 }