github.com/nshntarora/pop@v0.1.2/migration_box_test.go (about)

     1  package pop
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/nshntarora/pop/logging"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  type logEntry struct {
    12  	lvl  logging.Level
    13  	s    string
    14  	args []interface{}
    15  }
    16  
    17  func setNewTestLogger() *[]logEntry {
    18  	var logs []logEntry
    19  	log = func(lvl logging.Level, s string, args ...interface{}) {
    20  		logs = append(logs, logEntry{lvl, s, args})
    21  	}
    22  	return &logs
    23  }
    24  
    25  func Test_MigrationBox(t *testing.T) {
    26  	if PDB == nil {
    27  		t.Skip("skipping integration tests")
    28  	}
    29  
    30  	t.Run("finds testdata", func(t *testing.T) {
    31  		r := require.New(t)
    32  
    33  		b, err := NewMigrationBox(os.DirFS("testdata/migrations/multiple"), PDB)
    34  		r.NoError(err)
    35  		r.Equal(4, len(b.UpMigrations.Migrations))
    36  		r.Equal("mysql", b.UpMigrations.Migrations[0].DBType)
    37  		r.Equal("postgres", b.UpMigrations.Migrations[1].DBType)
    38  		r.Equal("sqlite3", b.UpMigrations.Migrations[2].DBType)
    39  		r.Equal("all", b.UpMigrations.Migrations[3].DBType)
    40  	})
    41  
    42  	t.Run("ignores clutter files", func(t *testing.T) {
    43  		logs := setNewTestLogger()
    44  		r := require.New(t)
    45  
    46  		b, err := NewMigrationBox(os.DirFS("testdata/migrations/cluttered"), PDB)
    47  		r.NoError(err)
    48  		r.Equal(1, len(b.UpMigrations.Migrations))
    49  		r.Equal(1, len(*logs))
    50  		r.Equal(logging.Warn, (*logs)[0].lvl)
    51  		r.Contains((*logs)[0].s, "ignoring file")
    52  		r.Equal([]interface{}{"clutter.txt"}, (*logs)[0].args)
    53  	})
    54  
    55  	t.Run("ignores unsupported files", func(t *testing.T) {
    56  		logs := setNewTestLogger()
    57  		r := require.New(t)
    58  
    59  		b, err := NewMigrationBox(os.DirFS("testdata/migrations/unsupported_dialect"), PDB)
    60  		r.NoError(err)
    61  		r.Equal(0, len(b.UpMigrations.Migrations))
    62  		r.Equal(1, len(*logs))
    63  		r.Equal(logging.Warn, (*logs)[0].lvl)
    64  		r.Contains((*logs)[0].s, "ignoring migration")
    65  		r.Equal([]interface{}{"unsupported dialect unsupported"}, (*logs)[0].args)
    66  	})
    67  }