|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "math/rand" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/apache/arrow-go/v18/arrow" |
| 12 | + "github.com/apache/arrow-go/v18/arrow/array" |
| 13 | + "github.com/apache/arrow-go/v18/arrow/memory" |
| 14 | + "github.com/cloudquery/plugin-sdk/v4/message" |
| 15 | + "github.com/cloudquery/plugin-sdk/v4/plugin" |
| 16 | + "github.com/cloudquery/plugin-sdk/v4/schema" |
| 17 | + "github.com/rs/zerolog" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +func TestDelete(t *testing.T) { |
| 22 | + testCases := []struct { |
| 23 | + name string |
| 24 | + insertValues []string |
| 25 | + deleteValues []string |
| 26 | + deleteAll bool |
| 27 | + expectedCount int |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "delete single record", |
| 31 | + insertValues: []string{"foo", "bar"}, |
| 32 | + deleteValues: []string{"foo"}, |
| 33 | + expectedCount: 1, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "delete both records", |
| 37 | + insertValues: []string{"foo", "bar"}, |
| 38 | + deleteValues: []string{"foo", "bar"}, |
| 39 | + expectedCount: 0, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "delete none", |
| 43 | + insertValues: []string{"foo"}, |
| 44 | + deleteValues: []string{"bar"}, |
| 45 | + expectedCount: 1, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "delete all records", |
| 49 | + insertValues: []string{"foo", "bar"}, |
| 50 | + deleteAll: true, |
| 51 | + expectedCount: 0, |
| 52 | + }, |
| 53 | + } |
| 54 | + for _, tc := range testCases { |
| 55 | + t.Run(tc.name, func(t *testing.T) { |
| 56 | + r := require.New(t) |
| 57 | + ctx := context.Background() |
| 58 | + client := withPluginClient(ctx, r) |
| 59 | + |
| 60 | + table := createTestTable() |
| 61 | + r.NoError(client.MigrateTables(ctx, message.WriteMigrateTables{{Table: table}})) |
| 62 | + |
| 63 | + writeInserts := createInsertMessages(tc.insertValues, table) |
| 64 | + r.NoError(client.WriteTableBatch(ctx, "", writeInserts)) |
| 65 | + |
| 66 | + writeDeletes := createDeleteMessages(tc.deleteAll, table, tc.deleteValues) |
| 67 | + r.NoError(client.DeleteRecord(ctx, writeDeletes)) |
| 68 | + |
| 69 | + count, err := countAllRows(ctx, client, table) |
| 70 | + r.NoError(err) |
| 71 | + r.EqualValues(tc.expectedCount, count, "unexpected amount of items after delete with match") |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func countAllRows(ctx context.Context, client *Client, table *schema.Table) (int64, error) { |
| 77 | + var err error |
| 78 | + ch := make(chan arrow.Record) |
| 79 | + go func() { |
| 80 | + defer close(ch) |
| 81 | + err = client.Read(ctx, table, ch) |
| 82 | + }() |
| 83 | + count := int64(0) |
| 84 | + for record := range ch { |
| 85 | + count += record.NumRows() |
| 86 | + } |
| 87 | + return count, err |
| 88 | +} |
| 89 | + |
| 90 | +func withPluginClient(ctx context.Context, r *require.Assertions) *Client { |
| 91 | + s := &Spec{ConnectionString: ":memory:"} |
| 92 | + b, err := json.Marshal(s) |
| 93 | + r.NoError(err) |
| 94 | + c, err := New(ctx, zerolog.Nop(), b, plugin.NewClientOptions{}) |
| 95 | + r.NoError(err) |
| 96 | + return c.(*Client) |
| 97 | +} |
| 98 | + |
| 99 | +func valueToArrowRecord(tableName string, value string) arrow.Record { |
| 100 | + bldrDeleteMatch := array.NewRecordBuilder(memory.DefaultAllocator, (&schema.Table{ |
| 101 | + Name: tableName, |
| 102 | + Columns: schema.ColumnList{ |
| 103 | + schema.Column{Name: "id", Type: arrow.BinaryTypes.String}, |
| 104 | + }, |
| 105 | + }).ToArrowSchema()) |
| 106 | + bldrDeleteMatch.Field(0).(*array.StringBuilder).Append(value) |
| 107 | + deleteValue := bldrDeleteMatch.NewRecord() |
| 108 | + return deleteValue |
| 109 | +} |
| 110 | + |
| 111 | +func createDeleteMessages(deleteAll bool, table *schema.Table, deleteValues []string) message.WriteDeleteRecords { |
| 112 | + writeDeletes := message.WriteDeleteRecords{} |
| 113 | + |
| 114 | + if deleteAll { |
| 115 | + msg := message.WriteDeleteRecord{ |
| 116 | + DeleteRecord: message.DeleteRecord{ |
| 117 | + TableName: table.Name, |
| 118 | + }, |
| 119 | + } |
| 120 | + return append(writeDeletes, &msg) |
| 121 | + } |
| 122 | + for _, deleteValue := range deleteValues { |
| 123 | + msg := message.WriteDeleteRecord{ |
| 124 | + DeleteRecord: message.DeleteRecord{ |
| 125 | + TableName: table.Name, |
| 126 | + WhereClause: message.PredicateGroups{ |
| 127 | + { |
| 128 | + GroupingType: "AND", |
| 129 | + Predicates: []message.Predicate{ |
| 130 | + { |
| 131 | + Operator: "eq", |
| 132 | + Column: "id", |
| 133 | + Record: valueToArrowRecord(table.Name, deleteValue), |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + writeDeletes = append(writeDeletes, &msg) |
| 141 | + } |
| 142 | + return writeDeletes |
| 143 | +} |
| 144 | + |
| 145 | +func createInsertMessages(values []string, table *schema.Table) message.WriteInserts { |
| 146 | + const sourceName = "source-test" |
| 147 | + writeInserts := message.WriteInserts{} |
| 148 | + for _, insertValue := range values { |
| 149 | + bldr := array.NewRecordBuilder(memory.DefaultAllocator, table.ToArrowSchema()) |
| 150 | + bldr.Field(0).(*array.StringBuilder).Append(insertValue) |
| 151 | + bldr.Field(1).(*array.StringBuilder).Append(sourceName) |
| 152 | + bldr.Field(2).(*array.TimestampBuilder).AppendTime(time.Now()) |
| 153 | + record := bldr.NewRecord() |
| 154 | + writeInserts = append(writeInserts, &message.WriteInsert{Record: record}) |
| 155 | + } |
| 156 | + return writeInserts |
| 157 | +} |
| 158 | + |
| 159 | +func createTestTable() *schema.Table { |
| 160 | + tableName := fmt.Sprintf("cq_delete_test_%d_%04d", time.Now().UnixNano(), rand.Intn(1000)) |
| 161 | + table := &schema.Table{ |
| 162 | + Name: tableName, |
| 163 | + Columns: schema.ColumnList{ |
| 164 | + schema.Column{Name: "id", Type: arrow.BinaryTypes.String, PrimaryKey: true, NotNull: true}, |
| 165 | + schema.CqSourceNameColumn, |
| 166 | + schema.CqSyncTimeColumn, |
| 167 | + }, |
| 168 | + } |
| 169 | + return table |
| 170 | +} |
0 commit comments