Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12 update neomodel version to 5.3.0 #14

Merged
merged 12 commits into from
May 10, 2024
15 changes: 11 additions & 4 deletions .github/workflows/sonar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ jobs:

services:
neo4j-db:
# Set up neo4j DB with APOC extension for UUIDs.
image: neo4j:5.8.0
# Set up neo4j DB with APOC extension for UUIDs.
image: neo4j:5.18.0
ports:
- 7687
- 7474
env:
NEO4J_AUTH: none
NEO4J_PLUGINS: "apoc"
options: >-
--health-cmd "wget http://localhost:7474 || exit 1"
--health-interval 1s
--health-timeout 10s
--health-retries 20
--health-start-period 3s
strategy:
fail-fast: true
Expand Down Expand Up @@ -56,11 +63,11 @@ jobs:

# Install dependencies if cache does not exist
- name: Install dependencies
run: poetry install --no-interaction --no-root
run: poetry install --no-interaction

# Run tests
- name: Run tests
run: poetry run pytest tests/test_auth.py --cov --cov-report=xml --cov-report=html
run: poetry run pytest tests/models tests/schemas --cov --cov-report=xml --cov-report=html
env:
NEO4J_SERVER: "localhost:${{ job.services.neo4j-db.ports[7687] }}"

Expand Down
27 changes: 12 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ python = ">=3.9,<4.0.0"
flaat = "^1.1.15"
fastapi = "^0.109"
uvicorn = "^0.22.0"
neomodel = {git = "https://github.com/giosava94/neomodel.git", branch = "db-transaction"}
neomodel = "^5.3.0"
pydantic = {extras = ["email"], version = "^1.10.9"}
pycountry = "^22.3.5"

Expand Down
107 changes: 51 additions & 56 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""File to set tests configuration parameters and common fixtures."""
import os
from typing import Any, Generator
from unittest.mock import MagicMock, PropertyMock, patch
from uuid import uuid4

import pytest
from neo4j.exceptions import ServiceUnavailable
from neomodel import db

from fed_reg.flavor.models import Flavor
Expand Down Expand Up @@ -78,77 +76,74 @@
user_group_model_dict,
user_group_schema_dict,
)
from tests.db import MockDatabase

try:
db.begin()
db.commit()
USE_MOCK_DB = False
except ServiceUnavailable:
USE_MOCK_DB = True

def pytest_addoption(parser):
"""
Adds the command line option --resetdb.

@pytest.fixture(autouse=True)
def clear_os_environment() -> None:
"""Clear the OS environment."""
os.environ.clear()


@pytest.fixture()
def fake_db() -> MockDatabase:
return MockDatabase()
:param parser: The parser object. Please see <https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_addoption>`_
:type Parser object: For more information please see <https://docs.pytest.org/en/latest/reference.html#_pytest.config.Parser>`_
"""
parser.addoption(
"--resetdb",
action="store_true",
help="Ensures that the database is clear prior to running tests for neomodel",
default=False,
)


@pytest.fixture()
def db_core(fake_db: MockDatabase) -> Generator[None, Any, None]:
with patch("neomodel.core.db") as mock_db:
type(mock_db).database_version = PropertyMock(
return_value=str(fake_db.database_version)
)
mock_db.cypher_query.side_effect = fake_db.query_call
yield
@pytest.fixture(scope="session", autouse=True)
def setup_neo4j_session(request):
"""
Provides initial connection to the database and sets up the rest of the test suite

:param request: The request object. Please see <https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_sessionstart>`_
:type Request object: For more information please see <https://docs.pytest.org/en/latest/reference.html#request>`_
"""
# config.DATABASE_URL = os.environ.get(
# "NEO4J_BOLT_URL", "bolt://localhost:7687"
# )

@pytest.fixture()
def db_match(fake_db: MockDatabase) -> Generator[None, Any, None]:
with patch("neomodel.match.db") as mock_db:
type(mock_db).database_version = PropertyMock(
return_value=str(fake_db.database_version)
# Clear the database if required
database_is_populated, _ = db.cypher_query(
"MATCH (a) return count(a)>0 as database_is_populated"
)
if database_is_populated[0][0] and not request.config.getoption("resetdb"):
raise SystemError(
"Please note: The database seems to be populated.\n"
+ "\tEither delete all nodesand edges manually, or set the "
+ "--resetdb parameter when calling pytest\n\n"
+ "\tpytest --resetdb."
)
mock_db.cypher_query.side_effect = fake_db.query_call
yield

db.clear_neo4j_database(clear_constraints=True, clear_indexes=True)

@pytest.fixture()
def db_rel_mgr(fake_db: MockDatabase) -> Generator[None, Any, None]:
with patch("neomodel.relationship_manager.db") as mock_db:
type(mock_db).database_version = PropertyMock(
return_value=str(fake_db.database_version)
)
db.install_all_labels()

d = {}
cls_registry = MagicMock()
cls_registry.__getitem__.side_effect = d.__getitem__
cls_registry.__setitem__.side_effect = d.__setitem__
mock_db._NODE_CLASS_REGISTRY = cls_registry
db.cypher_query(
"CREATE OR REPLACE USER test SET PASSWORD 'foobarbaz' CHANGE NOT REQUIRED"
)
if db.database_edition == "enterprise":
db.cypher_query("GRANT ROLE publisher TO test")
db.cypher_query("GRANT IMPERSONATE (test) ON DBMS TO admin")

yield

@pytest.fixture(scope="session", autouse=True)
def cleanup() -> Generator[None, Any, None]:
"""Close connection with the DB at the end of the test.

