-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
129 lines (120 loc) · 3.48 KB
/
reader_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
package dal
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestReadAll(t *testing.T) {
for _, tt := range []struct {
name string
reader Reader
shouldPanic bool
expectedRecordsCount int
expectedErrTexts []string
}{
{name: "nil_reader_no_limit", reader: nil, shouldPanic: true},
{name: "empty_reader", reader: &EmptyReader{}, expectedRecordsCount: 0},
{name: "2records", reader: NewRecordsReader([]Record{
NewRecord(NewKeyWithID("collection", 1)),
NewRecord(NewKeyWithID("collection", 2)),
}), expectedRecordsCount: 2},
{name: "fails in next", reader: NewRecordsReader(nil), expectedErrTexts: []string{"no records"}},
} {
t.Run(tt.name, func(t *testing.T) {
if tt.shouldPanic {
defer func() {
if r := recover(); r == nil {
t.Errorf("panic expected")
}
}()
}
ctx := context.Background()
records, err := ReadAll(ctx, tt.reader, -1)
if tt.expectedErrTexts == nil {
assert.Nil(t, err)
assert.Equal(t, tt.expectedRecordsCount, len(records))
} else {
assert.Nil(t, records)
assert.NotNil(t, err)
for _, expectedErrText := range tt.expectedErrTexts {
assert.Contains(t, err.Error(), expectedErrText)
}
}
})
}
}
func TestRecordsReader(t *testing.T) {
t.Run("Close", func(t *testing.T) {
reader := &recordsReader{}
err := reader.Close()
assert.Nil(t, err)
})
t.Run("Cursor", func(t *testing.T) {
reader := NewRecordsReader([]Record{NewRecord(NewKeyWithID("a", "b"))})
cursor, err := reader.Cursor()
assert.True(t, errors.Is(err, ErrReaderNotStarted))
assert.Equal(t, "", cursor)
_, err = reader.Next()
assert.Nil(t, err)
cursor, err = reader.Cursor()
assert.Nil(t, err)
assert.Equal(t, "0", cursor)
_, err = reader.Next()
assert.True(t, errors.Is(err, ErrNoMoreRecords))
cursor, err = reader.Cursor()
assert.Equal(t, ErrReaderClosed.Error(), err.Error())
assert.Equal(t, "", cursor)
})
t.Run("Next", func(t *testing.T) {
for _, tt := range []struct {
name string
reader Reader
expectedErr error
expectedErrTexts []string
}{
{name: "no_records", reader: NewRecordsReader(nil), expectedErrTexts: []string{"no records"}},
{name: "empty_records", reader: NewRecordsReader([]Record{}), expectedErr: ErrNoMoreRecords},
{name: "single_record", reader: NewRecordsReader([]Record{NewRecord(NewKeyWithID("a", "b"))}), expectedErr: nil},
} {
t.Run(tt.name, func(t *testing.T) {
record, err := tt.reader.Next()
if tt.expectedErr == nil && tt.expectedErrTexts == nil {
assert.Nil(t, err)
assert.NotNil(t, record)
} else {
if tt.expectedErr != nil {
assert.True(t, errors.Is(err, tt.expectedErr))
if errors.Is(err, ErrNoMoreRecords) {
assert.Nil(t, record)
}
}
if tt.expectedErrTexts != nil {
for _, expectedErrText := range tt.expectedErrTexts {
assert.Contains(t, err.Error(), expectedErrText)
}
}
}
})
}
})
}
func TestEmptyReader(t *testing.T) {
t.Run("Close", func(t *testing.T) {
reader := &EmptyReader{}
err := reader.Close()
assert.Nil(t, err)
})
t.Run("Cursor", func(t *testing.T) {
reader := &EmptyReader{}
cursor, err := reader.Cursor()
assert.Equal(t, ErrNotSupported, err)
assert.Equal(t, "", cursor)
})
t.Run("Next", func(t *testing.T) {
reader := &EmptyReader{}
record, err := reader.Next()
assert.Equal(t, ErrNoMoreRecords, err)
assert.Nil(t, record)
})
}