-
Notifications
You must be signed in to change notification settings - Fork 5
/
options_test.go
107 lines (97 loc) · 2.84 KB
/
options_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package schema
import (
"context"
"fmt"
"log"
"os"
"strings"
"testing"
)
func TestWithTableNameOptionWithSchema(t *testing.T) {
schema := "special"
table := "my_migrations"
m := NewMigrator(WithTableName(schema, table))
if m.SchemaName != schema {
t.Errorf("Expected SchemaName to be '%s'. Got '%s' instead.", schema, m.SchemaName)
}
if m.TableName != table {
t.Errorf("Expected TableName to be '%s'. Got '%s' instead.", table, m.TableName)
}
}
func TestWithTableNameOptionWithoutSchema(t *testing.T) {
name := "terrible_migrations_table_name"
m := NewMigrator(WithTableName(name))
if m.SchemaName != "" {
t.Errorf("Expected SchemaName to be blank. Got '%s' instead.", m.SchemaName)
}
if m.TableName != name {
t.Errorf("Expected TableName to be '%s'. Got '%s' instead.", name, m.TableName)
}
}
func TestDefaultTableName(t *testing.T) {
name := "schema_migrations"
m := NewMigrator()
if m.SchemaName != "" {
t.Errorf("Expected SchemaName to be blank by default. Got '%s' instead.", m.SchemaName)
}
if m.TableName != name {
t.Errorf("Expected TableName to be '%s' by default. Got '%s' instead.", name, m.TableName)
}
}
func TestDefaultDialect(t *testing.T) {
m := NewMigrator()
if m.Dialect != Postgres {
t.Errorf("Expected Migrator to have Postgres Dialect by default. Got: %v", m.Dialect)
}
}
func TestWithDialectOption(t *testing.T) {
m := Migrator{Dialect: nil}
if m.Dialect != nil {
t.Errorf("Expected nil Dialect. Got '%v'", m.Dialect)
}
modifiedMigrator := WithDialect(Postgres)(m)
if modifiedMigrator.Dialect != Postgres {
t.Errorf("Expected modifiedMigrator to have Postgres dialect. Got '%v'.", modifiedMigrator.Dialect)
}
if m.Dialect != nil {
t.Errorf("Expected Option to not modify the original Migrator's Dialect, but it changed it to '%v'.", m.Dialect)
}
}
func TestWithContextOption(t *testing.T) {
m := NewMigrator()
if m.ctx == nil {
t.Errorf("Expected NewMigrator to set a default .ctx")
}
ctx := context.WithValue(context.Background(), "key", "value")
m = NewMigrator(WithContext(ctx))
if m.ctx != ctx {
t.Errorf("Expected WithContext to set a custom context")
}
}
func TestWithLoggerOption(t *testing.T) {
m := Migrator{}
if m.Logger != nil {
t.Errorf("Expected nil Logger by default. Got '%v'", m.Logger)
}
modifiedMigrator := WithLogger(log.New(os.Stdout, "schema: ", log.Ldate|log.Ltime))(m)
if modifiedMigrator.Logger == nil {
t.Errorf("Expected logger to have been added")
}
}
type StrLog string
func (nl *StrLog) Print(msgs ...interface{}) {
var sb strings.Builder
for _, msg := range msgs {
sb.WriteString(fmt.Sprintf("%s", msg))
}
result := StrLog(sb.String())
*nl = result
}
func TestSimpleLogger(t *testing.T) {
var str StrLog
m := NewMigrator(WithLogger(&str))
m.log("Test message")
if str != "Test message" {
t.Errorf("Expected logger to print 'Test message'. Got '%s'", str)
}
}