github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/tools/soda/fizz/creator_test.go (about)

     1  package fizz
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func Test_Fizz_Create(t *testing.T) {
    10  	f := Creator{}
    11  
    12  	t.Run("generate migration files", func(t *testing.T) {
    13  		dir := t.TempDir()
    14  		name := "users"
    15  		args := []string{}
    16  
    17  		if err := f.Create(dir, name, args); err != nil {
    18  			t.Errorf("creating migration files should not be error, but got %v", err)
    19  		}
    20  
    21  		// Validating files existence
    22  		match, err := filepath.Glob(filepath.Join(dir, "*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 migration singularized name", func(t *testing.T) {
    41  		dir := t.TempDir()
    42  		name := "company"
    43  		args := []string{}
    44  
    45  		if err := f.Create(dir, name, args); err != nil {
    46  			t.Errorf("creating migration files should not be error, but got %v", err)
    47  		}
    48  
    49  		// Validating files existence
    50  		match, err := filepath.Glob(filepath.Join(dir, "*company.*.fizz"))
    51  		if err != nil {
    52  			t.Errorf("searching for files should not error, but got %v", err)
    53  		}
    54  
    55  		if len(match) == 0 {
    56  			t.Error("migration files does not exists on the path")
    57  		}
    58  
    59  		if !strings.HasSuffix(match[0], "_company.down.fizz") {
    60  			t.Error("'companies.up.fizz' file does not exists on the path")
    61  		}
    62  
    63  		if !strings.HasSuffix(match[1], "_company.up.fizz") {
    64  			t.Error("'company.down.fizz' file does not exists on the path")
    65  		}
    66  	})
    67  }