aboutsummaryrefslogtreecommitdiffstats
path: root/internal/models
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-06 11:53:18 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-06 11:53:18 -0500
commit64a8adf7039ea91d0674c80e05f4942116c5339f (patch)
treebd967c2c631439132b84e80759e3eef80949cd5d /internal/models
parentTweak a few comments (diff)
downloadcli-64a8adf7039ea91d0674c80e05f4942116c5339f.zip
Tweak model tests
Diffstat (limited to 'internal/models')
-rw-r--r--internal/models/migration.go11
-rw-r--r--internal/models/migration_test.go77
2 files changed, 60 insertions, 28 deletions
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)
+ }
}