github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/action/generator_test.go (about)

     1  package action
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"regexp"
     8  	"testing"
     9  )
    10  
    11  func Test_ActionGenerator(t *testing.T) {
    12  	g := Generator{}
    13  
    14  	t.Run("generate action", func(t *testing.T) {
    15  		dir := t.TempDir()
    16  		modelsPath := filepath.Join(dir, "app", "actions")
    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", "action", "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  	t.Run("generate action and checking the content", func(t *testing.T) {
    35  		dir := t.TempDir()
    36  		modelsPath := filepath.Join(dir, "app", "actions")
    37  		if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil {
    38  			t.Errorf("creating templates folder should not be error, but got %v", err)
    39  		}
    40  
    41  		if err := g.Generate(context.Background(), dir, []string{"generate", "action", "users"}); err != nil {
    42  			t.Errorf("should not be error, but got %v", err)
    43  		}
    44  
    45  		// Validating Files existence
    46  		if !g.exists(filepath.Join(modelsPath, "user.go")) {
    47  			t.Error("'user.go' file does not exists on the path")
    48  		}
    49  
    50  		if !g.exists(filepath.Join(modelsPath, "user_test.go")) {
    51  			t.Error("'user_test.go' file does not exists on the path")
    52  		}
    53  
    54  		content, err := os.ReadFile(filepath.Join(modelsPath, "user.go"))
    55  		if err != nil {
    56  			t.Fatal(err)
    57  		}
    58  
    59  		text := string(content)
    60  		matched, err := regexp.MatchString(`func User`, text)
    61  
    62  		if !matched {
    63  			t.Fatalf("File's content is not correct, %v", err)
    64  		}
    65  	})
    66  }