github.com/aleksanderaleksic/tgmigrate@v0.1.7/migration/migration_file_test.go (about)

     1  package migration
     2  
     3  import (
     4  	"github.com/aleksanderaleksic/tgmigrate/test"
     5  	"github.com/stretchr/testify/assert"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestGetMigrationFilesFromEmptyDirectory(t *testing.T) {
    11  	ass := assert.New(t)
    12  	testDir := t.TempDir()
    13  
    14  	files, err := GetMigrationFiles(testDir)
    15  	ass.Nil(err)
    16  	ass.Len(*files, 0)
    17  }
    18  
    19  func TestHandleInvalidMigrationFilesError(t *testing.T) {
    20  	ass := assert.New(t)
    21  	testDir := t.TempDir()
    22  	test.CopyTestData(t, "empty_migration", testDir)
    23  
    24  	files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
    25  	ass.NotNil(err)
    26  	ass.Nil(files)
    27  }
    28  
    29  func TestHandleInvalidMoveBlock(t *testing.T) {
    30  	ass := assert.New(t)
    31  	testDir := t.TempDir()
    32  	test.CopyTestData(t, "invalid_move_block", testDir)
    33  
    34  	files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
    35  	ass.NotNil(err)
    36  	ass.Nil(files)
    37  }
    38  
    39  func TestHandleInvalidRemoveBlock(t *testing.T) {
    40  	ass := assert.New(t)
    41  	testDir := t.TempDir()
    42  	test.CopyTestData(t, "invalid_remove_block", testDir)
    43  
    44  	files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
    45  	ass.NotNil(err)
    46  	ass.Nil(files)
    47  }
    48  
    49  func TestHandleMissingVersionPrefix(t *testing.T) {
    50  	ass := assert.New(t)
    51  	testDir := t.TempDir()
    52  	test.CopyTestData(t, "missing_version", testDir)
    53  
    54  	files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
    55  	ass.NotNil(err)
    56  	ass.Nil(files)
    57  }
    58  
    59  func TestHandleDuplicateVersionPrefix(t *testing.T) {
    60  	ass := assert.New(t)
    61  	testDir := t.TempDir()
    62  	test.CopyTestData(t, "duplicate_version", testDir)
    63  
    64  	files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
    65  	ass.NotNil(err)
    66  	ass.Nil(files)
    67  }
    68  
    69  func TestHandleFilePermissionDenied(t *testing.T) {
    70  	ass := assert.New(t)
    71  	testDir := t.TempDir()
    72  	test.CopyTestData(t, "duplicate_version", testDir)
    73  
    74  	files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
    75  	ass.NotNil(err)
    76  	ass.Nil(files)
    77  }
    78  
    79  func TestValidMigration(t *testing.T) {
    80  	ass := assert.New(t)
    81  	testDir := t.TempDir()
    82  	test.CopyTestData(t, "simple", testDir)
    83  
    84  	f, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
    85  	ass.Nil(err)
    86  
    87  	files := *f
    88  	ass.Len(files, 2)
    89  	ass.Equal(files[0], File{
    90  		Metadata: FileMetadata{
    91  			FileName: "V1__move.hcl",
    92  			FileHash: "5f18eb04c47a8f7ead44b7e727e1d21f9c07954c3143180383a6cc2280e2fc0d",
    93  			Version:  1,
    94  		},
    95  		Config: Config{
    96  			Environments: []string{"test"},
    97  			Description:  "    - Move rest_api lambda to rest_v2\n",
    98  		},
    99  		Migrations: []Migration{
   100  			{
   101  				Type: "move",
   102  				Name: "rest_api",
   103  				Move: &MoveBlock{
   104  					From: MoveFromBlock{
   105  						State:    "us-east-1/apis/rest",
   106  						Resource: "aws_lambda_function.rest_api",
   107  					},
   108  					To: MoveFromBlock{
   109  						State:    "us-east-1/apis/rest_v2",
   110  						Resource: "aws_lambda_function.rest_api",
   111  					},
   112  				},
   113  				Remove: nil,
   114  			},
   115  		},
   116  	})
   117  
   118  	ass.Equal(files[1], File{
   119  		Metadata: FileMetadata{
   120  			FileName: "V2__remove.hcl",
   121  			FileHash: "92cb5ae4c96519f4213a6ee2d0ada9c360866ea81cd7977f2caabbebd736b598",
   122  			Version:  2,
   123  		},
   124  		Config: Config{
   125  			Environments: []string{"test"},
   126  			Description:  "    - Remove testfile from files module\n",
   127  		},
   128  		Migrations: []Migration{
   129  			{
   130  				Type: "remove",
   131  				Name: "file",
   132  				Move: nil,
   133  				Remove: &RemoveBlock{
   134  					State:    "us-east-1/files",
   135  					Resource: "file.test_file",
   136  				},
   137  			},
   138  		},
   139  	})
   140  }