Skip to content

Commit f1db22c

Browse files
committed
Use external neo4j_testcontainers
1 parent e039048 commit f1db22c

File tree

2 files changed

+52
-82
lines changed

2 files changed

+52
-82
lines changed

lib/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ webpki-roots = "0.23.0"
3131
[dev-dependencies]
3232
lenient_semver = { version = "0.4.2", default_features = false, features = ["version_lite"] }
3333
pretty_env_logger = "0.4.0"
34-
testcontainers = { version = "0.14.0" }
34+
testcontainers = "0.14.0"
35+
neo4j_testcontainers = "0.1.0"
3536
uuid = { version = "1.0.0", features = ["v4"] }

lib/tests/container.rs

Lines changed: 50 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use lenient_semver::Version;
2+
use neo4j_testcontainers::Neo4j;
23
use neo4rs::{ConfigBuilder, Graph};
3-
use testcontainers::{clients::Cli, core::WaitFor, Container, Image};
4+
use testcontainers::{clients::Cli, Container};
45

5-
use std::{collections::HashMap, sync::Arc};
6+
use std::sync::Arc;
67

78
pub struct Neo4jContainer {
89
graph: Arc<Graph>,
@@ -19,17 +20,21 @@ impl Neo4jContainer {
1920
pub async fn from_config(config: ConfigBuilder) -> Self {
2021
let _ = pretty_env_logger::try_init();
2122

22-
let (server, version) = Self::server_from_env();
23+
let server = Self::server_from_env();
2324

2425
let (connection, _container) = match server {
25-
TestServer::TestContainer { auth } => {
26-
let (uri, container) = Self::create_testcontainer(&auth, &version).await;
27-
(TestConnection { uri, auth }, Some(container))
26+
TestServer::TestContainer => {
27+
let (connection, container) = Self::create_testcontainer();
28+
(connection, Some(container))
29+
}
30+
TestServer::External(uri) => {
31+
let connection = Self::create_test_endpoint(uri);
32+
(connection, None)
2833
}
29-
TestServer::External { connection } => (connection, None),
3034
};
3135

32-
let graph = Self::connect(config, connection).await;
36+
let version = connection.version;
37+
let graph = Self::connect(config, connection.uri, &connection.auth).await;
3338
Self {
3439
graph,
3540
version,
@@ -49,52 +54,57 @@ impl Neo4jContainer {
4954
.0
5055
}
5156

52-
fn server_from_env() -> (TestServer, String) {
57+
fn server_from_env() -> TestServer {
58+
const TEST_URI_VAR: &str = "NEO4J_TEST_URI";
59+
60+
if let Ok(uri) = std::env::var(TEST_URI_VAR) {
61+
TestServer::External(uri)
62+
} else {
63+
TestServer::TestContainer
64+
}
65+
}
66+
67+
fn create_testcontainer() -> (TestConnection, Container<'static, Neo4j>) {
68+
let docker = Cli::default();
69+
let docker = Box::leak(Box::new(docker));
70+
71+
let container = docker.run(Neo4j::default());
72+
73+
let uri = Neo4j::uri_ipv4(&container);
74+
let version = container.image().version().to_owned();
75+
let user = container.image().user().to_owned();
76+
let pass = container.image().pass().to_owned();
77+
let auth = TestAuth { user, pass };
78+
79+
let connection = TestConnection { uri, version, auth };
80+
81+
(connection, container)
82+
}
83+
84+
fn create_test_endpoint(uri: String) -> TestConnection {
5385
const USER_VAR: &str = "NEO4J_TEST_USER";
5486
const PASS_VAR: &str = "NEO4J_TEST_PASS";
55-
const TEST_URI_VAR: &str = "NEO4J_TEST_URI";
5687
const VERSION_VAR: &str = "NEO4J_VERSION_TAG";
5788

5889
const DEFAULT_USER: &str = "neo4j";
5990
const DEFAULT_PASS: &str = "neo";
60-
const DEFAULT_VERSION_TAG: &str = "4.2";
91+
const DEFAULT_VERSION_TAG: &str = "5";
6192

6293
use std::env::var;
6394

6495
let user = var(USER_VAR).unwrap_or_else(|_| DEFAULT_USER.to_owned());
6596
let pass = var(PASS_VAR).unwrap_or_else(|_| DEFAULT_PASS.to_owned());
6697
let auth = TestAuth { user, pass };
67-
6898
let version = var(VERSION_VAR).unwrap_or_else(|_| DEFAULT_VERSION_TAG.to_owned());
6999

70-
if let Ok(uri) = var(TEST_URI_VAR) {
71-
let config = TestConnection { uri, auth };
72-
(TestServer::External { connection: config }, version)
73-
} else {
74-
(TestServer::TestContainer { auth }, version)
75-
}
100+
TestConnection { uri, auth, version }
76101
}
77102

78-
async fn create_testcontainer(
79-
auth: &TestAuth,
80-
version: &str,
81-
) -> (String, Container<'static, Neo4j>) {
82-
let docker = Cli::default();
83-
let docker = Box::leak(Box::new(docker));
84-
85-
let container = docker.run(Neo4j::new(&auth.user, &auth.pass, version.to_owned()));
86-
87-
let bolt_port = container.ports().map_to_host_port_ipv4(7687).unwrap();
88-
let uri = format!("bolt://127.0.0.1:{}", bolt_port);
89-
90-
(uri, container)
91-
}
92-
93-
async fn connect(config: ConfigBuilder, info: TestConnection) -> Arc<Graph> {
103+
async fn connect(config: ConfigBuilder, uri: String, auth: &TestAuth) -> Arc<Graph> {
94104
let config = config
95-
.uri(&info.uri)
96-
.user(&info.auth.user)
97-
.password(&info.auth.pass)
105+
.uri(uri)
106+
.user(&auth.user)
107+
.password(&auth.pass)
98108
.build()
99109
.unwrap();
100110

@@ -111,52 +121,11 @@ struct TestAuth {
111121

112122
struct TestConnection {
113123
uri: String,
124+
version: String,
114125
auth: TestAuth,
115126
}
116127

117128
enum TestServer {
118-
TestContainer { auth: TestAuth },
119-
External { connection: TestConnection },
120-
}
121-
122-
#[derive(Debug)]
123-
struct Neo4j {
124-
version: String,
125-
env_vars: HashMap<String, String>,
126-
}
127-
128-
impl Neo4j {
129-
fn new(user: &str, pass: &str, version: String) -> Self {
130-
let mut env_vars = HashMap::new();
131-
env_vars.insert("NEO4J_AUTH".to_owned(), format!("{user}/{pass}"));
132-
env_vars.insert(
133-
"NEO4J_dbms_security_auth__minimum__password__length".to_owned(),
134-
"3".to_owned(),
135-
);
136-
137-
Self { env_vars, version }
138-
}
139-
}
140-
141-
impl Image for Neo4j {
142-
type Args = ();
143-
144-
fn name(&self) -> String {
145-
"neo4j".to_owned()
146-
}
147-
148-
fn tag(&self) -> String {
149-
self.version.clone()
150-
}
151-
152-
fn ready_conditions(&self) -> Vec<WaitFor> {
153-
vec![
154-
WaitFor::message_on_stdout("Bolt enabled on"),
155-
WaitFor::message_on_stdout("Started."),
156-
]
157-
}
158-
159-
fn env_vars(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
160-
Box::new(self.env_vars.iter())
161-
}
129+
TestContainer,
130+
External(String),
162131
}

0 commit comments

Comments
 (0)