-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_map_get_test.go
More file actions
180 lines (159 loc) · 4.67 KB
/
Copy pathsql_map_get_test.go
File metadata and controls
180 lines (159 loc) · 4.67 KB
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
package dalgo2sql
import (
"context"
"database/sql"
"fmt"
"testing"
"github.com/dal-go/dalgo/dal"
_ "modernc.org/sqlite"
)
// openTestSQLiteDB opens an in-memory SQLite database using modernc.org/sqlite
// (pure Go, no CGo) and creates the given table schema. It returns the *sql.DB
// and a cleanup function.
func openTestSQLiteDB(t *testing.T, createSQL string) *sql.DB {
t.Helper()
// Each test gets an isolated in-memory database via a unique URI.
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
db, err := sql.Open("sqlite", dsn)
if err != nil {
t.Fatalf("sql.Open: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
if _, err := db.Exec(createSQL); err != nil {
t.Fatalf("CREATE TABLE: %v", err)
}
return db
}
func TestMapDataGetRoundTrip(t *testing.T) {
ctx := context.Background()
opts := DbOptions{
Recordsets: map[string]*Recordset{
"widgets": NewRecordset("widgets", Table, []dal.FieldRef{dal.Field("id")}),
},
}
createSQL := `CREATE TABLE widgets (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
price TEXT NOT NULL
)`
newDB := func(t *testing.T) *database {
t.Helper()
sqlDB := openTestSQLiteDB(t, createSQL)
return NewDatabase(sqlDB, newSchema(), opts).(*database)
}
t.Run("Insert_then_Get_map", func(t *testing.T) {
db := newDB(t)
// Insert via map[string]any.
rec := dal.NewRecordWithData(
dal.NewKeyWithID("widgets", "w1"),
map[string]any{"name": "Sprocket", "price": "9.99"},
)
if err := db.Insert(ctx, rec); err != nil {
t.Fatalf("Insert: %v", err)
}
// Get back into a new map.
got := make(map[string]any)
getRec := dal.NewRecordWithData(dal.NewKeyWithID("widgets", "w1"), got)
if err := db.Get(ctx, getRec); err != nil {
t.Fatalf("Get: %v", err)
}
if got["name"] != "Sprocket" {
t.Errorf("name = %v, want Sprocket", got["name"])
}
if got["price"] != "9.99" {
t.Errorf("price = %v, want 9.99", got["price"])
}
// PK column is included in SELECT *, so it should be present in the map.
if got["id"] != "w1" {
t.Errorf("id = %v, want w1", got["id"])
}
})
t.Run("Get_missing_returns_IsNotFound", func(t *testing.T) {
db := newDB(t)
got := make(map[string]any)
getRec := dal.NewRecordWithData(dal.NewKeyWithID("widgets", "missing"), got)
err := db.Get(ctx, getRec)
if err == nil {
t.Fatal("expected error for missing record, got nil")
}
if !dal.IsNotFound(err) {
t.Errorf("expected IsNotFound error, got: %v", err)
}
})
t.Run("Set_upsert_map_then_Get", func(t *testing.T) {
db := newDB(t)
// Set (insert new).
rec := dal.NewRecordWithData(
dal.NewKeyWithID("widgets", "w2"),
map[string]any{"name": "Bolt", "price": "1.50"},
)
if err := db.Set(ctx, rec); err != nil {
t.Fatalf("Set (insert): %v", err)
}
// Get to verify.
got := make(map[string]any)
getRec := dal.NewRecordWithData(dal.NewKeyWithID("widgets", "w2"), got)
if err := db.Get(ctx, getRec); err != nil {
t.Fatalf("Get after Set (insert): %v", err)
}
if got["name"] != "Bolt" {
t.Errorf("name = %v, want Bolt", got["name"])
}
// Set (update existing).
updRec := dal.NewRecordWithData(
dal.NewKeyWithID("widgets", "w2"),
map[string]any{"name": "Big Bolt", "price": "3.00"},
)
if err := db.Set(ctx, updRec); err != nil {
t.Fatalf("Set (update): %v", err)
}
// Get again to verify update.
got2 := make(map[string]any)
getRec2 := dal.NewRecordWithData(dal.NewKeyWithID("widgets", "w2"), got2)
if err := db.Get(ctx, getRec2); err != nil {
t.Fatalf("Get after Set (update): %v", err)
}
if got2["name"] != "Big Bolt" {
t.Errorf("name after update = %v, want Big Bolt", got2["name"])
}
if got2["price"] != "3.00" {
t.Errorf("price after update = %v, want 3.00", got2["price"])
}
})
t.Run("GetMulti_map_data", func(t *testing.T) {
db := newDB(t)
// Insert two records.
for _, d := range []struct {
id string
name string
price string
}{
{"m1", "WidgetA", "10.00"},
{"m2", "WidgetB", "20.00"},
} {
r := dal.NewRecordWithData(
dal.NewKeyWithID("widgets", d.id),
map[string]any{"name": d.name, "price": d.price},
)
if err := db.Insert(ctx, r); err != nil {
t.Fatalf("Insert %s: %v", d.id, err)
}
}
// GetMulti both back.
m1 := make(map[string]any)
m2 := make(map[string]any)
records := []dal.Record{
dal.NewRecordWithData(dal.NewKeyWithID("widgets", "m1"), m1),
dal.NewRecordWithData(dal.NewKeyWithID("widgets", "m2"), m2),
}
if err := db.GetMulti(ctx, records); err != nil {
t.Fatalf("GetMulti: %v", err)
}
if m1["name"] != "WidgetA" {
t.Errorf("m1 name = %v, want WidgetA", m1["name"])
}
if m2["name"] != "WidgetB" {
t.Errorf("m2 name = %v, want WidgetB", m2["name"])
}
})
}