This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from orus-io/yago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils_test.go
71 lines (60 loc) · 1.69 KB
/
testutils_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
package yago_test
import (
"os"
"testing"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/orus-io/yago"
"github.com/slicebit/qb"
_ "github.com/slicebit/qb/dialects/postgres"
_ "github.com/slicebit/qb/dialects/sqlite"
"github.com/stretchr/testify/assert"
)
func asSQL(query yago.Query) string {
sql, _ := asSQLBind(query)
return sql
}
func asSQLBind(query yago.Query) (string, []interface{}) {
dialect := qb.NewDialect("default")
s := query.SelectStmt()
ctx := qb.NewCompilerContext(dialect)
return s.Accept(ctx), ctx.Binds
}
func initModel(t *testing.T) (db *yago.DB, model FixtureModel, cleanup func()) {
return initModelWithDriver(t, "sqlite3")
}
func initModelWithDriver(t *testing.T, driver string) (db *yago.DB, model FixtureModel, cleanup func()) {
var dsn string
switch driver {
case "postgres":
dsn = os.Getenv("YAGO_TEST_POSTGRES")
case "sqlite3":
dsn = os.Getenv("YAGO_TEST_SQLITE")
if dsn == "" {
dsn = ":memory:"
}
default:
panic("Unknown driver for tests: " + driver)
}
engine, err := qb.New(driver, dsn)
assert.Nil(t, err)
meta := yago.NewMetadata()
model = NewFixtureModel(meta)
db = yago.New(meta, engine)
CleanupDB(t, db, false)
assert.Nil(t, meta.GetQbMetadata().CreateAll(engine))
cleanup = func() { CleanupFunc(t, db, true) }
return
}
func CleanupFunc(t *testing.T, db *yago.DB, reportErrors bool) {
CleanupDB(t, db, reportErrors)
db.Close()
}
func CleanupDB(t *testing.T, db *yago.DB, reportErrors bool) {
for _, table := range db.Metadata.GetQbMetadata().Tables() {
_, err := db.Engine.DB().Exec("DROP TABLE " + table.Name)
if err != nil && reportErrors {
t.Errorf("Could not drop table '%s': %s", table.Name, err)
}
}
}