github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/resource/resource_test.go (about) 1 package resource 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 ) 9 10 func Test_GenerateActions(t *testing.T) { 11 dir := t.TempDir() 12 args := []string{"companies"} 13 14 if err := os.MkdirAll(filepath.Join(dir, "app", "actions"), os.ModePerm); err != nil { 15 t.Errorf("creating actions directory should not error, but got %v", err) 16 } 17 18 resource := New(dir, args) 19 if err := resource.GenerateActions(); err != nil { 20 t.Errorf("should not be error, but got %v", err) 21 } 22 23 // Checking Action Files 24 if _, err := os.Stat(filepath.Join(dir, "app", "actions", "companies.go")); os.IsNotExist(err) { 25 t.Error("'companies.go' file does not exists on the path") 26 } 27 28 if _, err := os.Stat(filepath.Join(dir, "app", "actions", "companies_test.go")); os.IsNotExist(err) { 29 t.Error("'companies_test.go' file does not exists on the path") 30 } 31 32 // Validating existence of the attributes 33 companyDir := filepath.Join(dir, "app", "actions", "companies.go") 34 data, err := os.ReadFile(companyDir) 35 if err != nil { 36 t.Error("reading file error") 37 } 38 39 stringData := string(data) 40 shouldContain := []string{ 41 "func (v CompaniesResource) List(c buffalo.Context) error {", 42 "func (v CompaniesResource) New(c buffalo.Context) error {", 43 "func (v CompaniesResource) Show(c buffalo.Context) error {", 44 "func (v CompaniesResource) Update(c buffalo.Context) error {", 45 "func (v CompaniesResource) Destroy(c buffalo.Context) error {", 46 "func (v CompaniesResource) Create(c buffalo.Context) error {", 47 } 48 for _, contain := range shouldContain { 49 if !strings.Contains(stringData, contain) { 50 t.Errorf("unexpected content, file should contain '%s'", contain) 51 } 52 } 53 } 54 55 func Test_GenerateMigrations(t *testing.T) { 56 dir := t.TempDir() 57 args := []string{"events"} 58 59 if err := os.MkdirAll(filepath.Join(dir, "migrations"), os.ModePerm); err != nil { 60 t.Errorf("creating actions directory should not error, but got %v", err) 61 } 62 63 resource := New(dir, args) 64 if err := resource.GenerateMigrations(); err != nil { 65 t.Errorf("should not be error, but got %v", err) 66 } 67 68 // Validating Files existence 69 match, err := filepath.Glob(filepath.Join(dir, "migrations", "*events.*.fizz")) 70 if err != nil { 71 t.Errorf("searching for files should not error, but got %v", err) 72 } 73 74 if len(match) == 0 { 75 t.Error("migration files does not exists on the path") 76 } 77 78 if !strings.HasSuffix(match[0], "_events.down.fizz") { 79 t.Error("'events.up.fizz' file does not exists on the path") 80 } 81 82 if !strings.HasSuffix(match[1], "_events.up.fizz") { 83 t.Error("'events.down.fizz' file does not exists on the path") 84 } 85 } 86 87 func Test_GenerateModels(t *testing.T) { 88 dir := t.TempDir() 89 args := []string{"users", "first_name", "last_name", "email"} 90 modelsPath := filepath.Join(dir, "app", "models") 91 if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil { 92 t.Errorf("creating templates folder should not be error, but got %v", err) 93 } 94 95 resource := New(dir, args) 96 if err := resource.GenerateModel(); err != nil { 97 t.Errorf("should not be error, but got %v", err) 98 } 99 100 // Validating Files existence 101 if _, err := os.Stat(filepath.Join(dir, "app", "models", "user.go")); os.IsNotExist(err) { 102 t.Error("'users.go' file does not exists on the path") 103 } 104 105 if _, err := os.Stat(filepath.Join(dir, "app", "models", "user_test.go")); os.IsNotExist(err) { 106 t.Error("'users_test.go' file does not exists on the path") 107 } 108 109 // Validating existence of the attributes 110 userFilePath := filepath.Join(modelsPath, "user.go") 111 data, err := os.ReadFile(userFilePath) 112 if err != nil { 113 t.Error("reading file error") 114 } 115 116 stringData := string(data) 117 shouldContain := []string{"ID", "CreatedAt", "UpdatedAt", "FirstName", "LastName", "Email", "time.Time", "string"} 118 for _, contain := range shouldContain { 119 if !strings.Contains(stringData, contain) { 120 t.Errorf("unexpected content, file should contain '%s'", contain) 121 } 122 } 123 } 124 125 func Test_GenerateTemplates(t *testing.T) { 126 dir := t.TempDir() 127 args := []string{"cars", "model", "brand"} 128 modelsPath := filepath.Join(dir, "app", "templates") 129 if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil { 130 t.Errorf("creating templates folder should not be error, but got %v", err) 131 } 132 133 resource := New(dir, args) 134 if err := resource.GenerateTemplates(); err != nil { 135 t.Errorf("should not be error, but got %v", err) 136 } 137 138 // Validating Files existence 139 templateFolder := filepath.Join(dir, "app", "templates", "cars") 140 templates := []string{"index", "new", "edit", "show", "form"} 141 142 for _, tmpl := range templates { 143 if _, err := os.Stat(filepath.Join(templateFolder, tmpl+".plush.html")); os.IsNotExist(err) { 144 t.Error("'index.plush.html' file does not exists on the path") 145 } 146 } 147 }