-
Notifications
You must be signed in to change notification settings - Fork 111
/
testruntime.go
106 lines (86 loc) · 3.03 KB
/
testruntime.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
package testruntime
import (
"context"
"fmt"
"path/filepath"
goruntime "runtime"
"strings"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/drivers"
"github.com/stretchr/testify/require"
// Load database drivers for testing.
_ "github.com/rilldata/rill/runtime/drivers/duckdb"
_ "github.com/rilldata/rill/runtime/drivers/file"
_ "github.com/rilldata/rill/runtime/drivers/sqlite"
)
// TestingT satisfies both *testing.T and *testing.B.
type TestingT interface {
Name() string
TempDir() string
FailNow()
Errorf(format string, args ...interface{})
}
// New returns a runtime configured for use in tests.
func New(t TestingT) *runtime.Runtime {
opts := &runtime.Options{
ConnectionCacheSize: 100,
MetastoreDriver: "sqlite",
// Setting a test-specific name ensures a unique connection when "cache=shared" is enabled.
// "cache=shared" is needed to prevent threading problems.
MetastoreDSN: fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name()),
QueryCacheSize: 10000,
AllowHostAccess: true,
}
rt, err := runtime.New(opts, nil)
require.NoError(t, err)
return rt
}
// NewInstance creates a runtime and an instance for use in tests.
// The instance's repo is a temp directory that will be cleared when the tests finish.
func NewInstance(t TestingT) (*runtime.Runtime, string) {
rt := New(t)
inst := &drivers.Instance{
OLAPDriver: "duckdb",
OLAPDSN: "",
RepoDriver: "file",
RepoDSN: t.TempDir(),
EmbedCatalog: true,
}
err := rt.CreateInstance(context.Background(), inst)
require.NoError(t, err)
require.NotEmpty(t, inst.ID)
return rt, inst.ID
}
// NewInstanceWithModel creates a runtime and an instance for use in tests.
// The passed model name and SQL SELECT statement will be loaded into the instance.
func NewInstanceWithModel(t TestingT, name, sql string) (*runtime.Runtime, string) {
rt, instanceID := NewInstance(t)
path := filepath.Join("models", name+".sql")
err := rt.PutFile(context.Background(), instanceID, path, strings.NewReader(sql), true, false)
require.NoError(t, err)
res, err := rt.Reconcile(context.Background(), instanceID, nil, nil, false, false)
require.NoError(t, err)
require.Empty(t, res.Errors)
return rt, instanceID
}
// NewInstanceForProject creates a runtime and an instance for use in tests.
// The passed name should match a test project in the testdata folder.
// You should not do mutable repo operations on the returned instance.
func NewInstanceForProject(t TestingT, name string) (*runtime.Runtime, string) {
rt := New(t)
_, currentFile, _, _ := goruntime.Caller(0)
inst := &drivers.Instance{
OLAPDriver: "duckdb",
OLAPDSN: "?access_mode=read_write",
RepoDriver: "file",
RepoDSN: filepath.Join(currentFile, "..", "testdata", name),
EmbedCatalog: true,
}
err := rt.CreateInstance(context.Background(), inst)
require.NoError(t, err)
require.NotEmpty(t, inst.ID)
res, err := rt.Reconcile(context.Background(), inst.ID, nil, nil, false, false)
require.NoError(t, err)
require.Empty(t, res.Errors)
return rt, inst.ID
}