-
Notifications
You must be signed in to change notification settings - Fork 22
/
testing_write_insert.go
138 lines (119 loc) · 4.02 KB
/
testing_write_insert.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
130
131
132
133
134
135
136
137
138
package plugin
import (
"context"
"fmt"
"github.com/apache/arrow/go/v13/arrow"
"github.com/apache/arrow/go/v13/arrow/array"
"github.com/apache/arrow/go/v13/arrow/memory"
"github.com/cloudquery/plugin-sdk/v4/message"
"github.com/cloudquery/plugin-sdk/v4/schema"
)
func TotalRows(records []arrow.Record) int64 {
totalRows := int64(0)
for _, record := range records {
totalRows += record.NumRows()
}
return totalRows
}
func (s *WriterTestSuite) testInsertBasic(ctx context.Context) error {
tableName := s.tableNameForTest("insert_basic")
table := &schema.Table{
Name: tableName,
Columns: []schema.Column{
{Name: "id", Type: arrow.PrimitiveTypes.Int64, NotNull: true},
{Name: "name", Type: arrow.BinaryTypes.String},
},
}
if err := s.plugin.writeOne(ctx, &message.WriteMigrateTable{
Table: table,
}); err != nil {
return fmt.Errorf("failed to create table: %w", err)
}
bldr := array.NewRecordBuilder(memory.DefaultAllocator, table.ToArrowSchema())
bldr.Field(0).(*array.Int64Builder).Append(1)
bldr.Field(1).(*array.StringBuilder).Append("foo")
record := bldr.NewRecord()
if err := s.plugin.writeOne(ctx, &message.WriteInsert{
Record: record,
}); err != nil {
return fmt.Errorf("failed to insert record: %w", err)
}
record = s.handleNulls(record) // we process nulls after writing
readRecords, err := s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to sync: %w", err)
}
totalItems := TotalRows(readRecords)
if totalItems != 1 {
return fmt.Errorf("expected 1 item, got %d", totalItems)
}
if err := s.plugin.writeOne(ctx, &message.WriteInsert{
Record: record,
}); err != nil {
return fmt.Errorf("failed to insert record: %w", err)
}
readRecords, err = s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to sync: %w", err)
}
sortRecords(table, readRecords, "id")
totalItems = TotalRows(readRecords)
if totalItems != 2 {
return fmt.Errorf("expected 2 items, got %d", totalItems)
}
if diff := RecordDiff(readRecords[0], record); diff != "" {
return fmt.Errorf("record[0] differs: %s", diff)
}
if diff := RecordDiff(readRecords[1], record); diff != "" {
return fmt.Errorf("record[1] differs: %s", diff)
}
return nil
}
func (s *WriterTestSuite) testInsertAll(ctx context.Context) error {
tableName := s.tableNameForTest("insert_all")
table := schema.TestTable(tableName, s.genDatOptions)
if err := s.plugin.writeOne(ctx, &message.WriteMigrateTable{
Table: table,
}); err != nil {
return fmt.Errorf("failed to create table: %w", err)
}
tg := schema.NewTestDataGenerator()
normalRecord := tg.Generate(table, schema.GenTestDataOptions{MaxRows: 1, TimePrecision: s.genDatOptions.TimePrecision})[0]
if err := s.plugin.writeOne(ctx, &message.WriteInsert{
Record: normalRecord,
}); err != nil {
return fmt.Errorf("failed to insert record: %w", err)
}
normalRecord = s.handleNulls(normalRecord) // we process nulls after writing
readRecords, err := s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to sync: %w", err)
}
totalItems := TotalRows(readRecords)
if totalItems != 1 {
return fmt.Errorf("expected 1 item, got %d", totalItems)
}
nullRecord := tg.Generate(table, schema.GenTestDataOptions{MaxRows: 1, TimePrecision: s.genDatOptions.TimePrecision, NullRows: true})[0]
if err := s.plugin.writeOne(ctx, &message.WriteInsert{
Record: nullRecord,
}); err != nil {
return fmt.Errorf("failed to insert record: %w", err)
}
nullRecord = s.handleNulls(nullRecord) // we process nulls after writing
readRecords, err = s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to sync: %w", err)
}
sortRecords(table, readRecords, "id")
totalItems = TotalRows(readRecords)
if totalItems != 2 {
return fmt.Errorf("expected 2 items, got %d", totalItems)
}
if diff := RecordDiff(readRecords[0], normalRecord); diff != "" {
return fmt.Errorf("record[0] differs: %s", diff)
}
if diff := RecordDiff(readRecords[1], nullRecord); diff != "" {
return fmt.Errorf("record[1] differs: %s", diff)
}
return nil
}