-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_select_funcs_test.go
120 lines (114 loc) · 2.68 KB
/
query_select_funcs_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
package dal
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSelectAll(t *testing.T) {
type args struct {
reader func() Reader
limit int
}
type testCase[T comparable] struct {
name string
args args
shouldPanic bool
wantIds []T
wantErr error
}
getRecordsReader := func() Reader {
return NewRecordsReader([]Record{
&record{key: &Key{ID: 1, collection: "test"}},
&record{key: &Key{ID: 2, collection: "test"}},
&record{key: &Key{ID: 3, collection: "test"}},
&record{key: &Key{ID: 4, collection: "test"}},
})
}
tests := []testCase[int]{
{name: "nil_reader", shouldPanic: true, args: args{reader: func() Reader {
return nil
}}},
{name: "empty_reader", args: args{reader: func() Reader {
return &EmptyReader{}
}}, wantIds: []int{}, wantErr: nil},
{
name: "with_records_0_limit",
args: args{
limit: 0,
reader: getRecordsReader,
},
wantIds: []int{1, 2, 3, 4},
},
{
name: "with_records_negative_limit",
args: args{
reader: getRecordsReader,
limit: -1,
},
wantIds: []int{1, 2, 3, 4},
},
{
name: "with_records_limit_2",
args: args{
limit: 2,
reader: getRecordsReader,
},
wantIds: []int{1, 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertErr := func(t *testing.T, err error) {
if tt.wantErr == nil && err != nil {
t.Errorf("unexpected error: %v", err)
}
if tt.wantErr != nil && err == nil {
t.Errorf("expected error: %v", tt.wantErr)
}
if tt.wantErr != nil && err != nil && !errors.Is(err, tt.wantErr) {
t.Errorf("expected %v, got %v", tt.wantErr, err)
}
}
t.Run("SelectAllIDs", func(t *testing.T) {
if tt.shouldPanic {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
}
gotIds, err := SelectAllIDs[int](tt.args.reader(), WithLimit(tt.args.limit))
assertErr(t, err)
assert.Equal(t, tt.wantIds, gotIds)
})
t.Run("SelectAllRecords", func(t *testing.T) {
if tt.shouldPanic {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
}
gotRecords, err := SelectAllRecords(tt.args.reader(), WithLimit(tt.args.limit))
assertErr(t, err)
if err == nil {
assert.NotNil(t, gotRecords)
}
})
})
}
}
func TestWithOffset(t *testing.T) {
ro := new(readerOptions)
WithOffset(3)(ro)
assert.Equal(t, 3, ro.offset)
ro.offset = 0
assert.Equal(t, readerOptions{}, *ro)
}
func TestWithLimit(t *testing.T) {
ro := new(readerOptions)
WithLimit(4)(ro)
assert.Equal(t, 4, ro.limit)
ro.limit = 0
assert.Equal(t, readerOptions{}, *ro)
}