-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathoverride_test.go
More file actions
126 lines (122 loc) · 2.55 KB
/
override_test.go
File metadata and controls
126 lines (122 loc) · 2.55 KB
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
package opts
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestTypeOverrides(t *testing.T) {
for _, test := range []struct {
override Override
pkg string
typeName string
basic bool
}{
{
Override{
DBType: "uuid",
GoType: GoType{Spec: "github.com/segmentio/ksuid.KSUID"},
},
"github.com/segmentio/ksuid",
"ksuid.KSUID",
false,
},
// TODO: Add test for struct pointers
//
// {
// Override{
// DBType: "uuid",
// GoType: "github.com/segmentio/*ksuid.KSUID",
// },
// "github.com/segmentio/ksuid",
// "*ksuid.KSUID",
// false,
// },
{
Override{
DBType: "citext",
GoType: GoType{Spec: "string"},
},
"",
"string",
true,
},
{
Override{
DBType: "timestamp",
GoType: GoType{Spec: "time.Time"},
},
"time",
"time.Time",
false,
},
{
Override{
DBType: "text",
GoType: GoType{Spec: "github.com/disgoorg/snowflake/v2.ID"},
},
"github.com/disgoorg/snowflake/v2",
"snowflake.ID",
false,
},
} {
tt := test
t.Run(tt.override.GoType.Spec, func(t *testing.T) {
if err := tt.override.parse(nil); err != nil {
t.Fatalf("override parsing failed; %s", err)
}
if diff := cmp.Diff(tt.pkg, tt.override.GoImportPath); diff != "" {
t.Errorf("package mismatch;\n%s", diff)
}
if diff := cmp.Diff(tt.typeName, tt.override.GoTypeName); diff != "" {
t.Errorf("type name mismatch;\n%s", diff)
}
if diff := cmp.Diff(tt.basic, tt.override.GoBasicType); diff != "" {
t.Errorf("basic mismatch;\n%s", diff)
}
})
}
for _, test := range []struct {
override Override
err string
}{
{
Override{
DBType: "uuid",
GoType: GoType{Spec: "Pointer"},
},
"Package override `go_type` specifier \"Pointer\" is not a Go basic type e.g. 'string'",
},
{
Override{
DBType: "uuid",
GoType: GoType{Spec: "untyped rune"},
},
"Package override `go_type` specifier \"untyped rune\" is not a Go basic type e.g. 'string'",
},
} {
tt := test
t.Run(tt.override.GoType.Spec, func(t *testing.T) {
err := tt.override.parse(nil)
if err == nil {
t.Fatalf("expected parse to fail; got nil")
}
if diff := cmp.Diff(tt.err, err.Error()); diff != "" {
t.Errorf("error mismatch;\n%s", diff)
}
})
}
}
func FuzzOverride(f *testing.F) {
for _, spec := range []string{
"string",
"github.com/gofrs/uuid.UUID",
"github.com/segmentio/ksuid.KSUID",
} {
f.Add(spec)
}
f.Fuzz(func(t *testing.T, s string) {
o := Override{
GoType: GoType{Spec: s},
}
o.parse(nil)
})
}