@pytest.fixture(autouse=USE_MOCK_DB)
def mock_db(
db_core: None, db_match: None, db_rel_mgr: None
) -> Generator[None, Any, None]:
print("Using MOCK DB")
Yields:
Generator[None, Any, None]: Nothing
"""
yield
db.close_connection()


@pytest.fixture(autouse=not USE_MOCK_DB)
def clear_db() -> Generator[None, Any, None]:
print("Using REAL DB")
db.begin()
yield
db.rollback()
@pytest.fixture(autouse=True)
def clear_os_environment() -> None:
"""Clear the OS environment."""
os.environ.clear()


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_network_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_multiple_linked_projects(network_model: Network) -> None:
with pytest.raises(AttemptedCardinalityViolation):
network_model.project.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
network_model.project.connect(item)
with pytest.raises(CardinalityViolation):
network_model.project.all()
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_multiple_linked_services(network_model: Network) -> None:
with pytest.raises(AttemptedCardinalityViolation):
network_model.service.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
network_model.service.connect(item)
with pytest.raises(CardinalityViolation):
network_model.service.all()
4 changes: 2 additions & 2 deletions tests/models/test_project_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_multiple_linked_provider(project_model: Project) -> None:
with pytest.raises(AttemptedCardinalityViolation):
project_model.provider.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
project_model.provider.connect(item)
with pytest.raises(CardinalityViolation):
project_model.provider.all()
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_multiple_linked_sla(project_model: Project) -> None:
with pytest.raises(AttemptedCardinalityViolation):
project_model.sla.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
project_model.sla.connect(item)
with pytest.raises(CardinalityViolation):
project_model.sla.all()
Expand Down
8 changes: 4 additions & 4 deletions tests/models/test_quota_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_multiple_linked_projects(
with pytest.raises(AttemptedCardinalityViolation):
quota_model.project.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
quota_model.project.connect(item)
with pytest.raises(CardinalityViolation):
quota_model.project.all()
Expand Down Expand Up @@ -282,7 +282,7 @@ def test_multiple_linked_block_storage_services(
with pytest.raises(AttemptedCardinalityViolation):
block_storage_quota_model.service.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
block_storage_quota_model.service.connect(item)
with pytest.raises(CardinalityViolation):
block_storage_quota_model.service.all()
Expand All @@ -295,7 +295,7 @@ def test_multiple_linked_compute_services(compute_quota_model: ComputeQuota) ->
with pytest.raises(AttemptedCardinalityViolation):
compute_quota_model.service.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
compute_quota_model.service.connect(item)
with pytest.raises(CardinalityViolation):
compute_quota_model.service.all()
Expand All @@ -308,7 +308,7 @@ def test_multiple_linked_network_services(network_quota_model: NetworkQuota) ->
with pytest.raises(AttemptedCardinalityViolation):
network_quota_model.service.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
network_quota_model.service.connect(item)
with pytest.raises(CardinalityViolation):
network_quota_model.service.all()
4 changes: 2 additions & 2 deletions tests/models/test_region_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_multiple_linked_location(region_model: Region) -> None:
with pytest.raises(AttemptedCardinalityViolation):
region_model.location.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
region_model.location.connect(item)
with pytest.raises(CardinalityViolation):
region_model.location.all()
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_multiple_linked_provider(region_model: Region) -> None:
with pytest.raises(AttemptedCardinalityViolation):
region_model.provider.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
region_model.provider.connect(item)
with pytest.raises(CardinalityViolation):
region_model.provider.all()
2 changes: 1 addition & 1 deletion tests/models/test_sla_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_multiple_linked_user_group(sla_model: SLA) -> None:
with pytest.raises(AttemptedCardinalityViolation):
sla_model.user_group.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
sla_model.user_group.connect(item)
with pytest.raises(CardinalityViolation):
sla_model.user_group.all()
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_user_group_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_multiple_linked_identity_provider(user_group_model: UserGroup) -> None:
with pytest.raises(AttemptedCardinalityViolation):
user_group_model.identity_provider.connect(item)

with patch("neomodel.match.QueryBuilder._count", return_value=0):
with patch("neomodel.sync_.match.QueryBuilder._count", return_value=0):
user_group_model.identity_provider.connect(item)
with pytest.raises(CardinalityViolation):
user_group_model.identity_provider.all()
Loading