Files
go-heartwood/main_test.go
T

74 lines
1.5 KiB
Go

package heartwood
import (
"context"
"github.com/boltdb/bolt"
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
"github.com/stretchr/testify/require"
"os"
"testing"
)
var testDriver neo4j.Driver
func TestMain(m *testing.M) {
// connect neo4j driver
if uri := os.Getenv("NEO4J_URI"); uri != "" {
d, err := neo4j.NewDriver(uri, neo4j.BasicAuth(
os.Getenv("NEO4J_USER"), os.Getenv("NEO4J_PASS"), "",
))
if err != nil {
panic(err)
}
testDriver = d
if err = d.VerifyConnectivity(context.Background()); err != nil {
panic(err)
}
if err := SetNeo4jSchema(context.Background(), d); err != nil {
panic(err)
}
}
// run tests
code := m.Run()
// disconnect neo4j driver
if testDriver != nil {
testDriver.Close(context.Background())
}
os.Exit(code)
}
type stores struct {
t *testing.T
graph neo4j.Driver
bolt *bolt.DB
}
// freshStores is the per-test fixture. It provides a cleared graph and fresh
// boltdb file. Skips when neo4j is unavailable.
func freshStores(t *testing.T) *stores {
t.Helper()
if testDriver == nil {
t.Skip("neo4j is unavailable")
}
wipeGraph(t)
b := newBoltStore(t)
require.NoError(t, SetupBoltDB(b))
return &stores{t: t, graph: testDriver, bolt: b}
}
// wipeGraph clears the neo4j graph provided by the test driver.
func wipeGraph(t *testing.T) {
t.Helper()
_, err := neo4j.ExecuteQuery(
t.Context(), testDriver,
"MATCH (n) DETACH DELETE n",
nil, neo4j.EagerResultTransformer,
)
require.NoError(t, err)
}