27
27
28
28
from flask import Response
29
29
from tests .integration_tests .conftest import with_feature_flags
30
+ from superset .charts .data .api import ChartDataRestApi
30
31
from superset .models .sql_lab import Query
31
32
from tests .integration_tests .base_tests import SupersetTestCase , test_client
32
33
from tests .integration_tests .annotation_layers .fixtures import create_annotation_layers
47
48
from superset .errors import SupersetErrorType
48
49
from superset .extensions import async_query_manager_factory , db
49
50
from superset .models .annotations import AnnotationLayer
50
- from superset .models .slice import Slice
51
51
from superset .superset_typing import AdhocColumn
52
52
from superset .utils .core import (
53
53
AnnotationType ,
@@ -88,7 +88,9 @@ def setUp(self) -> None:
88
88
BaseTestChartDataApi .query_context_payload_template = get_query_context (
89
89
"birth_names"
90
90
)
91
- self .query_context_payload = copy .deepcopy (self .query_context_payload_template )
91
+ self .query_context_payload = (
92
+ copy .deepcopy (self .query_context_payload_template ) or {}
93
+ )
92
94
93
95
def get_expected_row_count (self , client_id : str ) -> int :
94
96
start_date = datetime .now ()
@@ -124,7 +126,49 @@ def quote_name(self, name: str):
124
126
@pytest .mark .chart_data_flow
125
127
class TestPostChartDataApi (BaseTestChartDataApi ):
126
128
@pytest .mark .usefixtures ("load_birth_names_dashboard_with_slices" )
127
- def test_with_valid_qc__data_is_returned (self ):
129
+ def test__map_form_data_datasource_to_dataset_id (self ):
130
+ # arrange
131
+ self .query_context_payload ["datasource" ] = {"id" : 1 , "type" : "table" }
132
+ # act
133
+ response = ChartDataRestApi ._map_form_data_datasource_to_dataset_id (
134
+ ChartDataRestApi , self .query_context_payload
135
+ )
136
+ # assert
137
+ assert response == {"dataset_id" : 1 , "slice_id" : None }
138
+
139
+ # takes malformed content without raising an error
140
+ self .query_context_payload ["datasource" ] = "1__table"
141
+ # act
142
+ response = ChartDataRestApi ._map_form_data_datasource_to_dataset_id (
143
+ ChartDataRestApi , self .query_context_payload
144
+ )
145
+ # assert
146
+ assert response == {"dataset_id" : None , "slice_id" : None }
147
+
148
+ # takes a slice id
149
+ self .query_context_payload ["datasource" ] = None
150
+ self .query_context_payload ["form_data" ] = {"slice_id" : 1 }
151
+ # act
152
+ response = ChartDataRestApi ._map_form_data_datasource_to_dataset_id (
153
+ ChartDataRestApi , self .query_context_payload
154
+ )
155
+ # assert
156
+ assert response == {"dataset_id" : None , "slice_id" : 1 }
157
+
158
+ # takes missing slice id
159
+ self .query_context_payload ["datasource" ] = None
160
+ self .query_context_payload ["form_data" ] = {"foo" : 1 }
161
+ # act
162
+ response = ChartDataRestApi ._map_form_data_datasource_to_dataset_id (
163
+ ChartDataRestApi , self .query_context_payload
164
+ )
165
+ # assert
166
+ assert response == {"dataset_id" : None , "slice_id" : None }
167
+
168
+ @pytest .mark .usefixtures ("load_birth_names_dashboard_with_slices" )
169
+ @mock .patch ("superset.utils.decorators.g" )
170
+ def test_with_valid_qc__data_is_returned (self , mock_g ):
171
+ mock_g .logs_context = {}
128
172
# arrange
129
173
expected_row_count = self .get_expected_row_count ("client_id_1" )
130
174
# act
@@ -133,6 +177,9 @@ def test_with_valid_qc__data_is_returned(self):
133
177
assert rv .status_code == 200
134
178
self .assert_row_count (rv , expected_row_count )
135
179
180
+ # check that global logs decorator is capturing from form_data
181
+ assert isinstance (mock_g .logs_context .get ("dataset_id" ), int )
182
+
136
183
@staticmethod
137
184
def assert_row_count (rv : Response , expected_row_count : int ):
138
185
assert rv .json ["result" ][0 ]["rowcount" ] == expected_row_count
0 commit comments