diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-01 20:23:56 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-01 20:23:56 -0500 |
| commit | bc346a69c43961e1478ed3acfd80c24d566394bf (patch) | |
| tree | ac40e3cbd3e915e41d6ba48b23402b376c897c83 /internal | |
| parent | Use 14 character long version instead 32 (diff) | |
| download | cli-bc346a69c43961e1478ed3acfd80c24d566394bf.zip | |
Add migration tests
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/commands/create.go | 9 | ||||
| -rw-r--r-- | internal/models/migration.go | 24 | ||||
| -rw-r--r-- | internal/models/migration_test.go | 37 | ||||
| -rw-r--r-- | internal/util/clock.go | 13 |
4 files changed, 69 insertions, 14 deletions
diff --git a/internal/commands/create.go b/internal/commands/create.go index b9d63df..5176648 100644 --- a/internal/commands/create.go +++ b/internal/commands/create.go @@ -10,6 +10,7 @@ import ( "github.com/eugenetriguba/bolt/internal/db" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" + "github.com/eugenetriguba/bolt/internal/util" "github.com/google/subcommands" ) @@ -27,16 +28,16 @@ func (*CreateCmd) Usage() string { ` } -func (c *CreateCmd) SetFlags(f *flag.FlagSet) { +func (cmd *CreateCmd) SetFlags(f *flag.FlagSet) { f.StringVar( - &c.message, + &cmd.message, "message", "autogenerated", "Message to use for the migration", ) } -func (m *CreateCmd) Execute( +func (cmd *CreateCmd) Execute( _ context.Context, f *flag.FlagSet, _ ...interface{}, @@ -59,7 +60,7 @@ func (m *CreateCmd) Execute( return subcommands.ExitFailure } - migration := models.NewMigration(m.message) + migration := models.NewMigration(&util.RealClock{}, cmd.message) err = migrationRepo.Create(migration) if err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/internal/models/migration.go b/internal/models/migration.go index a19b95f..c02f17a 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -3,7 +3,8 @@ package models import ( "fmt" "strings" - "time" + + "github.com/eugenetriguba/bolt/internal/util" ) type Migration struct { @@ -12,8 +13,8 @@ type Migration struct { Applied bool } -func NewMigration(message string) *Migration { - now := time.Now() +func NewMigration(clock util.Clock, message string) *Migration { + now := clock.Now() version := fmt.Sprintf( "%d%02d%02d%02d%02d%02d", now.Year(), now.Month(), now.Day(), @@ -27,11 +28,9 @@ func (m *Migration) Dirname() string { } func (m *Migration) normalizedMessage() string { - return strings.ReplaceAll( - strings.TrimSpace(strings.ToLower(m.Message)), - " ", - "_", - ) + lowercaseMessage := strings.ToLower(m.Message) + trimmedMessage := strings.TrimSpace(lowercaseMessage) + return strings.ReplaceAll(trimmedMessage, " ", "_") } func (m *Migration) String() string { @@ -40,10 +39,15 @@ func (m *Migration) String() string { checked = "x" } + message := m.normalizedMessage() + if len(message) > 0 { + message = fmt.Sprintf("- %s ", message) + } + return fmt.Sprintf( - "%s - %s - [%s]", + "%s %s- [%s]", m.Version, - m.normalizedMessage(), + message, checked, ) } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go new file mode 100644 index 0000000..5df6dc8 --- /dev/null +++ b/internal/models/migration_test.go @@ -0,0 +1,37 @@ +package models + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +type FakeClock struct{} + +func (c *FakeClock) Now() time.Time { + return time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) +} + +func TestNewMigration(t *testing.T) { + m := NewMigration(&FakeClock{}, " test MESSAGE ") + + assert.Equal(t, "20200101000000", m.Version) + assert.Equal(t, " test MESSAGE ", m.Message) + assert.Equal(t, false, m.Applied) + assert.Equal(t, "20200101000000_test_message", m.Dirname()) + assert.Equal(t, "20200101000000 - test_message - [ ]", m.String()) +} + +func TestAppliedNewMigration(t *testing.T) { + m := NewMigration(&FakeClock{}, "test message") + m.Applied = true + + assert.Equal(t, "20200101000000 - test_message - [x]", m.String()) +} + +func TestEmptyMessageMigration(t *testing.T) { + m := NewMigration(&FakeClock{}, "") + + assert.Equal(t, "20200101000000 - [ ]", m.String()) +} diff --git a/internal/util/clock.go b/internal/util/clock.go new file mode 100644 index 0000000..d9a1afa --- /dev/null +++ b/internal/util/clock.go @@ -0,0 +1,13 @@ +package util + +import "time" + +type Clock interface { + Now() time.Time +} + +type RealClock struct{} + +func (RealClock) Now() time.Time { + return time.Now() +} |