Skip to content

Commit

Permalink
Add support for Neo4j Labs plugins. (#5)
Browse files Browse the repository at this point in the history
* Add support for Neo4j Labs plugins.

* Implement requested changes.

* Do what fmt says.

* Do what fmt says (II).
  • Loading branch information
meistermeier committed Jun 12, 2023
1 parent fb91ad9 commit 8f1c15b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ The following environment variables are supported:
- `NEO4J_TEST_USER`: The default user to use for authentication.
- `NEO4J_TEST_PASS`: The default password to use for authentication.

## Neo4j Labs Plugins

Neo4j offers built-in support for Neo4j Labs plugins.
The method `with_neo4j_labs_plugin` can be used to define them.

Supported plugins are APOC, APOC Core, Bloom, Streams, Graph Data Science, and Neo Semantics.

## MSRV

Expand Down
7 changes: 7 additions & 0 deletions doc/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ The following environment variables are supported:
* `NEO4J_TEST_USER`: The default user to use for authentication.
* `NEO4J_TEST_PASS`: The default password to use for authentication.

# Neo4j Labs Plugins

Neo4j offers built-in support for Neo4j Labs plugins.
The method `with_neo4j_labs_plugin` can be used to define them.

Supported plugins are APOC, APOC Core, Bloom, Streams, Graph Data Science, and Neo Semantics.

# MSRV

The crate has a minimum supported Rust version (MSRV) of `1.60.0`.
Expand Down
71 changes: 71 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@
use std::collections::HashMap;
use testcontainers::{core::WaitFor, Container, Image};

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Neo4jLabsPlugin {
Apoc,
ApocCore,
Bloom,
Streams,
GraphDataScience,
NeoSemantics,
Custom(String),
}

impl std::fmt::Display for Neo4jLabsPlugin {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Apoc => formatter.pad("apoc"),
Self::ApocCore => formatter.pad("apoc-core"),
Self::Bloom => formatter.pad("bloom"),
Self::Streams => formatter.pad("streams"),
Self::GraphDataScience => formatter.pad("graph-data-science"),
Self::NeoSemantics => formatter.pad("n10s"),
Self::Custom(plugin_name) => formatter.pad(plugin_name),
}
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Neo4j {
version: String,
Expand Down Expand Up @@ -74,6 +100,27 @@ impl Neo4j {
)
}

/// Define Neo4j lab plugins to get started with the database.
/// Returns new instance.
pub fn with_neo4j_labs_plugin(mut self, plugins: &[Neo4jLabsPlugin]) -> Self {
if plugins.is_empty() {
return self;
}

let plugin_names = plugins
.iter()
.map(|p| format!("\"{}\"", p))
.collect::<Vec<String>>()
.join(",");

let plugin_definition = format!("[{}]", plugin_names);

self.env_vars
.insert("NEO4JLABS_PLUGINS".to_owned(), plugin_definition);

self
}

fn new(user: Option<String>, pass: Option<String>, version: Option<String>) -> Self {
const USER_VAR: &str = "NEO4J_TEST_USER";
const PASS_VAR: &str = "NEO4J_TEST_PASS";
Expand Down Expand Up @@ -222,3 +269,27 @@ impl Image for Neo4j {
Box::new(self.env_vars.iter())
}
}

#[cfg(test)]
mod tests {
use crate::{Neo4j, Neo4jLabsPlugin};

#[test]
fn single_plugin_definition() {
let neo4j = Neo4j::default().with_neo4j_labs_plugin(&[Neo4jLabsPlugin::Apoc]);
assert_eq!(
neo4j.env_vars.get("NEO4JLABS_PLUGINS").unwrap(),
"[\"apoc\"]"
);
}

#[test]
fn multiple_plugin_definition() {
let neo4j = Neo4j::default()
.with_neo4j_labs_plugin(&[Neo4jLabsPlugin::Apoc, Neo4jLabsPlugin::Bloom]);
assert_eq!(
neo4j.env_vars.get("NEO4JLABS_PLUGINS").unwrap(),
"[\"apoc\",\"bloom\"]"
);
}
}

0 comments on commit 8f1c15b

Please sign in to comment.