Skip to content

Commit 2cc624b

Browse files
Merge pull request #910 from billycalladine/issue/909
issue/909 - Restore cardinality error message verbosity
2 parents 0c5394f + 17ab4d8 commit 2cc624b

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

neomodel/async_/cardinality.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ class AsyncZeroOrOne(AsyncRelationshipManager):
1818

1919
async def check_cardinality(self, node: "AsyncStructuredNode") -> None:
2020
if await self.get_len():
21+
detailed_description = str(self)
2122
if get_config().soft_cardinality_check:
2223
print(
23-
f"Cardinality violation detected : Node already has one relationship of type {self.definition['relation_type']}, should not connect more. Soft check is enabled so the relationship will be created. Note that strict check will be enabled by default in version 6.0"
24+
f"Cardinality violation detected : Node already has {detailed_description}, should not connect more. Soft check is enabled so the relationship will be created."
2425
)
2526
else:
2627
raise AttemptedCardinalityViolation(
27-
f"Node already has one relationship of type {self.definition['relation_type']}. Use reconnect() to replace the existing relationship."
28+
f"Node already has {detailed_description}. Use reconnect() to replace the existing relationship."
2829
)
2930

3031
async def single(self) -> Optional["AsyncStructuredNode"]:
@@ -111,13 +112,14 @@ class AsyncOne(AsyncRelationshipManager):
111112

112113
async def check_cardinality(self, node: "AsyncStructuredNode") -> None:
113114
if await self.get_len():
115+
detailed_description = str(self)
114116
if get_config().soft_cardinality_check:
115117
print(
116-
f"Cardinality violation detected : Node already has one relationship of type {self.definition['relation_type']}, should not connect more. Soft check is enabled so the relationship will be created. Note that strict check will be enabled by default in version 6.0"
118+
f"Cardinality violation detected : Node already has {detailed_description}, should not connect more. Soft check is enabled so the relationship will be created."
117119
)
118120
else:
119121
raise AttemptedCardinalityViolation(
120-
f"Node already has one relationship of type {self.definition['relation_type']}. Use reconnect() to replace the existing relationship."
122+
f"Node already has {detailed_description}. Use reconnect() to replace the existing relationship."
121123
)
122124

123125
async def single(self) -> "AsyncStructuredNode":

neomodel/sync_/cardinality.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ class ZeroOrOne(RelationshipManager):
1818

1919
def check_cardinality(self, node: "StructuredNode") -> None:
2020
if self.__len__():
21+
detailed_description = str(self)
2122
if get_config().soft_cardinality_check:
2223
print(
23-
f"Cardinality violation detected : Node already has one relationship of type {self.definition['relation_type']}, should not connect more. Soft check is enabled so the relationship will be created. Note that strict check will be enabled by default in version 6.0"
24+
f"Cardinality violation detected : Node already has {detailed_description}, should not connect more. Soft check is enabled so the relationship will be created."
2425
)
2526
else:
2627
raise AttemptedCardinalityViolation(
27-
f"Node already has one relationship of type {self.definition['relation_type']}. Use reconnect() to replace the existing relationship."
28+
f"Node already has {detailed_description}. Use reconnect() to replace the existing relationship."
2829
)
2930

3031
def single(self) -> Optional["StructuredNode"]:
@@ -111,13 +112,14 @@ class One(RelationshipManager):
111112

112113
def check_cardinality(self, node: "StructuredNode") -> None:
113114
if self.__len__():
115+
detailed_description = str(self)
114116
if get_config().soft_cardinality_check:
115117
print(
116-
f"Cardinality violation detected : Node already has one relationship of type {self.definition['relation_type']}, should not connect more. Soft check is enabled so the relationship will be created. Note that strict check will be enabled by default in version 6.0"
118+
f"Cardinality violation detected : Node already has {detailed_description}, should not connect more. Soft check is enabled so the relationship will be created."
117119
)
118120
else:
119121
raise AttemptedCardinalityViolation(
120-
f"Node already has one relationship of type {self.definition['relation_type']}. Use reconnect() to replace the existing relationship."
122+
f"Node already has {detailed_description}. Use reconnect() to replace the existing relationship."
121123
)
122124

123125
def single(self) -> "StructuredNode":

test/async_/test_cardinality.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,15 @@ async def test_cardinality_zero_or_one():
125125
assert single_driver.version == 1
126126

127127
j = await ScrewDriver(version=2).save()
128-
with raises(AttemptedCardinalityViolation):
128+
with raises(AttemptedCardinalityViolation) as exc_info:
129129
await m.driver.connect(j)
130130

131+
error_message = str(exc_info.value)
132+
assert (
133+
f"Node already has zero or one relationship in a outgoing direction of type HAS_SCREWDRIVER on node ({m.element_id}) of class 'Monkey'. Use reconnect() to replace the existing relationship."
134+
== error_message
135+
)
136+
131137
await m.driver.reconnect(h, j)
132138
single_driver = await m.driver.single()
133139
assert single_driver.version == 2
@@ -166,9 +172,12 @@ async def test_cardinality_one_or_more():
166172
cars = await m.car.all()
167173
assert len(cars) == 1
168174

169-
with raises(AttemptedCardinalityViolation):
175+
with raises(AttemptedCardinalityViolation) as exc_info:
170176
await m.car.disconnect(c)
171177

