forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqltest.go
92 lines (80 loc) · 2.04 KB
/
sqltest.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
package sqltest
import (
"testing"
"unicode/utf8"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/graphtest"
"github.com/cayleygraph/cayley/graph/graphtest/testutil"
"github.com/cayleygraph/cayley/graph/sql"
"github.com/cayleygraph/cayley/quad"
"github.com/stretchr/testify/require"
)
type Config struct {
TimeRound bool
}
func (c Config) quadStore() *graphtest.Config {
return &graphtest.Config{
NoPrimitives: true,
TimeInMcs: true,
TimeRound: c.TimeRound,
OptimizesComparison: true,
}
}
func TestAll(t *testing.T, typ string, fnc DatabaseFunc, c *Config) {
if c == nil {
c = &Config{}
}
create := makeDatabaseFunc(typ, fnc)
t.Run("qs", func(t *testing.T) {
t.Parallel()
graphtest.TestAll(t, create, c.quadStore())
})
t.Run("zero rune", func(t *testing.T) {
t.Parallel()
testZeroRune(t, create)
})
}
func BenchmarkAll(t *testing.B, typ string, fnc DatabaseFunc, c *Config) {
if c == nil {
c = &Config{}
}
create := makeDatabaseFunc(typ, fnc)
t.Run("qs", func(t *testing.B) {
graphtest.BenchmarkAll(t, create, c.quadStore())
})
}
type DatabaseFunc func(t testing.TB) (string, graph.Options, func())
func makeDatabaseFunc(typ string, create DatabaseFunc) testutil.DatabaseFunc {
return func(t testing.TB) (graph.QuadStore, graph.Options, func()) {
addr, opts, closer := create(t)
if err := sql.Init(typ, addr, opts); err != nil {
closer()
t.Fatal(err)
}
qs, err := sql.New(typ, addr, opts)
if err != nil {
closer()
t.Fatal(err)
}
return qs, nil, func() {
qs.Close()
closer()
}
}
}
func testZeroRune(t testing.TB, create testutil.DatabaseFunc) {
qs, opts, closer := create(t)
defer closer()
w := testutil.MakeWriter(t, qs, opts)
obj := quad.String("AB\u0000CD")
if !utf8.ValidString(string(obj)) {
t.Fatal("invalid utf8")
}
err := w.AddQuad(quad.Quad{
Subject: quad.IRI("bob"),
Predicate: quad.IRI("pred"),
Object: obj,
})
require.NoError(t, err)
require.Equal(t, obj, qs.NameOf(qs.ValueOf(quad.Raw(obj.String()))))
}