github.com/wawandco/oxplugins@v0.7.11/tools/buffalo/model/generator_test.go (about)

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