-
Notifications
You must be signed in to change notification settings - Fork 2
/
values_test.go
172 lines (153 loc) · 4.39 KB
/
values_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
package sol
import (
"encoding/json"
"reflect"
"testing"
"time"
)
func TestValues(t *testing.T) {
values := Values{"c": []byte("bytes")}
b, err := json.Marshal(values)
if err != nil {
t.Fatalf("Unexpected error while marshaling: %s", err)
}
if string(b) != `{"c":"bytes"}` {
t.Errorf(`Unexpected JSON marshal: %s != {"c":"bytes"}`, b)
}
}
func TestValues_Equals(t *testing.T) {
if !(Values{}).Equals(Values{}) {
t.Errorf("Empty maps should be equal to each other")
}
if (Values{"a": 1}).Equals(Values{"a": true}) {
t.Errorf("Maps with different values should not be equal")
}
if (Values{"a": 1}).Equals(Values{"a": 1, "b": 2}) {
t.Errorf("Maps with a different number of keys should not be equal")
}
}
func TestValues_Exclude(t *testing.T) {
unsafe := Values{"a": 1, "b": 1}
safe := unsafe.Exclude("a", "c")
if len(safe) != 1 {
t.Errorf("Unexpected length of safe Values: %d != 1", len(safe))
}
keys := safe.Keys()
if len(keys) != 1 {
t.Errorf("Unexpected length of safe keys: %d != 1", len(keys))
}
if keys[0] != "b" {
t.Errorf("Unexpected safe key: %s != b", keys[0])
}
}
func TestValues_Merge(t *testing.T) {
a := Values{"a": 1, "b": 2}
b := Values{"b": 3, "c": 4}
c := a.Merge(b)
if len(c) != 3 {
t.Errorf("Unexpected length of c Values: %d != 3", len(c))
}
v, ok := c["c"].(int)
if !ok {
t.Fatal("Failed to convert the 'c' value to int")
}
if v != 4 {
t.Errorf("Unexpected value of 'c': %d != 4", v)
}
// a should not be affected
if _, exists := a["c"]; exists {
t.Errorf("The original Values should not be modified")
}
}
func TestValuesOf(t *testing.T) {
var out Values
var err error
// Test map types
values := Values{"a": 1}
if out, err = ValuesOf(values); err != nil {
t.Errorf("Unexpected error from ValuesOf() with Values: %s", err)
}
if !reflect.DeepEqual(values, out) {
t.Errorf("Unexpected values from ValuesOf() with Values: %+v", out)
}
if out, err = ValuesOf(&values); err != nil {
t.Errorf("Unexpected error from ValuesOf() with *Values: %s", err)
}
if !reflect.DeepEqual(values, out) {
t.Errorf("Unexpected values from ValuesOf() with *Values: %+v", out)
}
attrs := map[string]interface{}{"b": 2}
if out, err = ValuesOf(attrs); err != nil {
t.Errorf("Unexpected error from ValuesOf() with map: %s", err)
}
if !reflect.DeepEqual(Values(attrs), out) {
t.Errorf("Unexpected values from ValuesOf() with map: %+v", out)
}
if out, err = ValuesOf(&attrs); err != nil {
t.Errorf("Unexpected error from ValuesOf() with *map: %s", err)
}
if !reflect.DeepEqual(Values(attrs), out) {
t.Errorf("Unexpected values from ValuesOf() with *map: %+v", out)
}
// The following types are declared in fields_test
embed := embedded{
Serial: Serial{ID: int64(20)},
Name: "Object",
}
if out, err = ValuesOf(embed); err != nil {
t.Errorf("Unexpected error from ValuesOf() with struct: %s", err)
}
expected := Values{
"id": int64(20),
"Name": "Object",
"UpdatedAt": (*time.Time)(nil),
"Custom": customScanner{},
"Value": false,
}
if !reflect.DeepEqual(expected, out) {
t.Errorf("Unexpected values from ValuesOf() with *struct: %+v", out)
}
if out, err = ValuesOf(embed); err != nil {
t.Errorf("Unexpected error from ValuesOf() with *struct: %s", err)
}
if !reflect.DeepEqual(expected, out) {
t.Errorf("Unexpected values from ValuesOf() with *struct: %+v", out)
}
now := time.Now()
embed.Serial.ID = int64(0)
embed.Timestamp.CreatedAt = now
if out, err = ValuesOf(embed); err != nil {
t.Errorf("Unexpected error from ValuesOf() with struct: %s", err)
}
delete(expected, "id")
expected["CreatedAt"] = now
if !reflect.DeepEqual(expected, out) {
t.Errorf("Unexpected values from ValuesOf() with *struct: %+v", out)
}
}
var emptyValues = []bool{
isEmptyValue(reflect.ValueOf(0)),
isEmptyValue(reflect.ValueOf("")),
isEmptyValue(reflect.ValueOf(false)),
isEmptyValue(reflect.ValueOf(0.0)),
isEmptyValue(reflect.ValueOf(time.Time{})),
}
var nonEmptyValues = []bool{
isEmptyValue(reflect.ValueOf(1)),
isEmptyValue(reflect.ValueOf("h")),
isEmptyValue(reflect.ValueOf(true)),
isEmptyValue(reflect.ValueOf(0.1)),
isEmptyValue(reflect.ValueOf(time.Now())),
}
func TestIsEmptyValue(t *testing.T) {
for i, isEmpty := range emptyValues {
if !isEmpty {
t.Errorf("Value %d should be empty", i)
}
}
for i, isNotEmpty := range nonEmptyValues {
if isNotEmpty {
t.Errorf("Value %d should not be empty", i)
}
}
}