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

     1  package liquibase
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestGeneratorRun(t *testing.T) {
    13  	t.Run("incomplete arguments", func(t *testing.T) {
    14  		g := Generator{}
    15  		err := g.Generate(context.Background(), "", []string{"a", "b"})
    16  		if err != ErrNameArgMissing {
    17  			t.Errorf("err should be %v, got %v", ErrNameArgMissing, err)
    18  		}
    19  	})
    20  
    21  	t.Run("simple", func(t *testing.T) {
    22  		root := t.TempDir()
    23  		err := os.Chdir(root)
    24  		if err != nil {
    25  			t.Error("could not change to temp directory")
    26  		}
    27  
    28  		g := Generator{mockTimestamp: "12345"}
    29  		err = g.Generate(context.Background(), root, []string{"generate", "migration", "aaa"})
    30  		if err != nil {
    31  			t.Errorf("error should be nil, got %v", err)
    32  		}
    33  
    34  		path := filepath.Join(root, "12345-aaa.xml")
    35  		_, err = os.Stat(path)
    36  		if os.IsNotExist(err) {
    37  			t.Error("should have created the file in the root")
    38  		}
    39  
    40  		d, err := ioutil.ReadFile(path)
    41  		if err != nil {
    42  			t.Errorf("error should be nil, got %v", err)
    43  		}
    44  
    45  		if content := string(d); !strings.Contains(content, "12345-aaa") {
    46  			t.Errorf("file content %v should contain %v", content, "12345-aaa")
    47  		}
    48  	})
    49  
    50  	t.Run("folder", func(t *testing.T) {
    51  		root := t.TempDir()
    52  		err := os.Chdir(root)
    53  		if err != nil {
    54  			t.Error("could not change to temp directory")
    55  		}
    56  
    57  		g := Generator{mockTimestamp: "12345"}
    58  		err = g.Generate(context.Background(), root, []string{"generate", "migration", "folder/is/here/aaa"})
    59  		if err != nil {
    60  			t.Errorf("error should be nil, got %v", err)
    61  		}
    62  
    63  		path := filepath.Join(root, "folder", "is", "here", "12345-aaa.xml")
    64  		_, err = os.Stat(path)
    65  		if os.IsNotExist(err) {
    66  			t.Error("should have created the file in the root")
    67  		}
    68  
    69  		d, err := ioutil.ReadFile(path)
    70  		if err != nil {
    71  			t.Errorf("error should be nil, got %v", err)
    72  		}
    73  
    74  		if content := string(d); !strings.Contains(content, "12345-aaa") {
    75  			t.Errorf("file content %v should contain %v", content, "12345-aaa")
    76  		}
    77  	})
    78  
    79  	t.Run("folder exists", func(t *testing.T) {
    80  		root := t.TempDir()
    81  		err := os.Chdir(root)
    82  		if err != nil {
    83  			t.Error("could not change to temp directory")
    84  		}
    85  
    86  		err = os.MkdirAll(filepath.Join("folder", "is", "here"), 0755)
    87  		if err != nil {
    88  			t.Fatal("could not create the folder")
    89  		}
    90  
    91  		g := Generator{mockTimestamp: "12345"}
    92  		err = g.Generate(context.Background(), root, []string{"generate", "migration", "folder/is/here/aaa"})
    93  		if err != nil {
    94  			t.Errorf("error should be nil, got %v", err)
    95  		}
    96  
    97  		path := filepath.Join(root, "folder", "is", "here", "12345-aaa.xml")
    98  		_, err = os.Stat(path)
    99  		if os.IsNotExist(err) {
   100  			t.Error("should have created the file in the root")
   101  		}
   102  
   103  		d, err := ioutil.ReadFile(path)
   104  		if err != nil {
   105  			t.Errorf("error should be nil, got %v", err)
   106  		}
   107  
   108  		if content := string(d); !strings.Contains(content, "12345-aaa") {
   109  			t.Errorf("file content %v should contain %v", content, "12345-aaa")
   110  		}
   111  	})
   112  
   113  	t.Run("different base", func(t *testing.T) {
   114  		root := t.TempDir()
   115  		err := os.Chdir(root)
   116  		if err != nil {
   117  			t.Error("could not change to temp directory")
   118  		}
   119  
   120  		err = os.MkdirAll(filepath.Join("folder", "is", "here"), 0755)
   121  		if err != nil {
   122  			t.Fatal("could not create the folder")
   123  		}
   124  
   125  		g := Generator{
   126  			mockTimestamp: "12345",
   127  			baseFolder:    "migrations",
   128  		}
   129  
   130  		err = g.Generate(context.Background(), root, []string{"generate", "migration", "aaa"})
   131  		if err != nil {
   132  			t.Errorf("error should be nil, got %v", err)
   133  		}
   134  
   135  		path := filepath.Join(root, "migrations", "12345-aaa.xml")
   136  		_, err = os.Stat(path)
   137  		if os.IsNotExist(err) {
   138  			t.Error("should have created the file in the root/migrations folder")
   139  		}
   140  
   141  		d, err := ioutil.ReadFile(path)
   142  		if err != nil {
   143  			t.Errorf("error should be nil, got %v", err)
   144  		}
   145  
   146  		if content := string(d); !strings.Contains(content, "12345-aaa") {
   147  			t.Errorf("file content %v should contain %v", content, "12345-aaa")
   148  		}
   149  	})
   150  }
   151  
   152  func TestGeneratorComposeName(t *testing.T) {
   153  	t.Run("Valid name", func(t *testing.T) {
   154  		g := Generator{}
   155  
   156  		filename, err := g.composeFilename("addDevices", "composename")
   157  		if err != nil {
   158  			t.Errorf("err should be nil, got %v", err)
   159  		}
   160  
   161  		expected := "composename-add_devices.xml"
   162  		if filename != expected {
   163  			t.Errorf("filename should be %v, got %v", expected, filename)
   164  		}
   165  	})
   166  
   167  	t.Run("Invalid name", func(t *testing.T) {
   168  		g := Generator{}
   169  		_, err := g.composeFilename(".", "composename")
   170  		if err != ErrInvalidName {
   171  			t.Errorf("err should be ErrInvalidName, got %v", err)
   172  		}
   173  
   174  		_, err = g.composeFilename("/", "composename")
   175  		if err != ErrInvalidName {
   176  			t.Errorf("err should be ErrInvalidName, got %v", err)
   177  		}
   178  	})
   179  }