๐Ÿ“ฆ apache / superset

๐Ÿ“„ csv_tests.py ยท 320 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.


import pandas as pd
import pyarrow as pa
import pytest  # noqa: F401
from pandas.api.types import is_datetime64_any_dtype

from superset.utils import csv, json
from superset.utils.core import GenericDataType
from superset.utils.csv import (
    df_to_escaped_csv,
    get_chart_dataframe,
)


def test_escape_value():
    result = csv.escape_value("value")
    assert result == "value"

    result = csv.escape_value("-10")
    assert result == "-10"

    result = csv.escape_value("@value")
    assert result == "'@value"

    result = csv.escape_value("+value")
    assert result == "'+value"

    result = csv.escape_value("-value")
    assert result == "'-value"

    result = csv.escape_value("=value")
    assert result == "'=value"

    result = csv.escape_value("|value")
    assert result == r"'\|value"

    result = csv.escape_value("%value")
    assert result == "'%value"

    result = csv.escape_value("=cmd|' /C calc'!A0")
    assert result == r"'=cmd\|' /C calc'!A0"

    result = csv.escape_value('""=10+2')
    assert result == '\'""=10+2'

    result = csv.escape_value(" =10+2")
    assert result == "' =10+2"


def fake_get_chart_csv_data_none(chart_url, auth_cookies=None):
    return None


def fake_get_chart_csv_data_empty(chart_url, auth_cookies=None):
    # Return JSON with empty data so that the resulting DataFrame is empty
    fake_result = {
        "result": [{"data": {}, "coltypes": [], "colnames": [], "indexnames": []}]
    }
    return json.dumps(fake_result).encode("utf-8")


def fake_get_chart_csv_data_valid(chart_url, auth_cookies=None):
    # Return JSON with non-temporal data and valid indexnames so that they are used.
    fake_result = {
        "result": [
            {
                "data": {"col1": [1, 2], "col2": ["a", "b"]},
                "coltypes": [GenericDataType.NUMERIC, GenericDataType.STRING],
                "colnames": ["col1", "col2"],
                # Provide two index names so that a MultiIndex is built.
                "indexnames": ["idx1", "idx2"],
            }
        ]
    }
    return json.dumps(fake_result).encode("utf-8")


def fake_get_chart_csv_data_temporal(chart_url, auth_cookies=None):
    """
    Return JSON with a temporal column and valid indexnames
    so that a MultiIndex is built.
    """
    fake_result = {
        "result": [
            {
                "data": {"date": [1609459200000, 1612137600000], "val": [10, 20]},
                "coltypes": [GenericDataType.TEMPORAL, GenericDataType.NUMERIC],
                "colnames": ["date", "val"],
                # Provide two index names so a MultiIndex is built.
                "indexnames": [0, 1],
            }
        ]
    }
    return json.dumps(fake_result).encode("utf-8")


def fake_get_chart_csv_data_hierarchical(chart_url, auth_cookies=None):
    # Return JSON with hierarchical column (list-based) and matching index names.
    fake_result = {
        "result": [
            {
                "data": {"a": [1, 2]},
                "coltypes": [GenericDataType.NUMERIC],
                "colnames": [["level1", "a"]],
                # Provide two index tuples for two rows
                "indexnames": [["idx"], ["idx"]],
            }
        ]
    }
    return json.dumps(fake_result).encode("utf-8")


def fake_get_chart_csv_data_with_na_values(chart_url, auth_cookies=None):
    # Return JSON with data containing "NA" string value that will be treated as null
    fake_result = {
        "result": [
            {
                "data": {"first_name": ["Jeff", "Alice"], "last_name": ["Smith", "NA"]},
                "coltypes": [GenericDataType.STRING, GenericDataType.STRING],
                "colnames": ["first_name", "last_name"],
                "indexnames": ["idx1", "idx2"],
            }
        ]
    }
    return json.dumps(fake_result).encode("utf-8")


def test_df_to_escaped_csv():
    df = pd.DataFrame(
        data={
            "value": [
                "a",
                "col_a",
                "=func()",
                "-10",
                "=cmd|' /C calc'!A0",
                '""=b',
                " =a",
                "\x00",
            ]
        }
    )

    escaped_csv_str = df_to_escaped_csv(
        df,
        encoding="utf8",
        index=False,
        header=False,
    )

    escaped_csv_rows = [row.split(",") for row in escaped_csv_str.strip().split("\n")]

    assert escaped_csv_rows == [
        ["a"],
        ["col_a"],
        ["'=func()"],
        ["-10"],
        [r"'=cmd\\|' /C calc'!A0"],
        ['"\'""""=b"'],
        ["' =a"],
        ["\x00"],
    ]

    df = pa.array([1, None]).to_pandas(integer_object_nulls=True).to_frame()
    assert df_to_escaped_csv(df, encoding="utf8", index=False) == '0\n1\n""\n'


def test_get_chart_dataframe_returns_none_when_no_content(
    monkeypatch: pytest.MonkeyPatch,
):
    monkeypatch.setattr(csv, "get_chart_csv_data", fake_get_chart_csv_data_none)
    result = get_chart_dataframe("http://dummy-url")
    assert result is None


