diff --git a/boltdb_test.go b/boltdb_test.go index 59637a5..0490526 100644 --- a/boltdb_test.go +++ b/boltdb_test.go @@ -8,7 +8,7 @@ import ( "testing" ) -func tempDB(t *testing.T) *bolt.DB { +func newBoltStore(t *testing.T) *bolt.DB { path := filepath.Join(t.TempDir(), "test.db") db, err := bolt.Open(path, 0600, nil) require.NoError(t, err) @@ -17,7 +17,7 @@ func tempDB(t *testing.T) *bolt.DB { } func TestSetupBoltDB(t *testing.T) { - db := tempDB(t) + db := newBoltStore(t) err := SetupBoltDB(db) assert.NoError(t, err) @@ -89,7 +89,7 @@ func TestBatchCheckEventsExist(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - db := tempDB(t) + db := newBoltStore(t) if !tc.skipSetup { err := SetupBoltDB(db) @@ -149,7 +149,7 @@ func TestBatchWriteEvents(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - db := tempDB(t) + db := newBoltStore(t) err := SetupBoltDB(db) require.NoError(t, err) @@ -171,7 +171,7 @@ func TestBatchWriteEvents(t *testing.T) { } func TestWriteThenCheck(t *testing.T) { - db := tempDB(t) + db := newBoltStore(t) err := SetupBoltDB(db) require.NoError(t, err) diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..d7eb990 --- /dev/null +++ b/main_test.go @@ -0,0 +1,73 @@ +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) +} diff --git a/write_test.go b/write_test.go index 4b0735d..d0a67aa 100644 --- a/write_test.go +++ b/write_test.go @@ -10,7 +10,7 @@ import ( // Pipeline stage tests func TestEnforcePolicyRules(t *testing.T) { - db := tempDB(t) + db := newBoltStore(t) require.NoError(t, SetupBoltDB(db)) fx := LoadFixtures(t)