-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
226 lines (203 loc) · 5.17 KB
/
parse_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package pgc
import (
"fmt"
"reflect"
"regexp"
"strings"
"testing"
"github.com/cliqueinc/pgc/util"
)
// See ops_test for some good test structs
func TestParseModelSimple(t *testing.T) {
// TODO might be able to turn these into table tests
type simpleAddress struct {
Street string
State string
City string
ID string
}
addr := &simpleAddress{
fmt.Sprintf("%d %s %s", util.RandomInt(10000, 999999),
util.RandomString(10), util.RandomString(10)),
util.RandomString(2),
util.RandomString(8) + " " + util.RandomString(5),
util.RandomString(40),
}
model := parseModel(addr, true)
if model == nil {
t.Fatalf("parseModel failed, got nil model back")
}
if model.StructName != "simpleAddress" || model.ReflectType.Kind() != reflect.Ptr {
t.Errorf("parseModel failed, expected name simpleAddress type ptr, %v", model)
}
if model.TableName != "simple_address" {
t.Errorf("Expected TableName simple_address, got %s", model.TableName)
}
if len(model.Fields) < 3 {
t.Fatalf("Didnt find three fields, found %d", len(model.Fields))
}
// This is a fairly stupid test, ug. Probably change to table tests
for i, f := range model.Fields {
switch i {
case 0:
if f.GoName != "Street" || f.PGName != "street" {
t.Errorf("Expected Go:Street, PG:street, was %s %s", f.GoName, f.PGName)
}
case 1:
if f.GoName != "State" || f.PGName != "state" {
t.Errorf("Expected Go:State, PG:state, was %s %s", f.GoName, f.PGName)
}
case 2:
if f.GoName != "City" || f.PGName != "city" {
t.Errorf("Expected Go:City, PG:city, was %s %s", f.GoName, f.PGName)
}
}
if !strings.HasPrefix(f.PGType, "text") { // Could be text with PRIMARY KEY
t.Errorf("Expected pg type text, got %s", f.PGType)
}
if f.ReflectKind != reflect.String {
t.Errorf("Expected reflect go type king string, got %d", f.ReflectKind)
}
}
}
func assertPanicParseModel(t *testing.T, badThing interface{}) {
defer func() {
if r := recover(); r == nil {
t.Errorf("assertPanicParseModel should have panicked")
}
}()
parseModel(badThing, true)
}
// This test should panic on the current not supported struct pointer field
func TestParseModelPanicPtrField(t *testing.T) {
type ptrAddress struct {
Street string
State *string
City string
}
assertPanicParseModel(t, &ptrAddress{})
}
// This test should panic on the current not supported struct pointer field
func TestParseModelPanicNonStruct(t *testing.T) {
type ptrAddress struct {
Street string
State *string
City string
}
assertPanicParseModel(t, ptrAddress{})
assertPanicParseModel(t, 42)
}
type tableMeth struct {
ID string
}
func (t tableMeth) TableName() string {
return "not_like_the_real_meth"
}
func TestGetTableName(t *testing.T) {
// Going to avoid using parseModel() directly for this test to isolate things
tm := &tableMeth{ID: util.RandomString(30)}
mod := &model{
Struct: tm,
ReflectType: reflect.TypeOf(tm),
}
rowModel := reflect.ValueOf(tm)
mod.StructName = rowModel.Elem().Type().Name()
mod.setTableName(rowModel)
if mod.TableName != "not_like_the_real_meth" {
t.Errorf("Expected (%s), was (%s)", "not_like_the_real_meth", mod.TableName)
}
}
func TestGetTableNameNoMeth(t *testing.T) {
// Going to avoid using parseModel() directly for this test to isolate things
type noTableMeth struct{ ID string }
tm := &noTableMeth{ID: util.RandomString(30)}
mod := &model{
Struct: tm,
ReflectType: reflect.TypeOf(tm),
}
rowModel := reflect.ValueOf(tm)
mod.StructName = rowModel.Elem().Type().Name()
mod.setTableName(rowModel)
if mod.TableName != "no_table_meth" {
t.Errorf("Expected (%s), was (%s)", "no_table_meth", mod.TableName)
}
}
func Test_ParseName(t *testing.T) {
var cases = []struct {
Name string
Input, Output string
}{
{
Name: "simple camelcase",
Input: "MyCamelCasedName",
Output: "my_camel_cased_name",
},
{
Name: "id",
Input: "ID",
Output: "id",
},
{
Name: "all letters capitalized",
Input: "URL",
Output: "url",
},
{
Name: "capitalized after camelcase",
Input: "MyJSONString",
Output: "my_json_string",
},
{
Name: "ends with capitalized",
Input: "MyStringJSON",
Output: "my_string_json",
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
o := parseName(c.Input)
if o != c.Output {
t.Errorf("parseName failed: expected (%s), got (%s)", c.Output, o)
}
})
}
}
func Benchmark_ParseName(b *testing.B) {
names := []string{
"MyBigCamelCasedStringThatIsVeryLong",
"SimpleString",
"under_scored",
}
var rep = regexp.MustCompile(`[A-Z]+`)
oldParseFunc := func(name string) string {
if name == "ID" {
// Primary key
return "id"
}
for true {
if name == strings.ToLower(name) {
return name
}
name = rep.ReplaceAllStringFunc(name, func(s string) string {
return "_" + strings.ToLower(s)
})
name = strings.TrimLeft(name, "_")
}
panic("unreachable")
}
b.ReportAllocs()
b.Run("v1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, name := range names {
oldParseFunc(name)
}
}
})
b.Run("v2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, name := range names {
parseName(name)
}
}
})
}