-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlt_test.go
122 lines (100 loc) · 2.76 KB
/
sqlt_test.go
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
package sqlt_test
import (
"context"
"database/sql"
"encoding/csv"
"os"
"testing"
"github.com/Masterminds/sprig/v3"
"github.com/wroge/sqlt"
_ "modernc.org/sqlite"
)
type Pokemon struct {
Number int `json:"number"`
Name string `json:"name"`
Height float64 `json:"height"`
Weight float64 `json:"weight"`
Generation int `json:"generation"`
Legendary bool `json:"legendary"`
Types []string `json:"types"`
Classification string `json:"classification"`
Abilities []string `json:"abilities"`
}
func NewPointer[T any](t T) Pointer[T] {
return &t
}
type Pointer[T any] *T
type Query struct {
HeightRange Pointer[[2]float64]
WeightRange Pointer[[2]float64]
Generation Pointer[int]
Legendary Pointer[bool]
TypeOneOf Pointer[[]string]
Classification Pointer[string]
AbilityOneOf Pointer[[]string]
}
var (
config = sqlt.Config{
Placeholder: sqlt.Question,
Cache: &sqlt.Cache{},
Templates: []sqlt.Template{
sqlt.Funcs(sprig.TxtFuncMap()),
sqlt.ParseFiles("./testdata/queries.sql"),
},
}
create = sqlt.Exec[any](config, sqlt.Lookup("create"))
insert = sqlt.Transaction(
nil,
sqlt.Exec[[][]string](config, sqlt.Lookup("insert_types")),
sqlt.Exec[[][]string](config, sqlt.Lookup("insert_classifications")),
sqlt.Exec[[][]string](config, sqlt.Lookup("insert_abilities")),
sqlt.Exec[[][]string](config, sqlt.Lookup("insert_pokemons")),
sqlt.Exec[[][]string](config, sqlt.Lookup("insert_pokemon_types")),
sqlt.Exec[[][]string](config, sqlt.Lookup("insert_pokemon_classifications")),
sqlt.Exec[[][]string](config, sqlt.Lookup("insert_pokemon_abilities")),
)
query = sqlt.All[Query, Pokemon](config, sqlt.Lookup("query"))
queryFirst = sqlt.First[Query, Pokemon](config, sqlt.Lookup("query"))
)
func TestQueryPokemon(t *testing.T) {
db, err := sql.Open("sqlite", "file:pokemon.db?mode=memory")
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
file, err := os.Open("./testdata/pokemon_data_pokeapi.csv")
if err != nil {
t.Fatal(err)
}
records, err := csv.NewReader(file).ReadAll()
if err != nil {
t.Fatal(err)
}
_, err = create.Exec(ctx, db, nil)
if err != nil {
t.Fatal(err)
}
_, err = insert.Exec(ctx, db, records[1:])
if err != nil {
t.Fatal(err)
}
pokemons, err := query.Exec(ctx, db, Query{
TypeOneOf: NewPointer([]string{"Dragon"}),
Generation: NewPointer(1),
})
if err != nil {
t.Fatal(err)
}
if len(pokemons) != 3 {
t.Errorf("Expected 3 Pokémon, got %d", len(pokemons))
}
rattata, err := queryFirst.Exec(ctx, db, Query{
Classification: NewPointer("Mouse Pokémon"),
})
if err != nil {
t.Fatal(err)
}
if rattata.Name != "Rattata" {
t.Errorf("Expected Rattata, got %s", rattata.Name)
}
}