Skip to content

Commit 8346d2a

Browse files
Add-in missing test
1 parent 084f9f5 commit 8346d2a

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

test/async_/test_relationships.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pytest import raises
44

55
from neomodel import (
6+
AsyncMutuallyExclusive,
67
AsyncOne,
78
AsyncRelationship,
89
AsyncRelationshipFrom,
@@ -14,6 +15,7 @@
1415
StringProperty,
1516
adb,
1617
)
18+
from neomodel.exceptions import MutualExclusionViolation
1719

1820

1921
class PersonWithRels(AsyncStructuredNode):
@@ -207,3 +209,64 @@ async def test_props_relationship():
207209

208210
with raises(NotImplementedError):
209211
await c.inhabitant.connect(u, properties={"city": "Thessaloniki"})
212+
213+
214+
class JealousDog(AsyncStructuredNode):
215+
name = StringProperty(required=True)
216+
217+
218+
class JealousCat(AsyncStructuredNode):
219+
name = StringProperty(required=True)
220+
221+
222+
class ExclusivePerson(AsyncStructuredNode):
223+
name = StringProperty(required=True)
224+
225+
# Define mutually exclusive relationships
226+
cat = AsyncRelationshipTo(
227+
"JealousCat",
228+
"HAS_PET",
229+
cardinality=AsyncMutuallyExclusive,
230+
exclusion_group=["dog"],
231+
)
232+
dog = AsyncRelationshipTo(
233+
"JealousDog",
234+
"HAS_PET",
235+
cardinality=AsyncMutuallyExclusive,
236+
exclusion_group=["cat"],
237+
)
238+
239+
240+
@mark_async_test
241+
async def test_mutually_exclusive_relationships():
242+
# Create test nodes
243+
bob = await ExclusivePerson(name="Bob").save()
244+
rex = await JealousDog(name="Rex").save()
245+
whiskers = await JealousCat(name="Whiskers").save()
246+
247+
# Bob can have a dog
248+
await bob.dog.connect(rex)
249+
250+
# But now Bob can't have a cat because he already has a dog
251+
with raises(MutualExclusionViolation):
252+
await bob.cat.connect(whiskers)
253+
254+
# Create another person
255+
alice = await ExclusivePerson(name="Alice").save()
256+
257+
# Alice can have a cat
258+
await alice.cat.connect(whiskers)
259+
260+
# But now Alice can't have a dog because she already has a cat
261+
with raises(MutualExclusionViolation):
262+
await alice.dog.connect(rex)
263+
264+
# If Alice disconnects her cat, she can then have a dog
265+
await alice.cat.disconnect(whiskers)
266+
await alice.dog.connect(rex)
267+
268+
# Verify the connections
269+
assert len(await bob.dog.all()) == 1
270+
assert len(await bob.cat.all()) == 0
271+
assert len(await alice.dog.all()) == 1
272+
assert len(await alice.cat.all()) == 0

test/sync_/test_relationships.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,9 @@ class ExclusivePerson(StructuredNode):
240240
@mark_sync_test
241241
def test_mutually_exclusive_relationships():
242242
# Create test nodes
243-
bob = ExclusivePerson(name="Bob")
244-
bob.save()
245-
rex = JealousDog(name="Rex")
246-
rex.save()
247-
whiskers = JealousCat(name="Whiskers")
248-
whiskers.save()
243+
bob = ExclusivePerson(name="Bob").save()
244+
rex = JealousDog(name="Rex").save()
245+
whiskers = JealousCat(name="Whiskers").save()
249246

250247
# Bob can have a dog
251248
bob.dog.connect(rex)
@@ -255,8 +252,7 @@ def test_mutually_exclusive_relationships():
255252
bob.cat.connect(whiskers)
256253

257254
# Create another person
258-
alice = ExclusivePerson(name="Alice")
259-
alice.save()
255+
alice = ExclusivePerson(name="Alice").save()
260256

261257
# Alice can have a cat
262258
alice.cat.connect(whiskers)

0 commit comments

Comments
 (0)