aboutsummaryrefslogtreecommitdiff
path: root/plan_test.go
blob: e9d8dea2276fcc762429e47dc0db1a7a28ec6c46 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package oblast

// ^ NOTE: This is testing internal types and thus must reside in the same package.

import (
	"reflect"
	"testing"
	"time"

	"go.xyrillian.de/oblast/internal/assert"
)

func TestPlanFieldTraversal(t *testing.T) {
	type Timestamps struct {
		CreatedAt time.Time  `db:"created_at"`
		UpdatedAt *time.Time `db:"updated_at"`
	}
	type yetMoreTimestamps struct {
		DeletedAt *time.Time `db:"deleted_at"`
	}
	type Log struct {
		ID       int64 `db:"id,auto"`
		Message  string
		private1 bool `db:"private1"` //nolint:unused
		Ignored  any  `db:"-"`
		Timestamps
		yetMoreTimestamps
	}

	// check that the plan for Log:
	// 1. has no IndexByColumnName entries for marker types
	// 2. uses the field name as a column name for "Message"
	// 3. ignores "private1" because it cannot be written through reflection
	// 4. ignores "Ignored" because its column name is "-"
	// 5. traverses into "Timestamps" and includes its fields as well
	// 6. traverses into "yetMoreTimestamps" as well (despite the extra pointer and the type being private)
	// 7. recognizes "id" as an autofilled column
	plan, err := buildPlan(reflect.TypeFor[Log](), PostgresDialect(), planOpts{
		TableName:             "log_entries",
		PrimaryKeyColumnNames: []string{"id"},
	})
	if err != nil {
		t.Error(err)
	}
	assert.Equal(t, plan.TableName, "log_entries")
	assert.DeepEqual(t, plan.AllColumnNames, []string{"id", "Message", "created_at", "updated_at", "deleted_at"})
	assert.DeepEqual(t, plan.PrimaryKeyColumnNames, []string{"id"})
	assert.DeepEqual(t, plan.AutoColumnNames, []string{"id"})
	assert.DeepEqual(t, plan.IndexByColumnName, map[string][]int{
		"id":         {0},
		"Message":    {1},
		"created_at": {4, 0},
		"updated_at": {4, 1},
		"deleted_at": {5, 0},
	})
}

func TestQueryConstructionBasic(t *testing.T) {
	type record struct {
		ID          int64 `db:",auto"`
		Description string
		CreatedAt   time.Time `db:"CreatedAt"`
	}
	opts := planOpts{
		TableName:             "basic_records",
		PrimaryKeyColumnNames: []string{"ID"},
	}

	t.Run("MysqlDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[record](), MysqlDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, "SELECT `ID`, `Description`, `CreatedAt` FROM `basic_records` WHERE ")
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}})
		assert.Equal(t, plan.Insert.Query, "INSERT INTO `basic_records` (`Description`, `CreatedAt`) VALUES (?, ?)")
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{1}, {2}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, [][]int{{0}})
		assert.Equal(t, plan.Update.Query, "UPDATE `basic_records` SET `Description` = ?, `CreatedAt` = ? WHERE `ID` = ?")
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{1}, {2}, {0}})
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, "DELETE FROM `basic_records` WHERE `ID` = ?")
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}})
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})

	t.Run("PostgresDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[record](), PostgresDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, `SELECT "ID", "Description", "CreatedAt" FROM "basic_records" WHERE `)
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}})
		assert.Equal(t, plan.Insert.Query, `INSERT INTO "basic_records" ("Description", "CreatedAt") VALUES ($1, $2) RETURNING "ID"`)
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{1}, {2}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, [][]int{{0}})
		assert.Equal(t, plan.Update.Query, `UPDATE "basic_records" SET "Description" = $1, "CreatedAt" = $2 WHERE "ID" = $3`)
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{1}, {2}, {0}})
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, `DELETE FROM "basic_records" WHERE "ID" = $1`)
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}})
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})

	t.Run("SqliteDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[record](), SqliteDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, `SELECT "ID", "Description", "CreatedAt" FROM "basic_records" WHERE `)
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}})
		assert.Equal(t, plan.Insert.Query, `INSERT INTO "basic_records" ("Description", "CreatedAt") VALUES (?, ?)`)
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{1}, {2}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, [][]int{{0}})
		assert.Equal(t, plan.Update.Query, `UPDATE "basic_records" SET "Description" = ?, "CreatedAt" = ? WHERE "ID" = ?`)
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{1}, {2}, {0}})
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, `DELETE FROM "basic_records" WHERE "ID" = ?`)
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}})
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})
}

