Skip to content

Commit

Permalink
Add more unit tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
robsdedude committed Apr 7, 2024
1 parent 6d0b671 commit 8c07f48
Show file tree
Hide file tree
Showing 15 changed files with 1,578 additions and 992 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: "ubuntu-latest"
strategy:
matrix:
rust-version: ["1.70", "stable"]
rust-version: [ "1.70", "stable" ]
steps:
- name: Pull Neo4j Docker Image
run: docker pull neo4j:5.6-enterprise
Expand Down Expand Up @@ -57,14 +57,24 @@ jobs:
TEST_NEO4J_PASS: pass
TEST_NEO4J_EDITION: enterprise
TEST_NEO4J_VERSION: 5.6
run: cargo test --all
run: cargo test --workspace --all-targets
- name: doc tests
env:
TEST_NEO4J_SCHEME: neo4j
TEST_NEO4J_HOST: localhost
TEST_NEO4J_PORT: 7687
TEST_NEO4J_USER: neo4j
TEST_NEO4J_PASS: pass
TEST_NEO4J_EDITION: enterprise
TEST_NEO4J_VERSION: 5.6
run: cargo test --workspace --doc
testkit:
name: TestKit
needs: [tests]
needs: [ tests ]
runs-on: ubuntu-latest
strategy:
matrix:
tests: [TESTKIT_TESTS]
tests: [ TESTKIT_TESTS ]
config:
- 4.4-community-bolt
- 4.4-community-neo4j
Expand Down
2 changes: 1 addition & 1 deletion doc_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn get_driver() -> Driver {
.is_test(true)
.try_init();

let driver = neo4j::driver::Driver::new(
let driver = Driver::new(
ConnectionConfig::new(get_address()),
DriverConfig::new().with_auth(Arc::new(get_auth_token())),
);
Expand Down
10 changes: 10 additions & 0 deletions examples/basic.rs → neo4j/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ fn main() {
assert_eq!(record.take_value("x"), Some(ValueReceive::Integer(123)));
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_main() {
main();
}
}
69 changes: 68 additions & 1 deletion neo4j/src/address_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ use std::vec::IntoIter;
use crate::error_::Result;
use resolution::{AddressResolver, CustomResolution, DnsResolution};

// imports for docs
#[allow(unused)]
use crate::driver::DriverConfig;

pub(crate) const DEFAULT_PORT: u16 = 7687;
const COLON_BYTES: usize = ':'.len_utf8();

Expand Down Expand Up @@ -72,6 +76,10 @@ pub struct Address {
pub(crate) is_dns_resolved: bool,
}

/// Note that equality of addresses is defined as equality of its [`Address::unresolved_host()`]
/// and [`Address::port()`] only.
/// Therefore, resolved to different IP addresses coming from the same host are considered equal
/// if their port is equal as well.
impl PartialEq for Address {
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -128,6 +136,41 @@ impl Address {
}

/// Return the host name or IP address.
///
/// For addresses that have been resolved by the driver, this will be the final IP address after
/// all resolutions.
/// This includes:
/// * potential custom address resolver, see [`DriverConfig::with_resolver`]
/// * DNS resolution, see [`ToSocketAddrs`].
///
/// # Example
/// ```
/// use neo4j::address::Address;
///
/// let addr = Address::from(("localhost", 1234));
/// assert_eq!(addr.host(), "localhost");
/// ```
///
/// # Example (after resolution)
/// ```
/// use neo4j::address::Address;
/// use neo4j::driver::Driver;
///
/// use std::net::ToSocketAddrs;
/// # use doc_test_utils::get_address;
///
/// let address: Address = get_address();
/// # fn get_driver(_: &Address) -> Driver {
/// # doc_test_utils::get_driver()
/// # }
/// let driver: Driver = get_driver(&address);
/// let resolved_address = driver.get_server_info().unwrap().address;
///
/// assert!(address
/// .to_socket_addrs()
/// .unwrap()
/// .any(|sock_addr| { &sock_addr.ip().to_string() == resolved_address.host() }));
/// ```
pub fn host(&self) -> &str {
self.host.as_str()
}
Expand All @@ -137,7 +180,31 @@ impl Address {
self.port
}

pub(crate) fn unresolved_host(&self) -> &str {
/// Return the host name (before a potential DNS resolution).
///
/// When a custom address resolver is registered with the driver (see
/// [`DriverConfig::with_resolver`]), `unresolved_host` will return the host name
/// from after the custom address resolver.
///
/// # Example
/// ```
/// use neo4j::address::Address;
/// use neo4j::driver::Driver;
/// # use doc_test_utils::get_address;
///
/// let address: Address = get_address();
/// # fn get_driver(_: &Address) -> Driver {
/// # doc_test_utils::get_driver()
/// # }
/// let driver: Driver = get_driver(&address);
/// let resolved_address = driver.get_server_info().unwrap().address;
///
/// assert_eq!(address.host(), resolved_address.unresolved_host());
/// // but not necessarily
/// // assert_eq!(address.host(), resolved_address.host());
/// // because resolved_address.host() will be DNS resolved.
/// ```
pub fn unresolved_host(&self) -> &str {
&self.key
}
}
Expand Down
Loading

0 comments on commit 8c07f48

Please sign in to comment.