Skip to content

Commit e67a25d

Browse files
author
matmoncon
committed
test: fix test cases and add new one for duplicate model registration
1 parent 38bbe05 commit e67a25d

File tree

7 files changed

+34
-164
lines changed

7 files changed

+34
-164
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
Things to come in the future. Truly exiting stuff! If you have feature requests which you think might improve `pyneo4j-ogm`, feel free to open up a feature request.
2525

2626
- [ ] [MemGraph](https://memgraph.com/) support.
27-
- [ ] Usage examples for `pyneo4j-ogm` in different environments.
2827

2928
## 📦 Installation
3029

@@ -369,16 +368,16 @@ In the following we are going to take a closer look at the different parts of `p
369368

370369
### Running the test suite
371370

372-
To run the test suite, you have to install the development dependencies and run the tests using `pytest`. The tests are located in the `tests` directory. Any tests located in the `tests/integration` directory will require you to have a Neo4j instance running on `localhost:7687` with the credentials (`neo4j:password`). This can easily be done using the provided `docker-compose.yml` file.
371+
To run the test suite, you have to install the development dependencies and run the tests using `pytest`. The tests are located in the `tests` directory. Some tests will require you to have a Neo4j instance running on `localhost:7687` with the credentials (`neo4j:password`). This can easily be done using the provided `docker-compose.yml` file.
373372

374373
```bash
375374
poetry run pytest tests --asyncio-mode=auto -W ignore::DeprecationWarning
376375
```
377376

378377
> **Note:** The `-W ignore::DeprecationWarning` can be omitted but will result in a lot of deprication warnings by Neo4j itself about the usage of the now deprecated `ID`.
379378
380-
As for running the tests with a different pydantic version, you can just run the following command locally:
379+
As for running the tests with a different pydantic version, you can just install a different pydantic version with the following command:
381380

382381
```bash
383-
poetry add pydantic@1.10
382+
poetry add pydantic@<version>
384383
```

pyneo4j_ogm/core/client.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,22 @@ async def register_models(self, models: List[Type[Union[NodeModel, RelationshipM
153153
logger.info("Registering models %s with client %s", models, self)
154154

155155
for model in models:
156-
# for registered_model in self.models:
157-
# registered_model_settings = registered_model.model_settings()
158-
# model_settings = model.model_settings()
159-
160-
# if (
161-
# isinstance(registered_model_settings, RelationshipModelSettings)
162-
# and isinstance(model_settings, RelationshipModelSettings)
163-
# and registered_model_settings.type == model_settings.type
164-
# ):
165-
# raise AlreadyRegistered(cast(str, model_settings.type))
166-
# elif (
167-
# isinstance(registered_model_settings, NodeModelSettings)
168-
# and isinstance(model_settings, NodeModelSettings)
169-
# and set(registered_model_settings.labels) == set(model_settings.labels)
170-
# ):
171-
# raise AlreadyRegistered(cast(str, model_settings.labels))
156+
for registered_model in self.models:
157+
registered_model_settings = registered_model.model_settings()
158+
model_settings = model.model_settings()
159+
160+
if (
161+
isinstance(registered_model_settings, RelationshipModelSettings)
162+
and isinstance(model_settings, RelationshipModelSettings)
163+
and registered_model_settings.type == model_settings.type
164+
):
165+
raise AlreadyRegistered(cast(str, model_settings.type))
166+
elif (
167+
isinstance(registered_model_settings, NodeModelSettings)
168+
and isinstance(model_settings, NodeModelSettings)
169+
and set(registered_model_settings.labels) == set(model_settings.labels)
170+
):
171+
raise AlreadyRegistered(cast(str, model_settings.labels))
172172

173173
if issubclass(model, (NodeModel, RelationshipModel)):
174174
logger.debug("Found valid mode %s, registering with client", model.__name__)

tests/core/test_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pyneo4j_ogm.core.node import NodeModel
1515
from pyneo4j_ogm.core.relationship import RelationshipModel
1616
from pyneo4j_ogm.exceptions import (
17+
AlreadyRegistered,
1718
InvalidEntityType,
1819
InvalidLabelOrType,
1920
MissingDatabaseURI,
@@ -40,6 +41,19 @@ class Settings:
4041
type = "TEST_RELATIONSHIP"
4142

4243

44+
async def test_duplicate_model_register(client: Pyneo4jClient):
45+
class TestNode(NodeModel):
46+
name: str
47+
48+
class Settings:
49+
labels = {"TestNode"}
50+
51+
await client.register_models([TestNode])
52+
53+
with pytest.raises(AlreadyRegistered):
54+
await client.register_models([TestNode])
55+
56+
4357
async def test_batch(client: Pyneo4jClient, session: AsyncSession):
4458
async with client.batch():
4559
await client.cypher("CREATE (n:Node) SET n.name = $name", parameters={"name": "TestName"})

tests/core/test_node.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,6 @@ async def test_delete_one_invalid_filter(setup_test_data):
972972

973973

974974
async def test_delete_one_no_results(client: Pyneo4jClient, session: AsyncSession, setup_test_data):
975-
await client.register_models([CoffeeShop])
976-
977975
with patch.object(client, "cypher") as mock_cypher:
978976
mock_cypher.return_value = [[], []]
979977

@@ -1001,8 +999,6 @@ async def test_delete_many(session: AsyncSession, setup_test_data):
1001999

10021000

10031001
async def test_delete_many_no_match(client: Pyneo4jClient, session: AsyncSession, setup_test_data):
1004-
await client.register_models([CoffeeShop])
1005-
10061002
count = await CoffeeShop.delete_many({"tags": {"$in": ["oh-no"]}})
10071003
assert count == 0
10081004

@@ -1022,8 +1018,6 @@ async def test_delete_many_no_match(client: Pyneo4jClient, session: AsyncSession
10221018

10231019

10241020
async def test_delete_many_no_results(client: Pyneo4jClient, session: AsyncSession, setup_test_data):
1025-
await client.register_models([CoffeeShop])
1026-
10271021
with patch.object(client, "cypher") as mock_cypher:
10281022
mock_cypher.return_value = [[], []]
10291023

0 commit comments

Comments
 (0)