mirror of
https://github.com/getredash/redash.git
synced 2026-03-22 01:00:14 -04:00
* Sanitize NaN, Infinite, -Infinite causing error when saving as PostgreSQL JSON #7339 (2nd try) * Move json nsanitaize to on the top of json_dumps * Fix comment
32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
from redash.utils import json_dumps, json_loads
|
|
from tests import BaseTestCase
|
|
|
|
|
|
class TestJsonDumps(BaseTestCase):
|
|
"""
|
|
NaN, Inf, and -Inf are sanitized to None.
|
|
"""
|
|
|
|
def test_data_with_nan_is_sanitized(self):
|
|
input_data = {
|
|
"columns": [
|
|
{"name": "_col0", "friendly_name": "_col0", "type": "float"},
|
|
{"name": "_col1", "friendly_name": "_col1", "type": "float"},
|
|
{"name": "_col2", "friendly_name": "_col1", "type": "float"},
|
|
{"name": "_col3", "friendly_name": "_col1", "type": "float"},
|
|
],
|
|
"rows": [{"_col0": 1.0, "_col1": float("nan"), "_col2": float("inf"), "_col3": float("-inf")}],
|
|
}
|
|
expected_output_data = {
|
|
"columns": [
|
|
{"name": "_col0", "friendly_name": "_col0", "type": "float"},
|
|
{"name": "_col1", "friendly_name": "_col1", "type": "float"},
|
|
{"name": "_col2", "friendly_name": "_col1", "type": "float"},
|
|
{"name": "_col3", "friendly_name": "_col1", "type": "float"},
|
|
],
|
|
"rows": [{"_col0": 1.0, "_col1": None, "_col2": None, "_col3": None}],
|
|
}
|
|
json_data = json_dumps(input_data)
|
|
actual_output_data = json_loads(json_data)
|
|
self.assertEquals(actual_output_data, expected_output_data)
|