-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandomize_test.go
161 lines (132 loc) · 3.72 KB
/
randomize_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
package randomize
import (
"reflect"
"testing"
"time"
)
type MagicType struct {
Value int
Randomized bool
}
func (m *MagicType) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
m.Value = int(nextInt())
m.Randomized = true
}
func TestRandomizeStruct(t *testing.T) {
t.Parallel()
s := NewSeed()
var testStruct = struct {
Int int
Int64 int64
Float64 float64
Bool bool
Time time.Time
String string
ByteSlice []byte
Interval string
Magic MagicType
Ignore int
}{}
fieldTypes := map[string]string{
"Int": "integer",
"Int64": "bigint",
"Float64": "decimal",
"Bool": "boolean",
"Time": "date",
"String": "character varying",
"ByteSlice": "bytea",
"Interval": "interval",
"Magic": "magic_type",
"Ignore": "integer",
}
err := Struct(s, &testStruct, fieldTypes, true, "Ignore")
if err != nil {
t.Fatal(err)
}
if testStruct.Ignore != 0 {
t.Error("blacklisted value was filled in:", testStruct.Ignore)
}
if testStruct.Int == 0 &&
testStruct.Int64 == 0 &&
testStruct.Float64 == 0 &&
testStruct.Bool == false &&
testStruct.Time.IsZero() &&
testStruct.String == "" &&
testStruct.Interval == "" &&
testStruct.Magic.Value == 0 &&
testStruct.ByteSlice == nil {
t.Errorf("the regular values are not being randomized: %#v", testStruct)
}
if !testStruct.Magic.Randomized {
t.Error("The randomize interface should have been used")
}
}
func TestRandomizeField(t *testing.T) {
t.Parallel()
type RandomizeTest struct {
In interface{}
Typs []string
Out interface{}
}
s := NewSeed()
inputs := []RandomizeTest{
{In: new(float32), Out: float32(0), Typs: []string{"real"}},
{In: new(float64), Out: float64(0), Typs: []string{"numeric"}},
{In: new(int), Out: int(0), Typs: []string{"integer"}},
{In: new(int8), Out: int8(0), Typs: []string{"integer"}},
{In: new(int16), Out: int16(0), Typs: []string{"smallserial"}},
{In: new(int32), Out: int32(0), Typs: []string{"integer"}},
{In: new(int64), Out: int64(0), Typs: []string{"bigserial"}},
{In: new(uint), Out: uint(0), Typs: []string{"integer"}},
{In: new(uint8), Out: uint8(0), Typs: []string{"integer"}},
{In: new(uint16), Out: uint16(0), Typs: []string{"integer"}},
{In: new(uint32), Out: uint32(0), Typs: []string{"integer"}},
{In: new(uint64), Out: uint64(0), Typs: []string{"integer"}},
{In: new(bool), Out: false},
{In: new(string), Out: ""},
{In: new([]byte), Out: new([]byte)},
{In: &time.Time{}, Out: &time.Time{}},
}
for i := 0; i < len(inputs); i++ {
for _, typ := range inputs[i].Typs {
val := reflect.Indirect(reflect.ValueOf(&inputs[i]))
field := val.FieldByName("In").Elem().Elem()
// Make sure we never get back values that would be considered null
// by the boil whitelist generator, or by the database driver
if err := randomizeField(s, field, typ, false); err != nil {
t.Errorf("%d) %s", i, err)
}
if inputs[i].In == inputs[i].Out {
t.Errorf("%d) Field should not be null, got: %v -- type: %s\n", i, inputs[i].In, typ)
}
}
}
}
func TestEnumValue(t *testing.T) {
t.Parallel()
s := NewSeed()
enum1 := "enum.workday('monday','tuesday')"
enum2 := "enum('monday','tuesday')"
enum3 := "enum('monday')"
r1, err := EnumValue(s.NextInt, enum1)
if err != nil {
t.Error(err)
}
if r1 != "monday" && r1 != "tuesday" {
t.Errorf("Expected monday or tuesday, got: %q", r1)
}
r2, err := EnumValue(s.NextInt, enum2)
if err != nil {
t.Error(err)
}
if r2 != "monday" && r2 != "tuesday" {
t.Errorf("Expected monday or tuesday, got: %q", r2)
}
r3, err := EnumValue(s.NextInt, enum3)
if err != nil {
t.Error(err)
}
if r3 != "monday" {
t.Errorf("Expected monday got: %q", r3)
}
}