|
16 | 16 | # under the License. |
17 | 17 | from contextlib import contextmanager |
18 | 18 | from datetime import datetime |
| 19 | +from typing import Any |
19 | 20 | from unittest.mock import call, patch, PropertyMock |
20 | 21 |
|
21 | 22 | import pandas as pd |
22 | 23 | import pytest |
23 | 24 | from flask_appbuilder.security.sqla.models import User |
24 | 25 | from pandas import DataFrame |
| 26 | +from pandas.errors import DataError |
25 | 27 |
|
26 | 28 | from superset.common.query_object import QueryObject |
27 | 29 | from superset.connectors.sqla.models import SqlaTable |
@@ -672,6 +674,149 @@ def test_exec_post_processing_unknown_op_raises(app_context: None) -> None: |
672 | 674 | assert "nonexistent_op" in str(excinfo.value) |
673 | 675 |
|
674 | 676 |
|
| 677 | +def _raise_value_error(df: DataFrame) -> DataFrame: |
| 678 | + """A custom operation that fails from its own internals.""" |
| 679 | + raise ValueError("a fault inside the operator's own code") |
| 680 | + |
| 681 | + |
| 682 | +@pytest.mark.parametrize( |
| 683 | + "operation,options,expected", |
| 684 | + [ |
| 685 | + pytest.param( |
| 686 | + "diff", |
| 687 | + {"columns": {"y": "y"}, "periods": "not-an-int"}, |
| 688 | + "periods must be an integer", |
| 689 | + id="diff-periods", |
| 690 | + ), |
| 691 | + pytest.param( |
| 692 | + "rolling", |
| 693 | + {"rolling_type": "sum", "columns": {"y": "y"}, "window": "not-an-int"}, |
| 694 | + "window must be an integer", |
| 695 | + id="rolling-window", |
| 696 | + ), |
| 697 | + ], |
| 698 | +) |
| 699 | +def test_exec_post_processing_reports_a_malformed_option_as_a_bad_request( |
| 700 | + operation: str, options: dict[str, Any], expected: str |
| 701 | +) -> None: |
| 702 | + """ |
| 703 | + A malformed option is a bad request, whatever pandas raises for it. |
| 704 | +
|
| 705 | + ``ChartDataPostProcessingOperationSchema.options`` is an untyped |
| 706 | + ``fields.Dict``, so no option is validated before it reaches pandas. Left |
| 707 | + unwrapped these reach ``app.errorhandler(Exception)``, which answers 500. |
| 708 | + """ |
| 709 | + query_object = QueryObject( |
| 710 | + row_limit=10, |
| 711 | + post_processing=[{"operation": operation, "options": options}], |
| 712 | + ) |
| 713 | + |
| 714 | + with pytest.raises(InvalidPostProcessingError) as excinfo: |
| 715 | + query_object.exec_post_processing(DataFrame({"y": [1.0, 2.0, 3.0]})) |
| 716 | + |
| 717 | + assert expected in str(excinfo.value) |
| 718 | + assert f"`{operation}`" in str(excinfo.value) |
| 719 | + |
| 720 | + |
| 721 | +def test_exec_post_processing_names_a_column_the_result_does_not_have() -> None: |
| 722 | + """ |
| 723 | + A KeyError from a built-in operation names a column the options asked for. |
| 724 | +
|
| 725 | + ``str(KeyError)`` is only the repr'd key, which on its own reads as a bare |
| 726 | + quoted string, so the message says what the key was. |
| 727 | + """ |
| 728 | + query_object = QueryObject( |
| 729 | + row_limit=10, |
| 730 | + post_processing=[ |
| 731 | + {"operation": "rank", "options": {"metric": "does_not_exist"}} |
| 732 | + ], |
| 733 | + ) |
| 734 | + |
| 735 | + with pytest.raises(InvalidPostProcessingError) as excinfo: |
| 736 | + query_object.exec_post_processing(DataFrame({"y": [1.0, 2.0, 3.0]})) |
| 737 | + |
| 738 | + assert "does_not_exist" in str(excinfo.value) |
| 739 | + assert "not in the query result" in str(excinfo.value) |
| 740 | + assert "`rank`" in str(excinfo.value) |
| 741 | + |
| 742 | + |
| 743 | +@pytest.mark.parametrize( |
| 744 | + "raised", |
| 745 | + [ |
| 746 | + pytest.param( |
| 747 | + AttributeError("'str' object has no attribute 'items'"), id="attr" |
| 748 | + ), |
| 749 | + pytest.param(DataError("No numeric types to aggregate"), id="data"), |
| 750 | + pytest.param(TypeError("unsupported operand type(s)"), id="type"), |
| 751 | + ], |
| 752 | +) |
| 753 | +def test_exec_post_processing_wraps_the_remaining_pandas_errors( |
| 754 | + raised: Exception, |
| 755 | +) -> None: |
| 756 | + """The other types pandas raises for a bad option are bad requests too.""" |
| 757 | + query_object = QueryObject( |
| 758 | + row_limit=10, |
| 759 | + post_processing=[{"operation": "rank", "options": {"metric": "y"}}], |
| 760 | + ) |
| 761 | + |
| 762 | + with ( |
| 763 | + patch.object(pandas_postprocessing, "rank", side_effect=raised), |
| 764 | + pytest.raises(InvalidPostProcessingError) as excinfo, |
| 765 | + ): |
| 766 | + query_object.exec_post_processing(DataFrame({"y": [1.0]})) |
| 767 | + |
| 768 | + assert str(raised) in str(excinfo.value) |
| 769 | + |
| 770 | + |
| 771 | +def test_exec_post_processing_leaves_a_custom_operation_fault_unwrapped( |
| 772 | + app_context: None, |
| 773 | +) -> None: |
| 774 | + """ |
| 775 | + A failure inside a custom operation is a server fault, not a bad request. |
| 776 | +
|
| 777 | + An operation registered through ``EXTRA_PANDAS_POSTPROCESSING_OPS`` is the |
| 778 | + operator's own code, which this module cannot reason about, so its |
| 779 | + exceptions are left for the error handler rather than reported as the |
| 780 | + caller's mistake. |
| 781 | + """ |
| 782 | + query_object = QueryObject( |
| 783 | + row_limit=10, |
| 784 | + post_processing=[{"operation": "_raise_value_error"}], |
| 785 | + ) |
| 786 | + |
| 787 | + with ( |
| 788 | + patch.dict( |
| 789 | + "superset.common.query_object.current_app.config", |
| 790 | + {"EXTRA_PANDAS_POSTPROCESSING_OPS": [_raise_value_error]}, |
| 791 | + ), |
| 792 | + pytest.raises(ValueError, match="operator's own code") as excinfo, |
| 793 | + ): |
| 794 | + query_object.exec_post_processing(DataFrame({"y": [1.0]})) |
| 795 | + |
| 796 | + assert not isinstance(excinfo.value, InvalidPostProcessingError) |
| 797 | + |
| 798 | + |
| 799 | +def test_exec_post_processing_leaves_a_missing_dependency_unwrapped() -> None: |
| 800 | + """ |
| 801 | + A missing optional dependency is a deployment matter, not a bad request. |
| 802 | +
|
| 803 | + ``rolling`` with a ``win_type`` needs scipy, which is optional, so the |
| 804 | + ImportError pandas raises for it is deliberately not reported as 400. |
| 805 | + """ |
| 806 | + query_object = QueryObject( |
| 807 | + row_limit=10, |
| 808 | + post_processing=[{"operation": "rank", "options": {"metric": "y"}}], |
| 809 | + ) |
| 810 | + |
| 811 | + with ( |
| 812 | + patch.object( |
| 813 | + pandas_postprocessing, "rank", side_effect=ImportError("no scipy") |
| 814 | + ), |
| 815 | + pytest.raises(ImportError, match="no scipy"), |
| 816 | + ): |
| 817 | + query_object.exec_post_processing(DataFrame({"y": [1.0]})) |
| 818 | + |
| 819 | + |
675 | 820 | @pytest.mark.parametrize( |
676 | 821 | "shadow_name", |
677 | 822 | ["build_extra_ops_map", "utils", "geography", "Any", "Callable", "annotations"], |
|
0 commit comments