|
1 | 1 | package connmysql |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
4 | 6 | "testing" |
5 | 7 |
|
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.temporal.io/sdk/log" |
| 10 | + |
6 | 11 | "github.com/PeerDB-io/peerdb/flow/generated/protos" |
7 | 12 | "github.com/PeerDB-io/peerdb/flow/shared/types" |
8 | 13 | ) |
@@ -82,3 +87,121 @@ func TestBuildSelectedColumns(t *testing.T) { |
82 | 87 | }) |
83 | 88 | } |
84 | 89 | } |
| 90 | + |
| 91 | +func TestGetDefaultPartitionKeyForTables(t *testing.T) { |
| 92 | + c := &MySqlConnector{logger: log.NewStructuredLogger(slog.Default())} |
| 93 | + |
| 94 | + tableMapping := func(name string) *protos.TableMapping { |
| 95 | + return &protos.TableMapping{SourceTableIdentifier: name, DestinationTableIdentifier: name} |
| 96 | + } |
| 97 | + |
| 98 | + fieldDesc := func(name string, qkind types.QValueKind) *protos.FieldDescription { |
| 99 | + return &protos.FieldDescription{Name: name, Type: string(qkind)} |
| 100 | + } |
| 101 | + |
| 102 | + testCases := []struct { |
| 103 | + schemas map[string]*protos.TableSchema |
| 104 | + expected map[string]string |
| 105 | + name string |
| 106 | + tableMappings []*protos.TableMapping |
| 107 | + }{ |
| 108 | + { |
| 109 | + name: "integer primary key is supported", |
| 110 | + tableMappings: []*protos.TableMapping{tableMapping("db.ints")}, |
| 111 | + schemas: map[string]*protos.TableSchema{ |
| 112 | + "db.ints": { |
| 113 | + PrimaryKeyColumns: []string{"id"}, |
| 114 | + Columns: []*protos.FieldDescription{fieldDesc("id", types.QValueKindInt64)}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + expected: map[string]string{"db.ints": "id"}, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "timestamp primary key is supported", |
| 121 | + tableMappings: []*protos.TableMapping{tableMapping("db.ts")}, |
| 122 | + schemas: map[string]*protos.TableSchema{ |
| 123 | + "db.ts": { |
| 124 | + PrimaryKeyColumns: []string{"created_at"}, |
| 125 | + Columns: []*protos.FieldDescription{fieldDesc("created_at", types.QValueKindTimestamp)}, |
| 126 | + }, |
| 127 | + }, |
| 128 | + expected: map[string]string{"db.ts": "created_at"}, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "composite primary key with valid first column", |
| 132 | + tableMappings: []*protos.TableMapping{tableMapping("db.composite")}, |
| 133 | + schemas: map[string]*protos.TableSchema{ |
| 134 | + "db.composite": { |
| 135 | + PrimaryKeyColumns: []string{"id", "created_at"}, |
| 136 | + Columns: []*protos.FieldDescription{ |
| 137 | + fieldDesc("id", types.QValueKindInt32), |
| 138 | + fieldDesc("created_at", types.QValueKindTimestamp), |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + expected: map[string]string{"db.composite": "id"}, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "composite primary key with invalid first column", |
| 146 | + tableMappings: []*protos.TableMapping{tableMapping("db.composite2")}, |
| 147 | + schemas: map[string]*protos.TableSchema{ |
| 148 | + "db.composite2": { |
| 149 | + PrimaryKeyColumns: []string{"name", "id"}, |
| 150 | + Columns: []*protos.FieldDescription{ |
| 151 | + fieldDesc("name", types.QValueKindString), |
| 152 | + fieldDesc("id", types.QValueKindInt32), |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + expected: map[string]string{}, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "no primary key", |
| 160 | + tableMappings: []*protos.TableMapping{tableMapping("db.nopk")}, |
| 161 | + schemas: map[string]*protos.TableSchema{ |
| 162 | + "db.nopk": { |
| 163 | + PrimaryKeyColumns: nil, |
| 164 | + Columns: []*protos.FieldDescription{fieldDesc("id", types.QValueKindInt64)}, |
| 165 | + }, |
| 166 | + }, |
| 167 | + expected: map[string]string{}, |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "multiple table schemas", |
| 171 | + tableMappings: []*protos.TableMapping{ |
| 172 | + tableMapping("db.a"), |
| 173 | + tableMapping("db.b"), |
| 174 | + tableMapping("db.c"), |
| 175 | + }, |
| 176 | + schemas: map[string]*protos.TableSchema{ |
| 177 | + "db.a": { |
| 178 | + PrimaryKeyColumns: []string{"id"}, |
| 179 | + Columns: []*protos.FieldDescription{fieldDesc("id", types.QValueKindInt16)}, |
| 180 | + }, |
| 181 | + "db.b": { |
| 182 | + PrimaryKeyColumns: []string{"uuid"}, |
| 183 | + Columns: []*protos.FieldDescription{ |
| 184 | + fieldDesc("uuid", types.QValueKindString), |
| 185 | + }, |
| 186 | + }, |
| 187 | + "db.c": { |
| 188 | + PrimaryKeyColumns: []string{"date"}, |
| 189 | + Columns: []*protos.FieldDescription{fieldDesc("date", types.QValueKindDate)}, |
| 190 | + }, |
| 191 | + }, |
| 192 | + expected: map[string]string{"db.a": "id", "db.c": "date"}, |
| 193 | + }, |
| 194 | + } |
| 195 | + |
| 196 | + for _, tc := range testCases { |
| 197 | + t.Run(tc.name, func(t *testing.T) { |
| 198 | + output, err := c.GetDefaultPartitionKeyForTables(context.Background(), &protos.GetDefaultPartitionKeyForTablesInput{ |
| 199 | + TableMappings: tc.tableMappings, |
| 200 | + TableSchemaMapping: tc.schemas, |
| 201 | + }) |
| 202 | + require.NoError(t, err) |
| 203 | + require.NotNil(t, output) |
| 204 | + require.Equal(t, tc.expected, output.TableDefaultPartitionKeyMapping) |
| 205 | + }) |
| 206 | + } |
| 207 | +} |
0 commit comments