forked from neo4j/neo4j-go-driver
/
asserts.go
312 lines (278 loc) · 6.8 KB
/
asserts.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package testutil contains shared test functionality
package testutil
import (
"fmt"
"io"
"reflect"
"sort"
"strings"
"testing"
"time"
"github.com/SGNL-ai/neo4j-go-driver/v5/neo4j/db"
)
func AssertNextOnlyRecord(t *testing.T, rec *db.Record, sum *db.Summary, err error) {
t.Helper()
if rec == nil {
t.Errorf("Expected record")
}
if sum != nil {
t.Errorf("Didn't expect summary")
}
if err != nil {
t.Errorf("Didn't expect error: %s", err)
}
}
func AssertNextOnlySummary(t *testing.T, rec *db.Record, sum *db.Summary, err error) {
t.Helper()
if rec != nil {
t.Errorf("Didn't expect record")
}
if sum == nil {
t.Errorf("Expected summary")
}
if err != nil {
t.Errorf("Didn't expect error")
}
}
func AssertNextOnlyError(t *testing.T, rec *db.Record, sum *db.Summary, err error) {
t.Helper()
if rec != nil {
t.Errorf("Didn't expect record")
}
if sum != nil {
t.Errorf("Didn't expect summary")
}
if err == nil {
t.Errorf("Expected error")
}
}
func AssertWriteSucceeds(t *testing.T, w io.Writer, b []byte) {
t.Helper()
_, err := w.Write(b)
AssertNoError(t, err)
}
func AssertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("Expected no error but was %T: %s", err, err)
}
}
func AssertErrorMessageContains(t *testing.T, err error, msg string, args ...any) {
AssertError(t, err)
AssertStringContain(t, err.Error(), fmt.Sprintf(msg, args...))
}
func AssertError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatal("Expected an error but none occurred")
}
}
func AssertNeo4jError(t *testing.T, err error) {
t.Helper()
AssertError(t, err)
_, isDbErr := err.(*db.Neo4jError)
if !isDbErr {
t.Errorf("Expected database error but was %T: %s", err, err)
}
}
func AssertNil(t *testing.T, x any) {
t.Helper()
if x != nil && !reflect.ValueOf(x).IsNil() {
t.Errorf("Expected nil but was %T: %s", x, x)
}
}
func AssertNotNil(t *testing.T, x any) {
t.Helper()
if x == nil || reflect.ValueOf(x).IsNil() {
t.Fatal("Expected not nil")
}
}
func AssertTrue(t *testing.T, b bool) {
t.Helper()
if !b {
t.Error("Expected true but was false")
}
}
func AssertFalse(t *testing.T, b bool) {
t.Helper()
if b {
t.Error("Expected false but was true")
}
}
func AssertLen(t *testing.T, x any, el int) {
t.Helper()
al := reflect.ValueOf(x).Len()
if al != el {
t.Errorf("Expected length %d but was %d", el, al)
}
}
func AssertSliceEqual(t *testing.T, x, y any) {
t.Helper()
lenx := reflect.ValueOf(x).Len()
leny := reflect.ValueOf(y).Len()
if lenx != leny {
t.Errorf("Lengths of slices differ %d vs %d", lenx, leny)
return
}
}
func AssertEmptyString(t *testing.T, s string) {
t.Helper()
if s != "" {
t.Errorf("Expected empty string but was '%s'", s)
}
}
func AssertStringNotEmpty(t *testing.T, s string) {
t.Helper()
if s == "" {
t.Errorf("Expected non empty string")
}
}
func AssertStringEqual(t *testing.T, as, es string) {
t.Helper()
if as != es {
t.Errorf("'%s' != '%s'", as, es)
}
}
func AssertStringContain(t *testing.T, s, sub string) {
t.Helper()
if !strings.Contains(s, sub) {
t.Errorf("Expected %s to contain %s", s, sub)
}
}
func AssertMapHasKey[K comparable](t *testing.T, m map[K]any, key K) {
t.Helper()
if _, ok := m[key]; !ok {
t.Errorf("Expected map to contain key %v", key)
}
}
func AssertMapDoesNotHaveKey[K comparable](t *testing.T, m map[K]any, key K) {
t.Helper()
if _, ok := m[key]; ok {
t.Errorf("Expected map to not contain key %v", key)
}
}
func AssertIntEqual(t *testing.T, ai, ei int) {
t.Helper()
if ai != ei {
t.Errorf("%d != %d", ai, ei)
}
if !reflect.DeepEqual(ai, ei) {
t.Errorf("Differs %+v vs %+v", ai, ei)
}
}
func AssertBoolEqual(t *testing.T, ai, ei bool) {
t.Helper()
if ai != ei {
t.Errorf("%t != %t", ai, ei)
}
if !reflect.DeepEqual(ai, ei) {
t.Errorf("Differs %+v vs %+v", ai, ei)
}
}
func AssertSameType(t *testing.T, x, y any) {
t.Helper()
t1 := reflect.TypeOf(x)
t2 := reflect.TypeOf(y)
if t1 != t2 {
t.Errorf("Expected types of %s and %s to be same but was %s and %s", x, y, t1, t2)
}
}
func AssertDeepEquals(t *testing.T, values ...any) {
t.Helper()
count := len(values)
if count == 0 {
return
}
prev := values[0]
for i := 1; i < count; i++ {
current := values[i]
if !reflect.DeepEqual(prev, current) {
t.Errorf("Expected value %v (parameter %d) to equal value %v (parameter %d)", prev, i-1, current, i)
return
}
prev = current
}
}
func AssertNotDeepEquals(t *testing.T, value1, value2 any) {
t.Helper()
if reflect.DeepEqual(value1, value2) {
t.Errorf("Expected value %v to not equal value %v", value1, value2)
return
}
}
func AssertAfter(t *testing.T, t1, t2 time.Time) {
t.Helper()
if !t1.After(t2) {
t.Errorf("Expected time %v to be after time %v", t1, t2)
}
}
func AssertPanics(t *testing.T, f func()) {
t.Helper()
defer func() {
if err := recover(); err == nil {
t.Errorf("Expected to function to panic, but did not or emitted a nil value")
}
}()
f()
}
func AssertEqualsInAnyOrder(t *testing.T, actual []string, expected []string) {
t.Helper()
if len(actual) != len(expected) {
t.Errorf("Expected actual slice %v and expected slice %v to have same length", actual, expected)
return
}
actual = actual[0:len(actual):len(actual)]
sort.Strings(actual)
sort.Strings(expected)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected actual slice %v and expected slice %v to have same elements", actual, expected)
}
}
func AssertVersionInHandshake(t *testing.T, handshake []byte, major, minor byte) {
t.Helper()
AssertMajorVersionInHandshake(t, handshake, major)
var minorMatches bool
for i := 0; i < 5; i++ {
version := handshake[(i * 4) : (i*4)+4]
if version[3] == major {
if version[2] >= minor {
minorMatches = true
break
}
}
}
if !minorMatches {
t.Errorf("Didn't find minor version >= %d in handshake: %X", minor, handshake)
}
}
func AssertMajorVersionInHandshake(t *testing.T, handshake []byte, major byte) {
t.Helper()
var majorMatches bool
for i := 0; i < 5; i++ {
version := handshake[(i * 4) : (i*4)+4]
if version[3] == major {
majorMatches = true
}
}
if !majorMatches {
t.Errorf("Didn't find major version %d in handshake: %X", major, handshake)
}
}