Skip to content

Commit

Permalink
Adjust cloud architecture test session URI to recent changes (#764)
Browse files Browse the repository at this point in the history
* Adjust cloud architecture test session URI to recent changes

* Switch default session uri to docker host network

* Initialize fixtures only when needed

* Get server version from correct bolt address

* Implement enter and exit for GDS

* Remove condition in cloud setup and set cleanup autouse to false

* Skip cleanup on cloud-architecture

* Explicitly invoke clean_up instead of making it a fixture

* Try autouse clean_up again and use updated bolt endpoints

* Use URI and AUTH variables for session connection info

* Revert uri and auth changes in conftest option parsing

* Guard cleanup against read-only db exception

* Reset aura db bolt port to 7687

* Fix checkstyle

* Revert changes to clean_up method
  • Loading branch information
soerenreichardt authored Oct 8, 2024
1 parent 5832db9 commit 60b1c2f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
7 changes: 7 additions & 0 deletions graphdatascience/graph_data_science.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,10 @@ def close(self) -> None:
Close the GraphDataScience object and release any resources held by it.
"""
self._query_runner.close()

def __enter__(self) -> GraphDataScience:
return self

def __exit__(self, exc_type, exc_val, exc_tb) -> bool:
self.close()
return False
55 changes: 27 additions & 28 deletions graphdatascience/tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from pathlib import Path
from typing import Any, Generator, Optional
from typing import Any, Generator

import neo4j.exceptions
import pytest
from neo4j import Driver, GraphDatabase

Expand All @@ -23,11 +24,11 @@

DB = os.environ.get("NEO4J_DB", "neo4j")

AURA_DB_URI = os.environ.get("NEO4J_AURA_DB_URI", "bolt://localhost:7689")
AURA_DB_URI = os.environ.get("AURA_DB_URI", "bolt://localhost:7687")
AURA_DB_AUTH = ("neo4j", "password")


@pytest.fixture(scope="package")
@pytest.fixture(scope="package", autouse=False)
def neo4j_driver() -> Generator[Driver, None, None]:
driver = GraphDatabase.driver(URI, auth=AUTH)

Expand All @@ -36,7 +37,7 @@ def neo4j_driver() -> Generator[Driver, None, None]:
driver.close()


@pytest.fixture(scope="package")
@pytest.fixture(scope="package", autouse=False)
def runner(neo4j_driver: Driver) -> Generator[Neo4jQueryRunner, None, None]:
_runner = Neo4jQueryRunner.create(neo4j_driver)
_runner.set_database(DB)
Expand All @@ -46,7 +47,7 @@ def runner(neo4j_driver: Driver) -> Generator[Neo4jQueryRunner, None, None]:
_runner.close()


@pytest.fixture(scope="package")
@pytest.fixture(scope="package", autouse=False)
def gds() -> Generator[GraphDataScience, None, None]:
_gds = GraphDataScience(URI, auth=AUTH)
_gds.set_database(DB)
Expand All @@ -56,7 +57,7 @@ def gds() -> Generator[GraphDataScience, None, None]:
_gds.close()


@pytest.fixture(scope="package")
@pytest.fixture(scope="package", autouse=False)
def gds_with_tls() -> Generator[GraphDataScience, None, None]:
integration_test_dir = Path(__file__).resolve().parent
cert = os.path.join(integration_test_dir, "resources", "arrow-flight-gds-test.crt")
Expand All @@ -78,7 +79,7 @@ def gds_with_tls() -> Generator[GraphDataScience, None, None]:
_gds.close()


@pytest.fixture(scope="package")
@pytest.fixture(scope="package", autouse=False)
def gds_without_arrow() -> Generator[GraphDataScience, None, None]:
_gds = GraphDataScience(URI, auth=AUTH, arrow=False)
_gds.set_database(DB)
Expand All @@ -89,19 +90,17 @@ def gds_without_arrow() -> Generator[GraphDataScience, None, None]:


@pytest.fixture(scope="package", autouse=False)
def gds_with_cloud_setup(request: pytest.FixtureRequest) -> Optional[Generator[AuraGraphDataScience, None, None]]:
if "cloud_architecture" not in request.keywords:
_gds = AuraGraphDataScience.create(
gds_session_connection_info=DbmsConnectionInfo(URI, AUTH[0], AUTH[1]),
db_connection_info=DbmsConnectionInfo(AURA_DB_URI, AURA_DB_AUTH[0], AURA_DB_AUTH[1]),
delete_fn=lambda: True,
)
_gds.set_database(DB)
def gds_with_cloud_setup(request: pytest.FixtureRequest) -> Generator[AuraGraphDataScience, None, None]:
_gds = AuraGraphDataScience.create(
gds_session_connection_info=DbmsConnectionInfo(URI, AUTH[0], AUTH[1]),
db_connection_info=DbmsConnectionInfo(AURA_DB_URI, AURA_DB_AUTH[0], AURA_DB_AUTH[1]),
delete_fn=lambda: True,
)
_gds.set_database(DB)

yield _gds
yield _gds

_gds.close()
return None
_gds.close()


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -131,7 +130,10 @@ def clean_up(gds: GraphDataScience) -> Generator[None, None, None]:
if model.exists():
model.drop(failIfMissing=True)

gds.run_cypher("MATCH (n) DETACH DELETE (n)")
try:
gds.run_cypher("MATCH (n) DETACH DELETE (n)")
except neo4j.exceptions.ClientError as e:
print(e)


def pytest_collection_modifyitems(config: Any, items: Any) -> None:
Expand Down Expand Up @@ -188,15 +190,12 @@ def pytest_collection_modifyitems(config: Any, items: Any) -> None:
if "cloud_architecture" in item.keywords:
item.add_marker(skip_cloud_architecture)

gds = GraphDataScience(URI, auth=AUTH)

try:
server_version = gds._server_version
except Exception as e:
print("Could not derive GDS library server version")
raise e
finally:
gds.close()
with GraphDataScience(URI, auth=AUTH) as gds:
try:
server_version = gds._server_version
except Exception as e:
print("Could not derive GDS library server version")
raise e

skip_incompatible_versions = pytest.mark.skip(reason=f"incompatible with GDS server version {server_version}")

Expand Down

0 comments on commit 60b1c2f

Please sign in to comment.