func TestQueryConstructionWithoutPrimaryKey(t *testing.T) {
	type relation struct {
		FooID int64 `db:"foo_id"`
		BarID int64 `db:"bar_id"`
	}
	opts := planOpts{
		TableName: "foo_bar_relations",
	}

	t.Run("MysqlDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[relation](), MysqlDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, "SELECT `foo_id`, `bar_id` FROM `foo_bar_relations` WHERE ")
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}})
		assert.Equal(t, plan.Insert.Query, "INSERT INTO `foo_bar_relations` (`foo_id`, `bar_id`) VALUES (?, ?)")
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, nil)
		assert.Equal(t, plan.Update.Query, "")
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, "")
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})

	t.Run("PostgresDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[relation](), PostgresDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, `SELECT "foo_id", "bar_id" FROM "foo_bar_relations" WHERE `)
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}})
		assert.Equal(t, plan.Insert.Query, `INSERT INTO "foo_bar_relations" ("foo_id", "bar_id") VALUES ($1, $2)`)
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, nil)
		assert.Equal(t, plan.Update.Query, "")
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, "")
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})

	t.Run("SqliteDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[relation](), SqliteDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, `SELECT "foo_id", "bar_id" FROM "foo_bar_relations" WHERE `)
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}})
		assert.Equal(t, plan.Insert.Query, `INSERT INTO "foo_bar_relations" ("foo_id", "bar_id") VALUES (?, ?)`)
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, nil)
		assert.Equal(t, plan.Update.Query, "")
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, "")
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})
}

func TestQueryConstructionImpossble(t *testing.T) {
	type unstructuredData struct {
		Foo int
		Bar *string
	}
	opts := planOpts{}

	testWith := func(dialect Dialect) func(*testing.T) {
		return func(t *testing.T) {
			plan, err := buildPlan(reflect.TypeFor[unstructuredData](), dialect, opts)
			if err != nil {
				t.Error(err)
			}

			assert.Equal(t, plan.Select.Query, "")
			assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
			assert.DeepEqual(t, plan.Select.ScanIndexes, nil)
			assert.Equal(t, plan.Insert.Query, "")
			assert.DeepEqual(t, plan.Insert.ArgumentIndexes, nil)
			assert.DeepEqual(t, plan.Insert.ScanIndexes, nil)
			assert.Equal(t, plan.Update.Query, "")
			assert.DeepEqual(t, plan.Update.ArgumentIndexes, nil)
			assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
			assert.Equal(t, plan.Delete.Query, "")
			assert.DeepEqual(t, plan.Delete.ArgumentIndexes, nil)
			assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
		}
	}

	t.Run("MysqlDialect", testWith(MysqlDialect()))
	t.Run("PostgresDialect", testWith(PostgresDialect()))
	t.Run("SqliteDialect", testWith(SqliteDialect()))
}

