-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtesting_support.go
More file actions
535 lines (477 loc) · 12.5 KB
/
Copy pathtesting_support.go
File metadata and controls
535 lines (477 loc) · 12.5 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package limen
import (
"cmp"
"context"
"fmt"
"maps"
"math"
"net/http"
"net/http/httptest"
"reflect"
"slices"
"strings"
"sync"
"testing"
"time"
)
// testSecret is a fixed 32-byte key for deterministic test encryption.
var testSecret = []byte("01234567890123456789012345678901")
// ---------------------------------------------------------------------------
// In-memory DatabaseAdapter
// ---------------------------------------------------------------------------
type testMemTable struct {
rows []map[string]any
nextID int64
}
type testMemoryAdapter struct {
mu sync.Mutex
tables map[SchemaTableName]*testMemTable
}
func newTestMemoryAdapter(t *testing.T) *testMemoryAdapter {
t.Helper()
return &testMemoryAdapter{
tables: make(map[SchemaTableName]*testMemTable),
}
}
func (a *testMemoryAdapter) table(name SchemaTableName) *testMemTable {
t, ok := a.tables[name]
if !ok {
t = &testMemTable{nextID: 1}
a.tables[name] = t
}
return t
}
func (a *testMemoryAdapter) Create(_ context.Context, tableName SchemaTableName, data map[string]any) (DatabaseResult, error) {
a.mu.Lock()
defer a.mu.Unlock()
if len(data) == 0 {
return DatabaseResult{}, fmt.Errorf("no data to insert")
}
tbl := a.table(tableName)
row := make(map[string]any, len(data))
for k, v := range data {
row[k] = testDerefPointer(v)
}
if _, hasID := row["id"]; !hasID {
row["id"] = tbl.nextID
tbl.nextID++
}
tbl.rows = append(tbl.rows, row)
return DatabaseResult{RowsAffected: 1}, nil
}
func (a *testMemoryAdapter) FindOne(_ context.Context, tableName SchemaTableName, conditions []Where, orderBy []OrderBy) (map[string]any, error) {
a.mu.Lock()
defer a.mu.Unlock()
tbl := a.table(tableName)
matched := testFilterRows(tbl.rows, conditions)
testSortRows(matched, orderBy)
if len(matched) == 0 {
return nil, ErrRecordNotFound
}
return maps.Clone(matched[0]), nil
}
func (a *testMemoryAdapter) FindMany(_ context.Context, tableName SchemaTableName, conditions []Where, options *QueryOptions) ([]map[string]any, error) {
a.mu.Lock()
defer a.mu.Unlock()
tbl := a.table(tableName)
matched := testFilterRows(tbl.rows, conditions)
if options != nil {
testSortRows(matched, options.OrderBy)
if options.Offset > 0 {
if options.Offset < len(matched) {
matched = matched[options.Offset:]
} else {
matched = nil
}
}
if options.Limit > 0 && options.Limit < len(matched) {
matched = matched[:options.Limit]
}
}
results := make([]map[string]any, len(matched))
for i, r := range matched {
results[i] = maps.Clone(r)
}
return results, nil
}
func (a *testMemoryAdapter) Update(_ context.Context, tableName SchemaTableName, conditions []Where, updates map[string]any) (DatabaseResult, error) {
a.mu.Lock()
defer a.mu.Unlock()
if len(updates) == 0 {
return DatabaseResult{}, nil
}
tbl := a.table(tableName)
var rowsAffected int64
for _, row := range tbl.rows {
if !testMatchesConditions(row, conditions) {
continue
}
for column, value := range updates {
arithmeticUpdate, ok := value.(ArithmeticUpdate)
if !ok {
row[column] = testDerefPointer(value)
continue
}
updated, err := testApplyArithmeticUpdate(row[column], arithmeticUpdate)
if err != nil {
return DatabaseResult{}, fmt.Errorf("update column %q: %w", column, err)
}
row[column] = updated
}
rowsAffected++
}
return DatabaseResult{RowsAffected: rowsAffected}, nil
}
func testApplyArithmeticUpdate(current any, update ArithmeticUpdate) (any, error) {
if err := update.Validate(); err != nil {
return nil, err
}
delta := update.Value()
switch value := testDerefPointer(current).(type) {
case int:
return value + int(delta), nil
case int32:
sum := int64(value) + delta
if sum > math.MaxInt32 || sum < math.MinInt32 {
return nil, fmt.Errorf("arithmetic update overflows int32")
}
return int32(sum), nil
case int64:
return value + delta, nil
}
return nil, fmt.Errorf("cannot apply arithmetic update to %T", current)
}
func (a *testMemoryAdapter) Delete(_ context.Context, tableName SchemaTableName, conditions []Where) (DatabaseResult, error) {
a.mu.Lock()
defer a.mu.Unlock()
tbl := a.table(tableName)
before := len(tbl.rows)
tbl.rows = slices.DeleteFunc(tbl.rows, func(row map[string]any) bool {
return testMatchesConditions(row, conditions)
})
return DatabaseResult{RowsAffected: int64(before - len(tbl.rows))}, nil
}
func (a *testMemoryAdapter) Exists(_ context.Context, tableName SchemaTableName, conditions []Where) (bool, error) {
a.mu.Lock()
defer a.mu.Unlock()
tbl := a.table(tableName)
for _, row := range tbl.rows {
if testMatchesConditions(row, conditions) {
return true, nil
}
}
return false, nil
}
func (a *testMemoryAdapter) Count(_ context.Context, tableName SchemaTableName, conditions []Where) (int64, error) {
a.mu.Lock()
defer a.mu.Unlock()
tbl := a.table(tableName)
var count int64
for _, row := range tbl.rows {
if testMatchesConditions(row, conditions) {
count++
}
}
return count, nil
}
// testDerefPointer flattens pointer values produced by ToStorage so the
// in-memory adapter behaves like a database driver.
func testDerefPointer(v any) any {
switch p := v.(type) {
case *string:
return testDereference(p)
case *time.Time:
return testDereference(p)
case *int:
return testDereference(p)
case *int32:
return testDereference(p)
case *int64:
return testDereference(p)
default:
return v
}
}
func testDereference[T any](value *T) any {
if value == nil {
return nil
}
return *value
}
// ---------------------------------------------------------------------------
// Condition matching
// ---------------------------------------------------------------------------
func testFilterRows(rows []map[string]any, conditions []Where) []map[string]any {
if len(conditions) == 0 {
return slices.Clone(rows)
}
var out []map[string]any
for _, row := range rows {
if testMatchesConditions(row, conditions) {
out = append(out, row)
}
}
return out
}
func testMatchesConditions(row map[string]any, conditions []Where) bool {
if len(conditions) == 0 {
return true
}
groups := GroupConditionsByConnector(conditions)
for _, group := range groups {
if !testMatchesGroup(row, group) {
return false
}
}
return true
}
func testMatchesGroup(row map[string]any, group []Where) bool {
for _, c := range group {
if testMatchesSingle(row, c) {
return true
}
}
return false
}
func testMatchesSingle(row map[string]any, c Where) bool {
val := row[c.Column]
switch c.Operator {
case OpIsNull:
return testIsNil(val)
case OpIsNotNull:
return !testIsNil(val)
case OpEq, "":
return testValuesEqual(val, c.Value)
case OpNe:
return !testValuesEqual(val, c.Value)
case OpLt:
order, ok := testCompareValues(val, c.Value)
return ok && order < 0
case OpLte:
order, ok := testCompareValues(val, c.Value)
return ok && order <= 0
case OpGt:
order, ok := testCompareValues(val, c.Value)
return ok && order > 0
case OpGte:
order, ok := testCompareValues(val, c.Value)
return ok && order >= 0
case OpContains:
return strings.Contains(fmt.Sprint(val), fmt.Sprint(c.Value))
case OpStartsWith:
return strings.HasPrefix(fmt.Sprint(val), fmt.Sprint(c.Value))
case OpEndsWith:
return strings.HasSuffix(fmt.Sprint(val), fmt.Sprint(c.Value))
case OpIn:
vals, ok := c.Value.([]any)
if !ok {
return false
}
for _, v := range vals {
if testValuesEqual(val, v) {
return true
}
}
return false
case OpNotIn:
vals, ok := c.Value.([]any)
if !ok {
return true
}
for _, v := range vals {
if testValuesEqual(val, v) {
return false
}
}
return true
default:
return testValuesEqual(val, c.Value)
}
}
func testValuesEqual(a, b any) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if a == b {
return true
}
// Named string types (e.g. InvitationStatus) must match plain strings.
if as, ok := testStringValue(a); ok {
bs, ok := testStringValue(b)
return ok && as == bs
}
return false
}
func testStringValue(value any) (string, bool) {
switch v := value.(type) {
case string:
return v, true
default:
rv := reflect.ValueOf(value)
if rv.Kind() == reflect.String {
return rv.String(), true
}
return "", false
}
}
func testIsNil(value any) bool {
if value == nil {
return true
}
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.Pointer, reflect.Map, reflect.Slice, reflect.Interface, reflect.Func, reflect.Chan:
return rv.IsNil()
default:
return false
}
}
func testAsTime(value any) (time.Time, bool) {
switch v := value.(type) {
case time.Time:
return v, true
case *time.Time:
if v == nil {
return time.Time{}, false
}
return *v, true
default:
return time.Time{}, false
}
}
func testCompareValues(a, b any) (int, bool) {
if av, ok := testIntegerValue(a); ok {
bv, ok := testIntegerValue(b)
if !ok {
return 0, false
}
return cmp.Compare(av, bv), true
}
if av, ok := testAsTime(a); ok {
bv, ok := testAsTime(b)
if !ok {
return 0, false
}
return av.Compare(bv), true
}
switch av := a.(type) {
case string:
bv, ok := b.(string)
if !ok {
return 0, false
}
return cmp.Compare(av, bv), true
default:
return 0, false
}
}
func testIntegerValue(value any) (int64, bool) {
switch value := value.(type) {
case int:
return int64(value), true
case int32:
return int64(value), true
case int64:
return value, true
default:
return 0, false
}
}
// ---------------------------------------------------------------------------
// Row sorting
// ---------------------------------------------------------------------------
func testSortRows(rows []map[string]any, orderBy []OrderBy) {
if len(orderBy) == 0 {
return
}
slices.SortStableFunc(rows, func(a, b map[string]any) int {
for _, ob := range orderBy {
if ob.Column == "" {
continue
}
order, ok := testCompareValues(a[ob.Column], b[ob.Column])
if !ok || order == 0 {
continue
}
if ob.Direction == OrderByDesc {
return -order
}
return order
}
return 0
})
}
// ---------------------------------------------------------------------------
// High-level test helpers
// ---------------------------------------------------------------------------
// NewTestLimen creates a fully-initialized *Limen backed by an in-memory
// adapter.
func NewTestLimen(t *testing.T, plugins ...Plugin) (*Limen, *LimenCore) {
t.Helper()
return NewTestLimenWithSchema(t, nil, plugins...)
}
// NewTestLimenWithSchema is NewTestLimen with a custom schema configuration,
// for tests that need schema-level features such as public IDs.
func NewTestLimenWithSchema(t *testing.T, schema *SchemaConfig, plugins ...Plugin) (*Limen, *LimenCore) {
t.Helper()
l, err := New(&Config{
BaseURL: "http://localhost:8080",
Database: newTestMemoryAdapter(t),
Schema: schema,
Secret: testSecret,
Plugins: plugins,
})
if err != nil {
t.Fatalf("NewTestLimen: %v", err)
}
return l, l.core
}
// SeedTestUser inserts a user directly into the in-memory DB and returns the
// full *User. The Limen instance must have been created with NewTestLimen.
func SeedTestUser(t *testing.T, l *Limen, email string) *User {
t.Helper()
ctx := context.Background()
extra := map[string]any{"first_name": "Test"}
if err := l.core.DBAction.CreateUser(ctx, &User{Email: email}, extra); err != nil {
t.Fatalf("SeedTestUser: %v", err)
}
user, err := l.core.DBAction.FindUserByEmail(ctx, email)
if err != nil {
t.Fatalf("SeedTestUser find: %v", err)
}
return user
}
// SeedTestSession creates a session and returns its SessionResult.
// The user must already exist.
func SeedTestSession(t *testing.T, l *Limen, userID any, email string) *SessionResult {
t.Helper()
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/signin", http.NoBody)
auth := &AuthenticationResult{User: &User{ID: userID, Email: email}}
result, err := l.core.SessionManager.CreateSession(context.Background(), req, auth, false)
if err != nil {
t.Fatalf("SeedTestSession: %v", err)
}
return result
}
// SeedTestSessionRecord creates a session and returns the stored *Session.
func SeedTestSessionRecord(t *testing.T, l *Limen, userID any, email string) *Session {
t.Helper()
result := SeedTestSession(t, l, userID, email)
if result.Token == "" {
t.Fatal("SeedTestSessionRecord: empty session token")
}
sessions, err := l.ListSessions(t.Context(), userID)
if err != nil {
t.Fatalf("SeedTestSessionRecord: %v", err)
}
for i := range sessions {
if sessions[i].Token == result.Token {
return &sessions[i]
}
}
t.Fatalf("SeedTestSessionRecord: session for token %q not found", result.Token)
return nil
}