diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-06 11:53:18 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-06 11:53:18 -0500 |
| commit | 64a8adf7039ea91d0674c80e05f4942116c5339f (patch) | |
| tree | bd967c2c631439132b84e80759e3eef80949cd5d /internal | |
| parent | Tweak a few comments (diff) | |
| download | cli-64a8adf7039ea91d0674c80e05f4942116c5339f.zip | |
Tweak model tests
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/commands/create.go | 4 | ||||
| -rw-r--r-- | internal/models/migration.go | 11 | ||||
| -rw-r--r-- | internal/models/migration_test.go | 77 | ||||
| -rw-r--r-- | internal/util/clock.go | 13 |
4 files changed, 62 insertions, 43 deletions
diff --git a/internal/commands/create.go b/internal/commands/create.go index de71fd7..9372157 100644 --- a/internal/commands/create.go +++ b/internal/commands/create.go @@ -5,12 +5,12 @@ import ( "flag" "fmt" "os" + "time" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/repositories" "github.com/eugenetriguba/bolt/internal/storage" - "github.com/eugenetriguba/bolt/internal/util" "github.com/google/subcommands" ) @@ -64,7 +64,7 @@ func (cmd *CreateCmd) Execute( return subcommands.ExitFailure } - migration := models.NewMigration(&util.RealClock{}, cmd.message) + migration := models.NewMigration(time.Now(), 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 c02f17a..a4b694c 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -3,8 +3,7 @@ package models import ( "fmt" "strings" - - "github.com/eugenetriguba/bolt/internal/util" + "time" ) type Migration struct { @@ -13,12 +12,10 @@ type Migration struct { Applied bool } -func NewMigration(clock util.Clock, message string) *Migration { - now := clock.Now() +func NewMigration(ts time.Time, message string) *Migration { version := fmt.Sprintf( - "%d%02d%02d%02d%02d%02d", - now.Year(), now.Month(), now.Day(), - now.Hour(), now.Minute(), now.Second(), + "%d%02d%02d%02d%02d%02d", ts.Year(), ts.Month(), ts.Day(), + ts.Hour(), ts.Minute(), ts.Second(), ) return &Migration{Version: version, Message: message, Applied: false} } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index 5df6dc8..623f4ad 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -1,37 +1,72 @@ -package models +package models_test import ( "testing" "time" - "github.com/stretchr/testify/assert" + "github.com/eugenetriguba/bolt/internal/models" ) -type FakeClock struct{} +var ts = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) -func (c *FakeClock) Now() time.Time { - return time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) -} +func TestMigration(t *testing.T) { + type test struct { + inputTs time.Time + inputMessage string + expectedVersion string + expectedDirname string + expectedString string + } + tests := []test{ + // Ensure message is trimmed of spaces, lowercased, and + // using underscores for spaces in normalized version. + { + inputTs: ts, + inputMessage: " test MESSAGE ", + expectedVersion: "20200101000000", + expectedDirname: "20200101000000_test_message", + expectedString: "20200101000000 - test_message - [ ]", + }, + // Ensure an empty message means there is no message shown for + // dirname and string. + { + inputTs: ts, + inputMessage: "", + expectedVersion: "20200101000000", + expectedDirname: "20200101000000_", + expectedString: "20200101000000 - [ ]", + }, + { -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()) + for _, tc := range tests { + m := models.NewMigration(tc.inputTs, tc.inputMessage) + if m.Version != tc.expectedVersion { + t.Fatalf("got: %v, expected: %v", m.Version, tc.expectedVersion) + } + if m.Message != tc.inputMessage { + t.Fatalf("got: %v, expected: %v", m.Message, tc.inputMessage) + } + if m.Applied != false { + t.Fatalf("got: %v, expected: %v", m.Applied, false) + } + if m.Dirname() != tc.expectedDirname { + t.Fatalf("got: %v, expected: %v", m.Dirname(), tc.expectedDirname) + } + if m.String() != tc.expectedString { + t.Fatalf("got: %v, expected: %v", m.String(), tc.expectedString) + } + } } func TestAppliedNewMigration(t *testing.T) { - m := NewMigration(&FakeClock{}, "test message") + m := models.NewMigration(ts, "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()) + expectedString := "20200101000000 - test_message - [x]" + if m.String() != expectedString { + t.Fatalf("got: %v, expected: %v", m.String(), expectedString) + } } diff --git a/internal/util/clock.go b/internal/util/clock.go deleted file mode 100644 index d9a1afa..0000000 --- a/internal/util/clock.go +++ /dev/null @@ -1,13 +0,0 @@ -package util - -import "time" - -type Clock interface { - Now() time.Time -} - -type RealClock struct{} - -func (RealClock) Now() time.Time { - return time.Now() -} |