178+
error_message = str(exc_info.value)
179+
assert "One or more expected" == error_message
180+
172181
d = await Car(version=3).save()
173182
await m.car.connect(d)
174183
cars = await m.car.all()
@@ -202,9 +211,15 @@ async def test_cardinality_one():
202211
assert single_toothbrush.name == "Jim"
203212

204213
x = await ToothBrush(name="Jim").save()
205-
with raises(AttemptedCardinalityViolation):
214+
with raises(AttemptedCardinalityViolation) as exc_info:
206215
await m.toothbrush.connect(x)
207216

217+
error_message = str(exc_info.value)
218+
assert (
219+
f"Node already has one relationship in a outgoing direction of type HAS_TOOTHBRUSH on node ({m.element_id}) of class 'Monkey'. Use reconnect() to replace the existing relationship."
220+
== error_message
221+
)
222+
208223
with raises(AttemptedCardinalityViolation):
209224
await m.toothbrush.disconnect(b)
210225

@@ -266,7 +281,6 @@ async def test_relationship_from_one_cardinality_enforced():
266281
console_output = stream.getvalue()
267282
assert "Cardinality violation detected" in console_output
268283
assert "Soft check is enabled so the relationship will be created" in console_output
269-
assert "strict check will be enabled by default in version 6.0" in console_output
270284

271285
config.soft_cardinality_check = False
272286

@@ -303,7 +317,6 @@ async def test_relationship_from_zero_or_one_cardinality_enforced():
303317
console_output = stream.getvalue()
304318
assert "Cardinality violation detected" in console_output
305319
assert "Soft check is enabled so the relationship will be created" in console_output
306-
assert "strict check will be enabled by default in version 6.0" in console_output
307320

308321
config.soft_cardinality_check = False
309322

@@ -363,6 +376,5 @@ async def test_bidirectional_cardinality_validation():
363376
console_output = stream.getvalue()
364377
assert "Cardinality violation detected" in console_output
365378
assert "Soft check is enabled so the relationship will be created" in console_output
366-
assert "strict check will be enabled by default in version 6.0" in console_output
367379

368380
config.soft_cardinality_check = False

test/sync_/test_cardinality.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,15 @@ def test_cardinality_zero_or_one():
121121
assert single_driver.version == 1
122122

123123
j = ScrewDriver(version=2).save()
124-
with raises(AttemptedCardinalityViolation):
124+
with raises(AttemptedCardinalityViolation) as exc_info:
125125
m.driver.connect(j)
126126

127+
error_message = str(exc_info.value)
128+
assert (
129+
f"Node already has zero or one relationship in a outgoing direction of type HAS_SCREWDRIVER on node ({m.element_id}) of class 'Monkey'. Use reconnect() to replace the existing relationship."
130+
== error_message
131+
)
132+
127133
m.driver.reconnect(h, j)
128134
single_driver = m.driver.single()
129135
assert single_driver.version == 2
@@ -162,9 +168,12 @@ def test_cardinality_one_or_more():
162168
cars = m.car.all()
163169
assert len(cars) == 1
164170

165-
with raises(AttemptedCardinalityViolation):
171+
with raises(AttemptedCardinalityViolation) as exc_info:
166172
m.car.disconnect(c)
167173

174+
error_message = str(exc_info.value)
175+
assert "One or more expected" == error_message
176+
168177
d = Car(version=3).save()
169178
m.car.connect(d)
170179
cars = m.car.all()
@@ -198,9 +207,15 @@ def test_cardinality_one():
198207
assert single_toothbrush.name == "Jim"
199208

200209
x = ToothBrush(name="Jim").save()
201-
with raises(AttemptedCardinalityViolation):
210+
with raises(AttemptedCardinalityViolation) as exc_info:
202211
m.toothbrush.connect(x)
203212

213+
error_message = str(exc_info.value)
214+
assert (
215+
f"Node already has one relationship in a outgoing direction of type HAS_TOOTHBRUSH on node ({m.element_id}) of class 'Monkey'. Use reconnect() to replace the existing relationship."
216+
== error_message
217+
)
218+
204219
with raises(AttemptedCardinalityViolation):
205220
m.toothbrush.disconnect(b)
206221

@@ -262,7 +277,6 @@ def test_relationship_from_one_cardinality_enforced():
262277
console_output = stream.getvalue()
263278
assert "Cardinality violation detected" in console_output
264279
assert "Soft check is enabled so the relationship will be created" in console_output
265-
assert "strict check will be enabled by default in version 6.0" in console_output
266280

267281
config.soft_cardinality_check = False
268282

@@ -299,7 +313,6 @@ def test_relationship_from_zero_or_one_cardinality_enforced():
299313
console_output = stream.getvalue()
300314
assert "Cardinality violation detected" in console_output
301315
assert "Soft check is enabled so the relationship will be created" in console_output
302-
assert "strict check will be enabled by default in version 6.0" in console_output
303316

304317
config.soft_cardinality_check = False
305318

@@ -359,6 +372,5 @@ def test_bidirectional_cardinality_validation():
359372
console_output = stream.getvalue()
360373
assert "Cardinality violation detected" in console_output
361374
assert "Soft check is enabled so the relationship will be created" in console_output
362-
assert "strict check will be enabled by default in version 6.0" in console_output
363375

364376
config.soft_cardinality_check = False

0 commit comments

Comments
 (0)