-
Notifications
You must be signed in to change notification settings - Fork 22
/
testing_upsert.go
125 lines (108 loc) · 3.8 KB
/
testing_upsert.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
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 (s *WriterTestSuite) testUpsertBasic(ctx context.Context) error {
tableName := s.tableNameForTest("upsert_basic")
table := &schema.Table{
Name: tableName,
Columns: []schema.Column{
{Name: "id", Type: arrow.PrimitiveTypes.Int64, NotNull: true},
{Name: "name", Type: arrow.BinaryTypes.String, PrimaryKey: true},
},
}
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
records, err := s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to readAll: %w", err)
}
totalItems := TotalRows(records)
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)
}
records, err = s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to sync: %w", err)
}
totalItems = TotalRows(records)
if totalItems != 1 {
return fmt.Errorf("expected 1 item, got %d", totalItems)
}
if diff := RecordDiff(records[0], record); diff != "" {
return fmt.Errorf("record differs: %s", diff)
}
return nil
}
func (s *WriterTestSuite) testUpsertAll(ctx context.Context) error {
tableName := s.tableNameForTest("upsert_all")
table := schema.TestTable(tableName, s.genDatOptions)
table.Columns = append(table.Columns, schema.Column{Name: "name", Type: arrow.BinaryTypes.String, PrimaryKey: true})
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
records, err := s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to readAll: %w", err)
}
totalItems := TotalRows(records)
if totalItems != 1 {
return fmt.Errorf("expected 1 item, got %d", totalItems)
}
if diff := RecordDiff(records[0], normalRecord); diff != "" {
return fmt.Errorf("record differs after insert: %s", diff)
}
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
records, err = s.plugin.readAll(ctx, table)
if err != nil {
return fmt.Errorf("failed to sync: %w", err)
}
totalItems = TotalRows(records)
if totalItems != 1 {
return fmt.Errorf("expected 1 item, got %d", totalItems)
}
if diff := RecordDiff(records[0], nullRecord); diff != "" {
return fmt.Errorf("record differs after upsert (columns should be null): %s", diff)
}
return nil
}