datasource_test.gno
3.23 Kb ยท 171 lines
1package datasource
2
3import (
4 "errors"
5 "testing"
6
7 "gno.land/p/demo/uassert"
8 "gno.land/p/demo/urequire"
9)
10
11func TestNewIterator(t *testing.T) {
12 cases := []struct {
13 name string
14 records []Record
15 err error
16 }{
17 {
18 name: "ok",
19 records: []Record{
20 testRecord{id: "1"},
21 testRecord{id: "2"},
22 testRecord{id: "3"},
23 },
24 },
25 {
26 name: "error",
27 err: errors.New("test"),
28 },
29 }
30
31 for _, tc := range cases {
32 t.Run(tc.name, func(t *testing.T) {
33 // Arrange
34 ds := testDatasource{
35 records: tc.records,
36 err: tc.err,
37 }
38
39 // Act
40 iter := NewIterator(ds)
41
42 // Assert
43 if tc.err != nil {
44 uassert.ErrorIs(t, tc.err, iter.Err())
45 return
46 }
47
48 uassert.NoError(t, iter.Err())
49
50 for i := 0; iter.Next(); i++ {
51 r := iter.Record()
52 urequire.NotEqual(t, nil, r, "valid record")
53 urequire.True(t, i < len(tc.records), "iteration count")
54 uassert.Equal(t, tc.records[i].ID(), r.ID())
55 }
56 })
57 }
58}
59
60func TestQueryRecords(t *testing.T) {
61 cases := []struct {
62 name string
63 records []Record
64 recordCount int
65 options []QueryOption
66 err error
67 }{
68 {
69 name: "ok",
70 records: []Record{
71 testRecord{id: "1"},
72 testRecord{id: "2"},
73 testRecord{id: "3"},
74 },
75 recordCount: 3,
76 },
77 {
78 name: "with count",
79 options: []QueryOption{WithCount(2)},
80 records: []Record{
81 testRecord{id: "1"},
82 testRecord{id: "2"},
83 testRecord{id: "3"},
84 },
85 recordCount: 2,
86 },
87 {
88 name: "invalid record",
89 records: []Record{
90 testRecord{id: "1"},
91 nil,
92 testRecord{id: "3"},
93 },
94 err: ErrInvalidRecord,
95 },
96 {
97 name: "iterator error",
98 records: []Record{
99 testRecord{id: "1"},
100 testRecord{id: "3"},
101 },
102 err: errors.New("test"),
103 },
104 }
105
106 for _, tc := range cases {
107 t.Run(tc.name, func(t *testing.T) {
108 // Arrange
109 ds := testDatasource{
110 records: tc.records,
111 err: tc.err,
112 }
113
114 // Act
115 records, err := QueryRecords(ds, tc.options...)
116
117 // Assert
118 if tc.err != nil {
119 uassert.ErrorIs(t, tc.err, err)
120 return
121 }
122
123 uassert.NoError(t, err)
124
125 urequire.Equal(t, tc.recordCount, len(records), "record count")
126 for i, r := range records {
127 urequire.NotEqual(t, nil, r, "valid record")
128 uassert.Equal(t, tc.records[i].ID(), r.ID())
129 }
130 })
131 }
132}
133
134type testDatasource struct {
135 records []Record
136 err error
137}
138
139func (testDatasource) Size() int { return -1 }
140func (testDatasource) Record(string) (Record, error) { return nil, nil }
141func (ds testDatasource) Records(Query) Iterator { return &testIter{records: ds.records, err: ds.err} }
142
143type testRecord struct {
144 id string
145 fields Fields
146 err error
147}
148
149func (r testRecord) ID() string { return r.id }
150func (r testRecord) String() string { return "str" + r.id }
151func (r testRecord) Fields() (Fields, error) { return r.fields, r.err }
152
153type testIter struct {
154 index int
155 records []Record
156 current Record
157 err error
158}
159
160func (it testIter) Err() error { return it.err }
161func (it testIter) Record() Record { return it.current }
162
163func (it *testIter) Next() bool {
164 count := len(it.records)
165 if it.err != nil || count == 0 || it.index >= count {
166 return false
167 }
168 it.current = it.records[it.index]
169 it.index++
170 return true
171}