Skip to content

Commit 70f18ca

Browse files
Added tests for leading, trailing and embedded extra underscore errors
1 parent 82f9bb1 commit 70f18ca

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

packages/python/plotly/plotly/tests/test_core/test_errors/test_dict_path_errors.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,174 @@ def test_subscript_error_exception_types(some_fig):
423423
some_fig.update_layout(width_yo=100)
424424
with pytest.raises(KeyError):
425425
yo = some_fig["layout_width_yo"]
426+
427+
428+
def form_error_string(call, exception, subs):
429+
"""
430+
call is a function that raises exception.
431+
exception is an exception class, e.g., KeyError.
432+
subs is a list of replacements to be performed on the exception string. Each
433+
replacement is only performed once on the exception string so the
434+
replacement of multiple occurences of a pattern is specified by repeating a
435+
(pattern,relacement) pair in the list.
436+
returns modified exception string
437+
"""
438+
raised = False
439+
try:
440+
call()
441+
except exception as e:
442+
raised = True
443+
msg = e.args[0]
444+
for pat, rep in subs:
445+
msg = msg.replace(pat, rep, 1)
446+
assert raised
447+
return msg
448+
449+
450+
def check_error_string(call, exception, correct_str, subs):
451+
raised = False
452+
try:
453+
call()
454+
except exception as e:
455+
raised = True
456+
msg = e.args[0]
457+
for pat, rep in subs:
458+
msg = msg.replace(pat, rep, 1)
459+
assert msg == correct_str
460+
assert raised
461+
462+
463+
def test_leading_underscore_errors(some_fig):
464+
# get error string but alter it to form the final expected string
465+
def _raise_bad_property_path_form():
466+
some_fig.update_layout(bogus=7)
467+
468+
def _raise_bad_property_path_real():
469+
some_fig.update_layout(_hey_yall=7)
470+
471+
correct_err_str = form_error_string(
472+
_raise_bad_property_path_form,
473+
ValueError,
474+
[("bogus", "_"), ("bogus", "_hey_yall"), ("^^^^^", "^")],
475+
)
476+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
477+
478+
479+
def test_trailing_underscore_errors(some_fig):
480+
# get error string but alter it to form the final expected string
481+
def _raise_bad_property_path_form():
482+
some_fig.update_layout(title_bogus="hi")
483+
484+
def _raise_bad_property_path_real():
485+
some_fig.update_layout(title_text_="hi")
486+
487+
correct_err_str = form_error_string(
488+
_raise_bad_property_path_form,
489+
ValueError,
490+
[("bogus", "text_"), ("title_bogus", "title_text_")],
491+
)
492+
# no need to replace ^^^^^ because bogus and text_ are same length
493+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
494+
495+
496+
def test_embedded_underscore_errors(some_fig):
497+
# get error string but alter it to form the final expected string
498+
def _raise_bad_property_path_form():
499+
some_fig.update_layout(title_bogus_family="hi")
500+
501+
def _raise_bad_property_path_real():
502+
some_fig.update_layout(title_font__family="hi")
503+
504+
correct_err_str = form_error_string(
505+
_raise_bad_property_path_form,
506+
ValueError,
507+
[("bogus", "font_"), ("title_bogus_family", "title_font__family")],
508+
)
509+
# no need to replace ^^^^^ because bogus and font_ are same length
510+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
511+
512+
513+
def test_solo_underscore_errors(some_fig):
514+
# get error string but alter it to form the final expected string
515+
def _raise_bad_property_path_form():
516+
some_fig.update_layout(bogus="hi")
517+
518+
def _raise_bad_property_path_real():
519+
some_fig.update_layout(_="hi")
520+
521+
correct_err_str = form_error_string(
522+
_raise_bad_property_path_form,
523+
ValueError,
524+
[("bogus", "_"), ("bogus", "_"), ("^^^^^", "^")],
525+
)
526+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
527+
528+
529+
def test_repeated_underscore_errors(some_fig):
530+
# get error string but alter it to form the final expected string
531+
def _raise_bad_property_path_form():
532+
some_fig.update_layout(bogus="hi")
533+
534+
def _raise_bad_property_path_real():
535+
some_fig.update_layout(__="hi")
536+
537+
correct_err_str = form_error_string(
538+
_raise_bad_property_path_form,
539+
ValueError,
540+
[("bogus", "__"), ("bogus", "__"), ("^^^^^", "^^")],
541+
)
542+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
543+
544+
545+
def test_leading_underscore_errors_dots_and_subscripts(some_fig):
546+
# get error string but alter it to form the final expected string
547+
some_fig.add_annotation(text="hi")
548+
549+
def _raise_bad_property_path_form():
550+
some_fig["layout.annotations[0].bogus_family"] = "hi"
551+
552+
def _raise_bad_property_path_real():
553+
some_fig["layout.annotations[0]._font_family"] = "hi"
554+
555+
correct_err_str = form_error_string(
556+
_raise_bad_property_path_form,
557+
ValueError,
558+
[("bogus", "_"), ("bogus", "_font"), ("^^^^^", "^")],
559+
)
560+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
561+
562+
563+
def test_trailing_underscore_errors_dots_and_subscripts(some_fig):
564+
# get error string but alter it to form the final expected string
565+
some_fig.add_annotation(text="hi")
566+
567+
def _raise_bad_property_path_form():
568+
some_fig["layout.annotations[0].font_bogusey"] = "hi"
569+
570+
def _raise_bad_property_path_real():
571+
some_fig["layout.annotations[0].font_family_"] = "hi"
572+
573+
correct_err_str = form_error_string(
574+
_raise_bad_property_path_form,
575+
ValueError,
576+
[("bogusey", "family_"), ("bogusey", "family_")],
577+
)
578+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])
579+
580+
581+
def test_repeated_underscore_errors_dots_and_subscripts(some_fig):
582+
# get error string but alter it to form the final expected string
583+
some_fig.add_annotation(text="hi")
584+
585+
def _raise_bad_property_path_form():
586+
some_fig["layout.annotations[0].bogus_family"] = "hi"
587+
588+
def _raise_bad_property_path_real():
589+
some_fig["layout.annotations[0].font__family"] = "hi"
590+
591+
correct_err_str = form_error_string(
592+
_raise_bad_property_path_form,
593+
ValueError,
594+
[("bogus", "font_"), ("bogus", "font_")],
595+
)
596+
check_error_string(_raise_bad_property_path_real, ValueError, correct_err_str, [])

0 commit comments

Comments
 (0)