-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsquashed_test.go
More file actions
156 lines (131 loc) · 3.31 KB
/
Copy pathsquashed_test.go
File metadata and controls
156 lines (131 loc) · 3.31 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
package hades_test
import (
"reflect"
"testing"
"crawshaw.io/sqlite"
"github.com/itchio/hades"
"github.com/itchio/hades/mtest"
"github.com/stretchr/testify/assert"
"xorm.io/builder"
)
func Test_SquashedToEq(t *testing.T) {
type BoneName string
type BoneTraits struct {
Name BoneName
Goodness int64
}
type Bone struct {
ID int64
Traits BoneTraits `hades:"squash"`
}
b := &Bone{
ID: 510,
Traits: BoneTraits{
Name: "cranium",
Goodness: 3,
},
}
c, err := hades.NewContext(&Bone{})
mtest.Must(t, err)
boneScope := c.NewScope(b)
eq := boneScope.ToEq(reflect.ValueOf(b))
// ToEq keys are SQL-ready (escaped) identifiers, not bare column names
assert.EqualValues(t, 3, len(eq))
assert.EqualValues(t, 510, eq[`"id"`])
assert.EqualValues(t, "cranium", eq[`"name"`])
assert.EqualValues(t, 3, eq[`"goodness"`])
}
func Test_SquashedInsert(t *testing.T) {
type BoneTraits struct {
Name string
Goodness int64
}
type Bone struct {
ID int64
Traits BoneTraits `hades:"squash"`
}
models := []any{&Bone{}}
withContext(t, models, func(conn *sqlite.Conn, c *hades.Context) {
b := &Bone{
ID: 128,
Traits: BoneTraits{
Name: "humerus",
Goodness: 98,
},
}
val := reflect.ValueOf(b)
scope := c.ScopeMap.ByType(val.Type())
mtest.Must(t, c.Insert(conn, scope, val))
bb := &Bone{}
mtest.Must(t, c.ExecRaw(conn, "SELECT * FROM bones", func(stmt *sqlite.Stmt) error {
bb.ID = stmt.ColumnInt64(0)
bb.Traits.Name = stmt.ColumnText(1)
bb.Traits.Goodness = stmt.ColumnInt64(2)
return nil
}))
assert.EqualValues(t, b, bb)
})
}
func Test_SquashedFull(t *testing.T) {
type FakeGameTraits struct {
Storied bool
Ubiquitous bool
}
type FakeGame struct {
ID int64
FakeUserID int64
Traits FakeGameTraits `hades:"squash"`
}
type FakeUser struct {
ID int64
Games []*FakeGame
}
models := []any{&FakeGame{}, &FakeUser{}}
withContext(t, models, func(conn *sqlite.Conn, c *hades.Context) {
fu := &FakeUser{
ID: 15,
Games: []*FakeGame{
&FakeGame{
ID: 2,
Traits: FakeGameTraits{
Storied: true,
},
},
&FakeGame{
ID: 4,
Traits: FakeGameTraits{
Ubiquitous: true,
},
},
&FakeGame{
ID: 6,
Traits: FakeGameTraits{
Storied: true,
Ubiquitous: true,
},
},
},
}
mtest.Must(t, c.Save(conn, fu, hades.Assoc("Games")))
u := &FakeUser{}
found, err := c.SelectOne(conn, u, builder.NewCond())
mtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, 15, u.ID)
mtest.Must(t, c.Preload(conn, u,
hades.AssocWithSearch("Games", hades.Search{}.OrderBy("id ASC").Offset(1)),
))
assert.EqualValues(t, 2, len(u.Games))
assert.EqualValues(t, FakeGameTraits{Ubiquitous: true}, u.Games[0].Traits)
assert.EqualValues(t, FakeGameTraits{Storied: true, Ubiquitous: true}, u.Games[1].Traits)
fu.Games[2].Traits.Storied = false
fu.Games[2].Traits.Ubiquitous = false
mtest.Must(t, c.Save(conn, fu, hades.Assoc("Games")))
mtest.Must(t, c.Preload(conn, u,
hades.AssocWithSearch("Games", hades.Search{}.OrderBy("id ASC").Offset(2).Limit(1)),
))
assert.EqualValues(t, 1, len(u.Games))
assert.EqualValues(t, 6, u.Games[0].ID)
assert.EqualValues(t, FakeGameTraits{Ubiquitous: false, Storied: false}, u.Games[0].Traits)
})
}