-
Notifications
You must be signed in to change notification settings - Fork 1
/
select_test.go
96 lines (78 loc) · 2.17 KB
/
select_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
package surrealhigh
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TODO move to a sub test package
type mockDriver struct{ update *bool }
func newMockDriver() mockDriver {
var b bool
return mockDriver{&b}
}
func (driver mockDriver) Driver() SurrealDB {
return mockDriverResult{update: driver.update}
}
type mockDriverResult struct{ update *bool }
func (mock mockDriverResult) Query(sql string, vars interface{}) (interface{}, error) {
// [
// > {
// > "result": [ {...}, ... ],
// > "status": "OK",
// > "time": "337.295µs"
// > }
// > ]
var results []interface{}
results = append(results, struct {
Result []mockDoc `json:"result"`
Status string `json:"status"`
}{
Result: []mockDoc{
{RecordID: 0, IsOut: *mock.update},
},
Status: "OK",
})
return results, nil
}
func (mock mockDriverResult) Update(what string, data interface{}) (interface{}, error) {
*mock.update = true
return nil, nil
}
type mockDoc struct {
RecordID int
IsOut bool
}
func (doc mockDoc) Table() Table { return "" }
func (doc mockDoc) Id() Thing { return "" }
func TestDBSelect_Do(t *testing.T) {
t.Run("mock driver result no error", func(t *testing.T) {
results, err := SelectOn[mockDoc](NewQueryFrom(Table("")), newMockDriver()).Do()
require.NoError(t, err)
assert.Equal(t, []mockDoc{
{RecordID: 0, IsOut: false},
}, results)
})
}
func TestDBSelectAndUpdate_Do(t *testing.T) {
t.Run("mock driver update no error", func(t *testing.T) {
db := newMockDriver()
_, err := SelectAndUpdate(NewQueryFrom(Table("")), func(mockDoc) mockDoc { return mockDoc{} }, db).Do()
require.NoError(t, err)
docs, err := SelectOn[mockDoc](NewQueryFrom(Table("")), db).Do()
require.NoError(t, err)
assert.Equal(t, []mockDoc{
{RecordID: 0, IsOut: true},
}, docs)
})
}
func (mock mockDriverResult) Create(thing string, data interface{}) (interface{}, error) {
tb, th := Table("mock"), Thing("mock:00000000_0000_0000_0000_000000000000")
id, err := NewIDFromThing(th, tb)
if err != nil {
return nil, fmt.Errorf("new id from thing: %w", err)
}
return struct {
Id string `json:"id"`
}{id.String()}, nil
}