-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkey_test.go
76 lines (56 loc) · 1.86 KB
/
key_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
package migrator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeys(t *testing.T) {
t.Run("it returns empty on empty keys", func(t *testing.T) {
k := keys{Key{}}
assert.Equal(t, "", k.render())
})
t.Run("it renders row from one key", func(t *testing.T) {
k := keys{Key{Columns: []string{"test_id"}}}
assert.Equal(t, "KEY (`test_id`)", k.render())
})
t.Run("it renders row from multiple keys", func(t *testing.T) {
k := keys{
Key{Columns: []string{"test_id"}},
Key{Columns: []string{"random_id"}},
}
assert.Equal(
t,
"KEY (`test_id`), KEY (`random_id`)",
k.render(),
)
})
}
func TestKey(t *testing.T) {
t.Run("it returns empty on empty keys", func(t *testing.T) {
k := Key{}
assert.Equal(t, "", k.render())
})
t.Run("it skips type if it is not in valid list", func(t *testing.T) {
k := Key{Type: "random", Columns: []string{"test_id"}}
assert.Equal(t, "KEY (`test_id`)", k.render())
})
t.Run("it renders with type", func(t *testing.T) {
k := Key{Type: "primary", Columns: []string{"test_id"}}
assert.Equal(t, "PRIMARY KEY (`test_id`)", k.render())
})
t.Run("it renders with multiple columns", func(t *testing.T) {
k := Key{Type: "unique", Columns: []string{"test_id", "random_id"}}
assert.Equal(t, "UNIQUE KEY (`test_id`, `random_id`)", k.render())
})
t.Run("it renders with name", func(t *testing.T) {
k := Key{Name: "random_idx", Columns: []string{"test_id"}}
assert.Equal(t, "KEY `random_idx` (`test_id`)", k.render())
})
}
func TestBuildUniqueIndexName(t *testing.T) {
t.Run("It builds name from one column", func(t *testing.T) {
assert.Equal(t, "table_test_unique", BuildUniqueKeyNameOnTable("table", "test"))
})
t.Run("it builds name from multiple columns", func(t *testing.T) {
assert.Equal(t, "table_test_again_unique", BuildUniqueKeyNameOnTable("table", "test", "again"))
})
}