aboutsummaryrefslogtreecommitdiff
path: root/runtimeindex_test.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-05-08 22:56:18 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-05-08 22:56:18 +0200
commit5d655f04f8ab0dfa018430620caa2f56fcd9450a (patch)
treea34c94bafbf31cca3751eb826751e3d80103df99 /runtimeindex_test.go
parent9b456c354ec23ae85f21054e1326683ebccca86a (diff)
downloadgo-oblast-5d655f04f8ab0dfa018430620caa2f56fcd9450a.tar.gz
add type RuntimeIndex
Diffstat (limited to 'runtimeindex_test.go')
-rw-r--r--runtimeindex_test.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/runtimeindex_test.go b/runtimeindex_test.go
new file mode 100644
index 0000000..ba16fd9
--- /dev/null
+++ b/runtimeindex_test.go
@@ -0,0 +1,72 @@
+// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
+// SPDX-License-Identifier: Apache-2.0
+
+package oblast_test
+
+import (
+ "database/sql"
+ "testing"
+
+ "go.xyrillian.de/oblast"
+ "go.xyrillian.de/oblast/internal/testhelpers/assert"
+ "go.xyrillian.de/oblast/internal/testhelpers/mock"
+ "go.xyrillian.de/oblast/internal/testhelpers/must"
+)
+
+func TestRuntimeIndex(t *testing.T) {
+ ctx := t.Context()
+ md := mock.NewDriver()
+ db := sql.OpenDB(md)
+
+ type basicRecord struct {
+ ID int64 `db:"id"`
+ Name string `db:"name"`
+ }
+ store := oblast.MustNewStore[basicRecord](
+ oblast.SqliteDialect(),
+ oblast.TableNameIs("basic_records"),
+ oblast.PrimaryKeyIs("id"),
+ )
+
+ commonSetup := func() {
+ md.ForQuery(`SELECT "id", "name" FROM "basic_records" WHERE id > 0`).
+ ExpectQueryWithArgs().
+ AndReturnColumns("id", "name").
+ WithRow(1, "foo").
+ WithRow(2, "bar").
+ WithRow(3, "baz")
+ }
+
+ t.Run("Index", func(t *testing.T) {
+ byName := oblast.NewRuntimeIndex(func(r basicRecord) string { return r.Name })
+
+ // test success path
+ commonSetup()
+ result := must.Return(byName.IndexFrom(store.SelectWhere(ctx, db, `id > 0`)))(t)
+ assert.DeepEqual(t, result, map[string]basicRecord{
+ "foo": {1, "foo"},
+ "bar": {2, "bar"},
+ "baz": {3, "baz"},
+ })
+
+ // test error path
+ _, err := byName.IndexFrom(store.SelectWhere(ctx, db, `id = 1`))
+ assert.ErrEqual(t, err, `during Query(): unexpected query: SELECT "id", "name" FROM "basic_records" WHERE id = 1`)
+ })
+
+ t.Run("Partition", func(t *testing.T) {
+ byFirstLetter := oblast.NewRuntimeIndex(func(r basicRecord) string { return r.Name[0:1] })
+
+ // test success path
+ commonSetup()
+ result := must.Return(byFirstLetter.PartitionFrom(store.SelectWhere(ctx, db, `id > 0`)))(t)
+ assert.DeepEqual(t, result, map[string][]basicRecord{
+ "f": {{1, "foo"}},
+ "b": {{2, "bar"}, {3, "baz"}},
+ })
+
+ // test error path
+ _, err := byFirstLetter.PartitionFrom(store.SelectWhere(ctx, db, `id = 1`))
+ assert.ErrEqual(t, err, `during Query(): unexpected query: SELECT "id", "name" FROM "basic_records" WHERE id = 1`)
+ })
+}