-
-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic support for Neo4j container
Fixes #921
- Loading branch information
Showing
5 changed files
with
178 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package neo4j | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Option func(*config) | ||
|
||
type config struct { | ||
imageCoordinates string | ||
adminPassword string | ||
labsPlugins []string | ||
} | ||
|
||
type LabsPlugin string | ||
|
||
const ( | ||
Apoc LabsPlugin = "apoc" | ||
ApocCore LabsPlugin = "apoc-core" | ||
Bloom LabsPlugin = "bloom" | ||
GraphDataScience LabsPlugin = "graph-data-science" | ||
NeoSemantics LabsPlugin = "n10s" | ||
Streams LabsPlugin = "streams" | ||
) | ||
|
||
// WithoutAuthentication disables authentication. | ||
func WithoutAuthentication() Option { | ||
return WithAdminPassword("") | ||
} | ||
|
||
// WithAdminPassword sets the admin password for the default account | ||
// An empty string disables authentication. | ||
// The default password is "password". | ||
func WithAdminPassword(adminPassword string) Option { | ||
return func(c *config) { | ||
c.adminPassword = adminPassword | ||
} | ||
} | ||
|
||
// WithImageCoordinates sets the image coordinates of the Neo4j container. | ||
func WithImageCoordinates(imageCoordinates string) Option { | ||
return func(c *config) { | ||
c.imageCoordinates = imageCoordinates | ||
} | ||
} | ||
|
||
// WithLabsPlugin registers one or more Neo4jLabsPlugin for download and server startup. | ||
// There might be plugins not supported by your selected version of Neo4j. | ||
func WithLabsPlugin(plugins ...LabsPlugin) Option { | ||
return func(c *config) { | ||
rawPluginValues := make([]string, len(plugins)) | ||
for i := 0; i < len(plugins); i++ { | ||
rawPluginValues[i] = string(plugins[i]) | ||
} | ||
c.labsPlugins = rawPluginValues | ||
} | ||
} | ||
|
||
func (c config) exportEnv() map[string]string { | ||
env := make(map[string]string) | ||
env["NEO4J_AUTH"] = c.authEnvVar() | ||
if len(c.labsPlugins) > 0 { | ||
env["NEO4JLABS_PLUGINS"] = c.labsPluginsEnvVar() | ||
} | ||
return env | ||
} | ||
|
||
func (c config) authEnvVar() string { | ||
if c.adminPassword == "" { | ||
return "none" | ||
} | ||
return fmt.Sprintf("neo4j/%s", c.adminPassword) | ||
} | ||
|
||
func (c config) labsPluginsEnvVar() string { | ||
return fmt.Sprintf(`["%s"]`, strings.Join(c.labsPlugins, `","`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,75 @@ | ||
package neo4j | ||
package neo4j_test | ||
|
||
import ( | ||
"context" | ||
neo "github.com/neo4j/neo4j-go-driver/v5/neo4j" | ||
"github.com/testcontainers/testcontainers-go/modules/neo4j" | ||
"testing" | ||
) | ||
|
||
func TestNeo4j(t *testing.T) { | ||
const testPassword = "letmein!" | ||
|
||
func TestNeo4j(outer *testing.T) { | ||
ctx := context.Background() | ||
|
||
container, err := setupNeo4j(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
outer.Fatal(err) | ||
} | ||
|
||
// Clean up the container after the test is complete | ||
t.Cleanup(func() { | ||
outer.Cleanup(func() { | ||
if err := container.Terminate(ctx); err != nil { | ||
t.Fatalf("failed to terminate container: %s", err) | ||
outer.Fatalf("failed to terminate container: %s", err) | ||
} | ||
}) | ||
|
||
outer.Run("connects via Bolt", func(t *testing.T) { | ||
driver := createDriver(t, ctx, container) | ||
|
||
err = driver.VerifyConnectivity(ctx) | ||
|
||
if err != nil { | ||
t.Fatalf("should have successfully connected to server but did not: %s", err) | ||
} | ||
}) | ||
|
||
outer.Run("exercises APOC plugin", func(t *testing.T) { | ||
driver := createDriver(t, ctx, container) | ||
|
||
result, err := neo.ExecuteQuery(ctx, driver, | ||
"RETURN apoc.number.arabicToRoman(1986) AS output", nil, | ||
neo.EagerResultTransformer) | ||
|
||
if err != nil { | ||
t.Fatalf("expected APOC query to successfully run but did not: %s", err) | ||
} | ||
if value, _ := result.Records[0].Get("output"); value != "MCMLXXXVI" { | ||
t.Fatalf("did not get expected roman number: %s", value) | ||
} | ||
}) | ||
|
||
// perform assertions | ||
} | ||
|
||
func setupNeo4j(ctx context.Context) (*neo4j.Neo4jContainer, error) { | ||
return neo4j.StartContainer(ctx, | ||
neo4j.WithAdminPassword(testPassword), | ||
neo4j.WithLabsPlugin(neo4j.Apoc), | ||
) | ||
} | ||
|
||
func createDriver(t *testing.T, ctx context.Context, container *neo4j.Neo4jContainer) neo.DriverWithContext { | ||
boltUrl, err := container.BoltUrl(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
driver, err := neo.NewDriverWithContext(boltUrl, neo.BasicAuth("neo4j", testPassword, "")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { | ||
if err := driver.Close(ctx); err != nil { | ||
t.Fatalf("failed to close neo: %s", err) | ||
} | ||
}) | ||
return driver | ||
} |