1
1
from __future__ import annotations
2
2
3
3
import logging
4
- import re
5
4
import sys
6
5
from contextlib import ExitStack
7
6
from pathlib import Path
8
- from test .utils .exceptions import ExceptionChecker
7
+ from test .utils .outcome import ExceptionChecker , OutcomeChecker , OutcomePrimitive
9
8
from typing import TYPE_CHECKING , Any , Dict , Mapping , Optional , Set , Tuple , Type , Union
10
9
11
10
import pytest
@@ -374,7 +373,7 @@ def test_compute_qname(
374
373
manager_prefixes : Optional [Mapping [str , Namespace ]],
375
374
graph_prefixes : Optional [Mapping [str , Namespace ]],
376
375
store_prefixes : Optional [Mapping [str , Namespace ]],
377
- expected_result : Union [Tuple [str , URIRef , str ], Type [ Exception ], Exception ],
376
+ expected_result : OutcomePrimitive [Tuple [str , URIRef , str ]],
378
377
) -> None :
379
378
"""
380
379
:param uri: argument to compute_qname()
@@ -403,25 +402,13 @@ def test_compute_qname(
403
402
nm .bind (prefix , ns )
404
403
405
404
def check () -> None :
406
- catcher : Optional [pytest .ExceptionInfo [Exception ]] = None
407
- with ExitStack () as xstack :
408
- if isinstance (expected_result , type ) and issubclass (
409
- expected_result , Exception
410
- ):
411
- catcher = xstack .enter_context (pytest .raises (expected_result ))
412
- if isinstance (expected_result , Exception ):
413
- catcher = xstack .enter_context (pytest .raises (type (expected_result )))
405
+ checker = OutcomeChecker [Tuple [str , URIRef , str ]].from_primitive (
406
+ expected_result
407
+ )
408
+ with checker .context ():
414
409
actual_result = nm .compute_qname (uri , generate )
415
410
logging .debug ("actual_result = %s" , actual_result )
416
- if catcher is not None :
417
- assert catcher is not None
418
- assert catcher .value is not None
419
- if isinstance (expected_result , Exception ):
420
- assert re .match (expected_result .args [0 ], f"{ catcher .value } " )
421
- else :
422
- assert isinstance (expected_result , tuple )
423
- assert isinstance (actual_result , tuple )
424
- assert actual_result == expected_result
411
+ checker .check (actual_result )
425
412
426
413
check ()
427
414
# Run a second time to check caching
@@ -452,7 +439,7 @@ def test_compute_qname_strict(
452
439
generate : bool ,
453
440
bind_namespaces : _NamespaceSetString ,
454
441
additional_prefixes : Optional [Mapping [str , Namespace ]],
455
- expected_result : Union [Tuple [str , URIRef , str ], Type [ Exception ], Exception ],
442
+ expected_result : OutcomePrimitive [Tuple [str , str , str ]],
456
443
) -> None :
457
444
graph = Graph (bind_namespaces = bind_namespaces )
458
445
nm = graph .namespace_manager
@@ -462,25 +449,11 @@ def test_compute_qname_strict(
462
449
nm .bind (prefix , ns )
463
450
464
451
def check () -> None :
465
- catcher : Optional [pytest .ExceptionInfo [Exception ]] = None
466
- with ExitStack () as xstack :
467
- if isinstance (expected_result , type ) and issubclass (
468
- expected_result , Exception
469
- ):
470
- catcher = xstack .enter_context (pytest .raises (expected_result ))
471
- if isinstance (expected_result , Exception ):
472
- catcher = xstack .enter_context (pytest .raises (type (expected_result )))
452
+ checker = OutcomeChecker [Tuple [str , str , str ]].from_primitive (expected_result )
453
+ with checker .context ():
473
454
actual_result = nm .compute_qname_strict (uri , generate )
474
455
logging .debug ("actual_result = %s" , actual_result )
475
- if catcher is not None :
476
- assert catcher is not None
477
- assert catcher .value is not None
478
- if isinstance (expected_result , Exception ):
479
- assert re .match (expected_result .args [0 ], f"{ catcher .value } " )
480
- else :
481
- assert isinstance (expected_result , tuple )
482
- assert isinstance (actual_result , tuple )
483
- assert actual_result == expected_result
456
+ checker .check (actual_result )
484
457
485
458
check ()
486
459
# Run a second time to check caching
@@ -538,16 +511,15 @@ def test_nsm_function() -> NamespaceManager:
538
511
def test_expand_curie (
539
512
test_nsm_session : NamespaceManager ,
540
513
curie : str ,
541
- expected_result : Union [ ExceptionChecker , str ],
514
+ expected_result : OutcomePrimitive [ str ],
542
515
) -> None :
543
516
nsm = test_nsm_session
544
- with ExitStack () as xstack :
545
- if isinstance (expected_result , ExceptionChecker ):
546
- xstack .enter_context (expected_result )
547
- result = nsm .expand_curie (curie )
548
-
549
- if not isinstance (expected_result , ExceptionChecker ):
550
- assert URIRef (expected_result ) == result
517
+ if isinstance (expected_result , str ):
518
+ expected_result = URIRef (expected_result )
519
+ checker = OutcomeChecker [str ].from_primitive (expected_result )
520
+ with checker .context ():
521
+ actual_result = nsm .expand_curie (curie )
522
+ checker .check (actual_result )
551
523
552
524
553
525
@pytest .mark .parametrize (
@@ -578,7 +550,7 @@ def test_generate_curie(
578
550
test_nsm_function : NamespaceManager ,
579
551
uri : str ,
580
552
generate : Optional [bool ],
581
- expected_result : Union [ ExceptionChecker , str ],
553
+ expected_result : OutcomePrimitive [ str ],
582
554
) -> None :
583
555
"""
584
556
.. note::
@@ -587,13 +559,10 @@ def test_generate_curie(
587
559
effects and will modify the namespace manager.
588
560
"""
589
561
nsm = test_nsm_function
590
- with ExitStack () as xstack :
591
- if isinstance (expected_result , ExceptionChecker ):
592
- xstack .enter_context (expected_result )
562
+ checker = OutcomeChecker [str ].from_primitive (expected_result )
563
+ with checker .context ():
593
564
if generate is None :
594
- result = nsm .curie (uri )
565
+ actual_result = nsm .curie (uri )
595
566
else :
596
- result = nsm .curie (uri , generate = generate )
597
-
598
- if not isinstance (expected_result , ExceptionChecker ):
599
- assert expected_result == result
567
+ actual_result = nsm .curie (uri , generate = generate )
568
+ checker .check (actual_result )
0 commit comments