-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream_test.go
129 lines (109 loc) · 2.94 KB
/
stream_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 filetypes_test
import (
"bufio"
"fmt"
"io"
"testing"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/cloudquery/filetypes/v4"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type uploadHelper struct {
t *testing.T
failAfter int
expect []byte
expectAt int
}
var errTest = fmt.Errorf("test error")
func (u *uploadHelper) Upload(r io.Reader) error {
s := bufio.NewScanner(r)
i := 0
for s.Scan() {
if u.failAfter > 0 && i == u.failAfter {
return errTest
}
if u.expect != nil && i == u.expectAt {
if !assert.Equal(u.t, u.expect, s.Bytes()) {
return fmt.Errorf("assertion failed")
}
}
i++
}
return s.Err()
}
func TestHappyPath(t *testing.T) {
cl, err := filetypes.NewClient(&filetypes.FileSpec{
Format: filetypes.FormatTypeJSON,
})
require.NoError(t, err)
table := &schema.Table{
Name: "test",
Columns: []schema.Column{
{Name: "name", Type: arrow.BinaryTypes.String},
},
}
u := &uploadHelper{
t: t,
expect: []byte(`{"name":"bar"}`),
expectAt: 1,
}
s, err := cl.StartStream(table, u.Upload)
require.NoError(t, err)
bldr := array.NewRecordBuilder(memory.DefaultAllocator, table.ToArrowSchema())
bldr.Field(0).(*array.StringBuilder).Append("foo")
bldr.Field(0).(*array.StringBuilder).Append("bar")
record := bldr.NewRecord()
require.NoError(t, s.Write([]arrow.Record{record}))
require.NoError(t, s.Finish())
}
func TestWriteError(t *testing.T) {
cl, err := filetypes.NewClient(&filetypes.FileSpec{
Format: filetypes.FormatTypeJSON,
})
require.NoError(t, err)
table := &schema.Table{
Name: "test",
Columns: []schema.Column{
{Name: "name", Type: arrow.BinaryTypes.String},
},
}
u := &uploadHelper{
t: t,
failAfter: 1,
}
s, err := cl.StartStream(table, u.Upload)
require.NoError(t, err)
bldr := array.NewRecordBuilder(memory.DefaultAllocator, table.ToArrowSchema())
bldr.Field(0).(*array.StringBuilder).Append("foo")
bldr.Field(0).(*array.StringBuilder).Append("bar")
record := bldr.NewRecord()
require.NoError(t, s.Write([]arrow.Record{record}))
require.ErrorIs(t, s.Finish(), errTest)
}
func TestCloseError(t *testing.T) {
cl, err := filetypes.NewClient(&filetypes.FileSpec{
Format: filetypes.FormatTypeJSON,
})
require.NoError(t, err)
table := &schema.Table{
Name: "test",
Columns: []schema.Column{
{Name: "name", Type: arrow.BinaryTypes.String},
},
}
u := &uploadHelper{
t: t,
}
s, err := cl.StartStream(table, u.Upload)
require.NoError(t, err)
bldr := array.NewRecordBuilder(memory.DefaultAllocator, table.ToArrowSchema())
bldr.Field(0).(*array.StringBuilder).Append("foo")
bldr.Field(0).(*array.StringBuilder).Append("bar")
record := bldr.NewRecord()
require.NoError(t, s.Write([]arrow.Record{record}))
require.ErrorIs(t, s.FinishWithError(errTest), errTest)
}