def test_get_chart_dataframe_returns_none_for_empty_data(
    monkeypatch: pytest.MonkeyPatch,
):
    monkeypatch.setattr(csv, "get_chart_csv_data", fake_get_chart_csv_data_empty)
    result = get_chart_dataframe("http://dummy-url")
    # When data is empty, the function should return None
    assert result is None


def test_get_chart_dataframe_valid_non_temporal(monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr(csv, "get_chart_csv_data", fake_get_chart_csv_data_valid)
    df = get_chart_dataframe("http://dummy-url")
    assert df is not None

    expected_columns = pd.MultiIndex.from_tuples([("col1",), ("col2",)])
    pd.testing.assert_index_equal(df.columns, expected_columns)

    expected_index = pd.MultiIndex.from_tuples([("idx1",), ("idx2",)])
    pd.testing.assert_index_equal(df.index, expected_index)

    pd.testing.assert_series_equal(
        df[("col1",)], pd.Series([1, 2], name=("col1",), index=df.index)
    )
    pd.testing.assert_series_equal(
        df[("col2",)], pd.Series(["a", "b"], name=("col2",), index=df.index)
    )
    markdown_str = df.to_markdown()
    expected_markdown_str = """
|           |   ('col1',) | ('col2',)   |
|:----------|------------:|:------------|
| ('idx1',) |           1 | a           |
| ('idx2',) |           2 | b           |
"""
    assert markdown_str.strip() == expected_markdown_str.strip()


def test_get_chart_dataframe_valid_temporal(monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr(csv, "get_chart_csv_data", fake_get_chart_csv_data_temporal)
    df = get_chart_dataframe("http://dummy-url")
    expected_columns = pd.MultiIndex.from_tuples([("date",), ("val",)])
    assert df is not None
    pd.testing.assert_index_equal(df.columns, expected_columns)

    expected_index = pd.MultiIndex.from_tuples([(0,), (1,)])
    pd.testing.assert_index_equal(df.index, expected_index)

    assert is_datetime64_any_dtype(df[("date",)])
    expected_dates = pd.to_datetime([1609459200000, 1612137600000], unit="ms").astype(
        "datetime64[ms]"
    )
    actual_dates = df[("date",)].reset_index(drop=True)
    pd.testing.assert_series_equal(
        actual_dates, pd.Series(expected_dates, name=("date",)), check_names=False
    )
    pd.testing.assert_series_equal(
        df[("val",)], pd.Series([10, 20], name=("val",), index=df.index)
    )
    markdown_str = df.to_markdown()
    expected_markdown_str = """
|      | ('date',)           |   ('val',) |
|:-----|:--------------------|-----------:|
| (0,) | 2021-01-01 00:00:00 |         10 |
| (1,) | 2021-02-01 00:00:00 |         20 |
"""
    assert markdown_str.strip() == expected_markdown_str.strip()


def test_get_chart_dataframe_with_hierarchical_columns(monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr(csv, "get_chart_csv_data", fake_get_chart_csv_data_hierarchical)
    df = get_chart_dataframe("http://dummy-url")
    assert df is not None
    expected_columns = pd.MultiIndex.from_tuples([("level1", "a")])
    pd.testing.assert_index_equal(df.columns, expected_columns)

    expected_index = pd.MultiIndex.from_tuples([("idx",)] * len(df))
    pd.testing.assert_index_equal(df.index, expected_index)

    pd.testing.assert_series_equal(
        df[("level1", "a")], pd.Series([1, 2], name=("level1", "a"), index=df.index)
    )
    markdown_str = df.to_markdown()
    expected_markdown_str = """
|          |   ('level1', 'a') |
|:---------|------------------:|
| ('idx',) |                 1 |
| ('idx',) |                 2 |
"""
    assert markdown_str.strip() == expected_markdown_str.strip()


def test_get_chart_dataframe_preserves_na_string_values(
    monkeypatch: pytest.MonkeyPatch,
):
    """
    Test that get_chart_dataframe currently preserves rows containing "NA"
    string values.
    This test verifies the existing behavior before implementing custom NA handling.
    """
    monkeypatch.setattr(
        csv, "get_chart_csv_data", fake_get_chart_csv_data_with_na_values
    )
    df = get_chart_dataframe("http://dummy-url")
    assert df is not None

    # Verify the DataFrame structure
    expected_columns = pd.MultiIndex.from_tuples([("first_name",), ("last_name",)])
    pd.testing.assert_index_equal(df.columns, expected_columns)

    expected_index = pd.MultiIndex.from_tuples([("idx1",), ("idx2",)])
    pd.testing.assert_index_equal(df.index, expected_index)

    # Check that we have both rows initially
    assert len(df) == 2

    # Verify the data contains the "NA" string value (not converted to NaN)
    pd.testing.assert_series_equal(
        df[("first_name",)],
        pd.Series(["Jeff", "Alice"], name=("first_name",), index=df.index),
    )
    pd.testing.assert_series_equal(
        df[("last_name",)],
        pd.Series(["Smith", "NA"], name=("last_name",), index=df.index),
    )

    last_name_values = df[("last_name",)].values
    assert last_name_values[0] == "Smith"
    assert last_name_values[1] == "NA"