File tree Expand file tree Collapse file tree 2 files changed +18
-6
lines changed Expand file tree Collapse file tree 2 files changed +18
-6
lines changed Original file line number Diff line number Diff line change 14
14
no_arg = object ()
15
15
16
16
17
+ class MultipleObjectsReturned (Exception ):
18
+ """The requested object does not exist"""
19
+
20
+
21
+ class ObjectDoesNotExist (Exception ):
22
+ """The query returned multiple objects when only one was expected."""
23
+
24
+
17
25
def keygetter (
18
26
obj : Mapping [str , Any ],
19
27
path : str ,
@@ -522,9 +530,9 @@ def get(
522
530
) -> Optional [T ]:
523
531
objs = self .filter (matcher = matcher , ** kwargs )
524
532
if len (objs ) > 1 :
525
- raise Exception ( "Multiple objects returned" )
533
+ raise MultipleObjectsReturned ( )
526
534
elif len (objs ) == 0 :
527
535
if default == no_arg :
528
- raise Exception ( "No objects found" )
536
+ raise ObjectDoesNotExist ( )
529
537
return default
530
538
return objs [0 ]
Original file line number Diff line number Diff line change 3
3
4
4
import pytest
5
5
6
- from libvcs ._internal .query_list import QueryList
6
+ from libvcs ._internal .query_list import (
7
+ MultipleObjectsReturned ,
8
+ ObjectDoesNotExist ,
9
+ QueryList ,
10
+ )
7
11
8
12
9
13
@dataclasses .dataclass
@@ -269,16 +273,16 @@ def test_filter(
269
273
else :
270
274
assert qs .get (filter_expr ) == expected_result [0 ]
271
275
elif len (expected_result ) > 1 :
272
- with pytest .raises (Exception ) as e :
276
+ with pytest .raises (MultipleObjectsReturned ) as e :
273
277
if isinstance (filter_expr , dict ):
274
278
assert qs .get (** filter_expr ) == expected_result
275
279
else :
276
280
assert qs .get (filter_expr ) == expected_result
277
281
assert e .match ("Multiple objects returned" )
278
282
elif len (expected_result ) == 0 :
279
- with pytest .raises (Exception ) as e :
283
+ with pytest .raises (ObjectDoesNotExist ) as exc :
280
284
if isinstance (filter_expr , dict ):
281
285
assert qs .get (** filter_expr ) == expected_result
282
286
else :
283
287
assert qs .get (filter_expr ) == expected_result
284
- assert e .match ("No objects found" )
288
+ assert exc .match ("No objects found" )
You can’t perform that action at this time.
0 commit comments