func TestQueryConstructionWithMultiplePrimaryKeyColumns(t *testing.T) {
	type record struct {
		GroupID   int64     `db:"group_id"`
		Name      string    `db:"name"`
		CreatedAt time.Time `db:"created_at"`
	}
	opts := planOpts{
		TableName:             "complex_records",
		PrimaryKeyColumnNames: []string{"group_id", "name"},
	}

	t.Run("MysqlDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[record](), MysqlDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, "SELECT `group_id`, `name`, `created_at` FROM `complex_records` WHERE ")
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}})
		assert.Equal(t, plan.Insert.Query, "INSERT INTO `complex_records` (`group_id`, `name`, `created_at`) VALUES (?, ?, ?)")
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}, {2}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, nil)
		assert.Equal(t, plan.Update.Query, "UPDATE `complex_records` SET `created_at` = ? WHERE `group_id` = ? AND `name` = ?")
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{2}, {0}, {1}})
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, "DELETE FROM `complex_records` WHERE `group_id` = ? AND `name` = ?")
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}, {1}})
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})

	t.Run("PostgresDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[record](), PostgresDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, `SELECT "group_id", "name", "created_at" FROM "complex_records" WHERE `)
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}})
		assert.Equal(t, plan.Insert.Query, `INSERT INTO "complex_records" ("group_id", "name", "created_at") VALUES ($1, $2, $3)`)
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}, {2}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, nil)
		assert.Equal(t, plan.Update.Query, `UPDATE "complex_records" SET "created_at" = $1 WHERE "group_id" = $2 AND "name" = $3`)
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{2}, {0}, {1}})
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, `DELETE FROM "complex_records" WHERE "group_id" = $1 AND "name" = $2`)
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}, {1}})
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})

	t.Run("SqliteDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[record](), SqliteDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, `SELECT "group_id", "name", "created_at" FROM "complex_records" WHERE `)
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}})
		assert.Equal(t, plan.Insert.Query, `INSERT INTO "complex_records" ("group_id", "name", "created_at") VALUES (?, ?, ?)`)
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}, {2}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, nil)
		assert.Equal(t, plan.Update.Query, `UPDATE "complex_records" SET "created_at" = ? WHERE "group_id" = ? AND "name" = ?`)
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{2}, {0}, {1}})
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, `DELETE FROM "complex_records" WHERE "group_id" = ? AND "name" = ?`)
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}, {1}})
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})
}

func TestQueryConstructionWithMultipleAutoColumns(t *testing.T) {
	type record struct {
		ID        int64     `db:"id,auto"`
		Name      string    `db:"name"`
		CreatedAt time.Time `db:"created_at,auto"`
	}
	opts := planOpts{
		TableName:             "autogenerated_records",
		PrimaryKeyColumnNames: []string{"id"},
	}

	t.Run("MysqlDialect", func(t *testing.T) {
		_, err := NewStore[record](MysqlDialect())
		assert.Equal(t, err.Error(), `cannot use type oblast.record for queries: multiple columns are marked as auto-filled (id, created_at), but this SQL dialect only supports at most one per table`)
	})

	t.Run("PostgresDialect", func(t *testing.T) {
		plan, err := buildPlan(reflect.TypeFor[record](), PostgresDialect(), opts)
		if err != nil {
			t.Error(err)
		}
		assert.Equal(t, plan.Select.Query, `SELECT "id", "name", "created_at" FROM "autogenerated_records" WHERE `)
		assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil)
		assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}})
		assert.Equal(t, plan.Insert.Query, `INSERT INTO "autogenerated_records" ("name") VALUES ($1) RETURNING "id", "created_at"`)
		assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{1}})
		assert.DeepEqual(t, plan.Insert.ScanIndexes, [][]int{{0}, {2}})
		assert.Equal(t, plan.Update.Query, `UPDATE "autogenerated_records" SET "name" = $1, "created_at" = $2 WHERE "id" = $3`)
		assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{1}, {2}, {0}})
		assert.DeepEqual(t, plan.Update.ScanIndexes, nil)
		assert.Equal(t, plan.Delete.Query, `DELETE FROM "autogenerated_records" WHERE "id" = $1`)
		assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}})
		assert.DeepEqual(t, plan.Delete.ScanIndexes, nil)
	})

	t.Run("SqliteDialect", func(t *testing.T) {
		_, err := NewStore[record](SqliteDialect())
		assert.Equal(t, err.Error(), `cannot use type oblast.record for queries: multiple columns are marked as auto-filled (id, created_at), but this SQL dialect only supports at most one per table`)
	})
}