-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
120 lines (105 loc) · 2.58 KB
/
client_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
108
109
110
111
112
113
114
115
116
117
118
119
120
package spoon_test
import (
"fmt"
"testing"
"time"
"cloud.google.com/go/spanner"
"github.com/google/go-cmp/cmp"
"github.com/pi9min/spoon"
)
var (
_ spoon.EntityBehavior = Test1{}
_ spoon.EntityBehavior = (*Test2)(nil)
)
type Test1 struct {
ID uint64
Name string
CreatedAt time.Time
UpdatedAt time.Time
}
func (t1 Test1) TableName() string {
return "Test1"
}
func (t1 Test1) Indexes() spoon.Indexes {
return spoon.Indexes{
spoon.AddIndex("Test1ByCreatedAtDesc", "Test1", false, spoon.KeyPart{ColumnName: "CreatedAt", IsOrderDesc: true}),
}
}
func (t1 Test1) PrimaryKey() *spoon.PrimaryKey {
return spoon.AddPrimaryKey(spoon.KeyPart{ColumnName: "ID"})
}
type Test2 struct {
ID uint64
Test1ID uint64
Comment spanner.NullString `db:"size=1024"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (t2 *Test2) TableName() string {
return "Test2"
}
func (t2 *Test2) Indexes() spoon.Indexes {
return spoon.Indexes{}
}
func (t2 *Test2) PrimaryKey() *spoon.PrimaryKey {
return spoon.AddPrimaryKeyWithInterleave("Test1", spoon.KeyPart{ColumnName: "ID"}, spoon.KeyPart{ColumnName: "CreatedAt", IsOrderDesc: true})
}
func TestGenerateCreateTable(t *testing.T) {
tests := []struct {
name string
entity spoon.EntityBehavior
expect string
}{
{
name: "1 all not null",
entity: &Test1{},
expect: fmt.Sprintf(`CREATE TABLE %s (
%s INT64 NOT NULL,
%s STRING(MAX) NOT NULL,
%s TIMESTAMP NOT NULL,
%s TIMESTAMP NOT NULL,
) PRIMARY KEY (%s)`,
spoon.Quote("Test1"),
spoon.Quote("ID"),
spoon.Quote("Name"),
spoon.Quote("CreatedAt"),
spoon.Quote("UpdatedAt"),
spoon.Quote("ID"),
),
},
{
name: "2 use nullable, size, with interleave",
entity: &Test2{},
expect: fmt.Sprintf(`CREATE TABLE %s (
%s INT64 NOT NULL,
%s INT64 NOT NULL,
%s STRING(1024),
%s TIMESTAMP NOT NULL,
%s TIMESTAMP NOT NULL,
) PRIMARY KEY (%s, %s), INTERLEAVE IN PARENT %s`,
spoon.Quote("Test2"),
spoon.Quote("ID"),
spoon.Quote("Test1ID"),
spoon.Quote("Comment"),
spoon.Quote("CreatedAt"),
spoon.Quote("UpdatedAt"),
spoon.Quote("ID"), spoon.Quote("CreatedAt")+" DESC", spoon.Quote("Test1"),
),
},
}
cli, err := spoon.New()
if err != nil {
t.Fatalf("error new Client")
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := cli.GenerateCreateTable(tt.entity)
if err != nil {
t.Fatalf("error generate create table schema %#v", err)
}
if diff := cmp.Diff(tt.expect, actual); diff != "" {
t.Errorf("GenerateCreateTable Diff:\n%s", diff)
}
})
}
}