-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscan_test.go
More file actions
143 lines (126 loc) · 2.86 KB
/
Copy pathscan_test.go
File metadata and controls
143 lines (126 loc) · 2.86 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
package hades_test
import (
"testing"
"crawshaw.io/sqlite"
"github.com/itchio/hades"
"github.com/itchio/hades/mtest"
"github.com/stretchr/testify/assert"
"xorm.io/builder"
)
func Test_Scan(t *testing.T) {
type GameEmbedData struct {
GameID int64 `hades:"primary_key"`
Width int64
Height int64
}
type Game struct {
ID int64
Title string
EmbedData *GameEmbedData
}
models := []any{
&Game{},
&GameEmbedData{},
}
withContext(t, models, func(conn *sqlite.Conn, c *hades.Context) {
mtest.Must(t, c.Save(conn, []*Game{
&Game{
ID: 24,
Title: "Jazz Jackrabbit",
EmbedData: &GameEmbedData{
Width: 640,
Height: 480,
},
},
&Game{
ID: 46,
Title: "Duke Nukem 2",
EmbedData: &GameEmbedData{
Width: 320,
Height: 240,
},
},
}, hades.Assoc("EmbedData")))
var rows []struct {
Game `hades:"squash"`
GameEmbedData `hades:"squash"`
}
mtest.Must(t, c.ExecWithSearch(conn,
builder.Select("games.*", "game_embed_data.*").
From("games").
LeftJoin("game_embed_data", builder.Expr("game_embed_data.game_id = games.id")),
hades.Search{}.OrderBy("games.id ASC"),
c.IntoRowsScanner(&rows),
))
assert.EqualValues(t, 2, len(rows))
assert.EqualValues(t, "Jazz Jackrabbit", rows[0].Game.Title)
assert.EqualValues(t, 640, rows[0].GameEmbedData.Width)
assert.EqualValues(t, 480, rows[0].GameEmbedData.Height)
assert.EqualValues(t, "Duke Nukem 2", rows[1].Game.Title)
assert.EqualValues(t, 320, rows[1].GameEmbedData.Width)
assert.EqualValues(t, 240, rows[1].GameEmbedData.Height)
})
}
// every field type automigrate accepts must survive a save/scan round trip
func Test_ScanTypeMatrix(t *testing.T) {
type Label string
type Gizmo struct {
ID int64
U uint
U8 uint8
U32 uint32
U64 uint64
I32 int32
F32 float32
L Label
PI *int
PI32 *int32
PI64 *int64
PU *uint
PF32 *float32
PB *bool
PS *string
PL *Label
}
pi := int(-4)
pi32 := int32(-32)
pi64 := int64(1 << 40)
pu := uint(7)
pf32 := float32(2.5)
pb := true
ps := "hello"
pl := Label("tag")
withContext(t, []any{&Gizmo{}}, func(conn *sqlite.Conn, c *hades.Context) {
in := &Gizmo{
ID: 1,
U: 3,
U8: 200,
U32: 1 << 30,
U64: 1 << 40,
I32: -1234,
F32: 1.5,
L: "named",
PI: &pi,
PI32: &pi32,
PI64: &pi64,
PU: &pu,
PF32: &pf32,
PB: &pb,
PS: &ps,
PL: &pl,
}
mtest.Must(t, c.Save(conn, in))
var out Gizmo
found, err := c.SelectOne(conn, &out, builder.Eq{"id": 1})
mtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, in, &out)
// nil pointers round-trip as NULL
mtest.Must(t, c.Save(conn, &Gizmo{ID: 2}))
var out2 Gizmo
found, err = c.SelectOne(conn, &out2, builder.Eq{"id": 2})
mtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, &Gizmo{ID: 2}, &out2)
})
}