-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathschema_test.go
180 lines (142 loc) · 3.66 KB
/
schema_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package y
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Schema", func() {
var p *Proxy
Context("when a struct has one field without tags", func() {
type something struct {
ID int64
}
BeforeEach(func() {
p = New(something{})
})
It("should contain one parsed field", func() {
Expect(p.schema.fields).To(HaveLen(1))
})
It("should contain not nil field schema", func() {
Expect(p.schema.fields["id"]).NotTo(BeNil())
})
})
Context("when a struct has disabled field", func() {
type something struct {
ID int64 `y:"-"`
}
BeforeEach(func() {
p = New(something{})
})
It("should be empty", func() {
Expect(p.schema.fields).To(BeEmpty())
})
})
Context("when a struct has a primary key without name", func() {
type something struct {
ID int64 `y:",pk"`
}
BeforeEach(func() {
p = New(something{})
})
It("should contain a primary key with name", func() {
Expect(p.schema.xinfo.pk).To(Equal([]string{"id"}))
})
})
Context("when a struct has a composite primary key", func() {
type something struct {
X int64 `y:"x,pk"`
Y int64 `y:"y,pk"`
}
BeforeEach(func() {
p = New(something{})
})
It("should contain correct PK sequence", func() {
Expect(p.schema.xinfo.pk).To(Equal([]string{"x", "y"}))
})
It("should contain the first key in the index", func() {
Expect(p.schema.xinfo.idx["x"]).To(Equal(1))
})
It("shouldn't contain the second key in the index", func() {
Expect(p.schema.xinfo.idx["y"]).To(Equal(0))
})
})
Context("when a struct has an auto-incremented key", func() {
type something struct {
ID int64 `y:",autoincr"`
}
BeforeEach(func() {
p = New(something{})
})
It("should contains enabled autoincr in field opts", func() {
Expect(p.schema.fields["id"].autoincr).To(BeTrue())
})
})
Context("when a slice of struct parsed", func() {
type something struct {
ID int64
}
BeforeEach(func() {
p = New([]something{})
})
It("should contain one parsed field", func() {
Expect(p.schema.fields).To(HaveLen(1))
})
It("should contain not nil field schema", func() {
Expect(p.schema.fields["id"]).NotTo(BeNil())
})
})
Context("when a struct contain embedded struct", func() {
type foo struct {
x int64
y int64
}
type bar struct {
foo
z int64
}
BeforeEach(func() {
p = New(bar{})
})
It("should contain three parsed field", func() {
Expect(p.schema.fields).To(HaveLen(3))
})
It("should contain parsed fields opts", func() {
Expect(p.schema.fields["x"]).NotTo(BeNil())
Expect(p.schema.fields["y"]).NotTo(BeNil())
Expect(p.schema.fields["z"]).NotTo(BeNil())
})
})
Context("when a struct has a foreign key", func() {
type something struct {
AnyID int64 `y:",fk"`
}
BeforeEach(func() {
p = New(something{})
})
It("should contain index opts for 'any_id'", func() {
Expect(p.schema.xinfo.idx["any_id"]).To(Equal(1))
})
It("should contain 'any_id' as own key", func() {
Expect(p.schema.fks["any"].from).To(Equal("any_id"))
})
It("should contain 'id' as a target key", func() {
Expect(p.schema.fks["any"].target).To(Equal("id"))
})
})
Context("when a struct has overrided foreign key", func() {
type something struct {
AnyID int64 `y:"aid,fk:any.id"`
}
BeforeEach(func() {
p = New(something{})
})
It("should contain index opts for 'aid'", func() {
Expect(p.schema.xinfo.idx["aid"]).To(Equal(1))
})
It("should contain 'aid' as own key", func() {
Expect(p.schema.fks["any"].from).To(Equal("aid"))
})
It("should contain 'id' as a target key", func() {
Expect(p.schema.fks["any"].target).To(Equal("id"))
})
})
})