github.com/supabase/cli@v1.168.1/test/migration_test.go (about)

     1  package integration
     2  
     3  // Basic imports
     4  import (
     5  	"os"
     6  	"testing"
     7  
     8  	gonanoid "github.com/matoous/go-nanoid/v2"
     9  	"github.com/spf13/cobra"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/stretchr/testify/suite"
    12  	clicmd "github.com/supabase/cli/cmd"
    13  	"github.com/supabase/cli/test/mocks/supabase"
    14  )
    15  
    16  type MigrationTestSuite struct {
    17  	suite.Suite
    18  	tempDir string
    19  	cmd     *cobra.Command
    20  }
    21  
    22  // test functions
    23  func (suite *MigrationTestSuite) TestNewMigration() {
    24  	// run command
    25  	migration, _, err := suite.cmd.Find([]string{"migration", "new"})
    26  	require.NoError(suite.T(), err)
    27  	name := gonanoid.MustGenerate(supabase.IDAlphabet, 10)
    28  	require.NoError(suite.T(), migration.RunE(migration, []string{name}))
    29  
    30  	// check migrations file created
    31  	subs, err := os.ReadDir("supabase/migrations")
    32  	require.NoError(suite.T(), err)
    33  	require.Regexp(suite.T(), `[0-9]{14}_`+name+".sql", subs[0].Name())
    34  }
    35  
    36  // hooks
    37  func (suite *MigrationTestSuite) SetupTest() {
    38  	// init cli
    39  	suite.cmd = clicmd.GetRootCmd()
    40  	suite.tempDir = NewTempDir(Logger, TempDir)
    41  
    42  	// init supabase
    43  	init, _, err := suite.cmd.Find([]string{"init"})
    44  	require.NoError(suite.T(), err)
    45  	require.NoError(suite.T(), init.RunE(init, []string{}))
    46  }
    47  
    48  func (suite *MigrationTestSuite) TeardownTest() {
    49  	require.NoError(suite.T(), os.Chdir(TempDir))
    50  }
    51  
    52  // In order for 'go test' to run this suite, we need to create
    53  // a normal test function and pass our suite to suite.Run
    54  func TestMigrationTestSuite(t *testing.T) {
    55  	suite.Run(t, new(MigrationTestSuite))
    56  }