-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_test.go
More file actions
101 lines (92 loc) · 2.18 KB
/
Copy pathsqlite_test.go
File metadata and controls
101 lines (92 loc) · 2.18 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
package sqlite
import (
"reflect"
"testing"
"github.com/dpup/prefab/plugins/storage"
"github.com/dpup/prefab/plugins/storage/storagetests"
)
func TestSqliteStore(t *testing.T) {
storagetests.Run(t, func() storage.Store {
return New(":memory:")
})
}
func TestSqliteStore_withPrefixAndDedicatedTable(t *testing.T) {
storagetests.Run(t, func() storage.Store {
s := New(":memory:", WithPrefix("prefix_")).(*store)
err := s.InitModel(storagetests.Fruit{})
if err != nil {
t.Fatal(err)
}
return s
})
}
type Vehicle struct {
ID string
Type string
Wheels int
Mods *string
}
func (v Vehicle) PK() string {
return v.ID
}
type Animal struct {
ID string
Type string
Legs int
}
func (v Animal) PK() string {
return v.ID
}
func TestBuildListQuery(t *testing.T) {
emptyString := ""
tests := []struct {
name string
filter storage.Model
query string
params []any
}{
{
"empty",
Vehicle{},
"SELECT value FROM custom_default WHERE entity_type = ?",
[]any{"vehicles"},
},
{
"single field filter",
Vehicle{Type: "car"},
"SELECT value FROM custom_default WHERE entity_type = ? AND json_extract(value, '$.Type') = ?",
[]any{"vehicles", "car"},
},
{
"two field filter",
Vehicle{Type: "car", Wheels: 4},
"SELECT value FROM custom_default WHERE entity_type = ? AND json_extract(value, '$.Type') = ? AND json_extract(value, '$.Wheels') = ?",
[]any{"vehicles", "car", 4},
},
{
"zero pointer filter",
Vehicle{Mods: &emptyString},
"SELECT value FROM custom_default WHERE entity_type = ? AND json_extract(value, '$.Mods') = ?",
[]any{"vehicles", &emptyString},
},
{
"dedicated table",
Animal{Legs: 3},
"SELECT value FROM custom_animals WHERE json_extract(value, '$.Legs') = ?",
[]any{3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := New(":memory:", WithPrefix("custom_")).(*store)
s.InitModel(Animal{})
query, params := s.buildListQuery(tt.filter)
if query != tt.query {
t.Errorf("buildListQuery() query = %v, want %v", query, tt.query)
}
if !reflect.DeepEqual(params, tt.params) {
t.Errorf("buildListQuery() params = %v, want %v", params, tt.params)
}
})
}
}