2019-01-15 18:53:27 -05:00
|
|
|
# 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.
|
2020-02-06 19:45:37 -05:00
|
|
|
# isort:skip_file
|
2019-10-18 17:44:27 -04:00
|
|
|
import unittest
|
|
|
|
import uuid
|
2017-11-07 23:23:40 -05:00
|
|
|
from datetime import date, datetime, time, timedelta
|
2017-01-05 12:56:07 -05:00
|
|
|
from decimal import Decimal
|
2020-03-27 13:07:07 -04:00
|
|
|
import hashlib
|
2020-04-29 15:16:47 -04:00
|
|
|
import json
|
2020-03-27 13:07:07 -04:00
|
|
|
import os
|
2020-08-07 01:41:39 -04:00
|
|
|
import re
|
2019-10-31 10:11:48 -04:00
|
|
|
from unittest.mock import Mock, patch
|
2021-01-11 08:57:55 -05:00
|
|
|
from tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices
|
2017-01-05 12:56:07 -05:00
|
|
|
|
2019-10-18 17:44:27 -04:00
|
|
|
import numpy
|
2021-01-11 08:57:55 -05:00
|
|
|
import pytest
|
2020-04-29 15:16:47 -04:00
|
|
|
from flask import Flask, g
|
2020-07-13 10:21:02 -04:00
|
|
|
import marshmallow
|
2019-07-27 20:43:39 -04:00
|
|
|
from sqlalchemy.exc import ArgumentError
|
2017-01-05 12:56:07 -05:00
|
|
|
|
2020-02-06 19:45:37 -05:00
|
|
|
import tests.test_app
|
2019-07-27 20:43:39 -04:00
|
|
|
from superset import app, db, security_manager
|
2020-03-27 13:07:07 -04:00
|
|
|
from superset.exceptions import CertificateException, SupersetException
|
2020-07-04 15:51:43 -04:00
|
|
|
from superset.models.core import Database, Log
|
2020-09-02 19:03:25 -04:00
|
|
|
from superset.models.dashboard import Dashboard
|
|
|
|
from superset.models.slice import Slice
|
2018-10-16 20:59:34 -04:00
|
|
|
from superset.utils.core import (
|
2018-07-16 17:27:40 -04:00
|
|
|
base_json_conv,
|
2020-08-12 12:58:14 -04:00
|
|
|
cast_to_num,
|
2018-07-31 16:52:20 -04:00
|
|
|
convert_legacy_filters_into_adhoc,
|
2020-03-27 13:07:07 -04:00
|
|
|
create_ssl_cert_file,
|
2019-09-22 12:37:43 -04:00
|
|
|
format_timedelta,
|
2020-08-07 01:41:39 -04:00
|
|
|
get_form_data_token,
|
2020-03-13 15:18:22 -04:00
|
|
|
get_iterable,
|
2020-04-15 09:58:03 -04:00
|
|
|
get_email_address_list,
|
2019-07-27 20:43:39 -04:00
|
|
|
get_or_create_db,
|
2018-07-16 17:27:40 -04:00
|
|
|
get_since_until,
|
2019-07-12 11:58:44 -04:00
|
|
|
get_stacktrace,
|
2018-07-16 17:27:40 -04:00
|
|
|
json_int_dttm_ser,
|
|
|
|
json_iso_dttm_ser,
|
|
|
|
JSONEncodedDict,
|
|
|
|
memoized,
|
|
|
|
merge_extra_filters,
|
|
|
|
merge_request_params,
|
2020-03-27 13:07:07 -04:00
|
|
|
parse_ssl_cert,
|
2018-07-16 17:27:40 -04:00
|
|
|
parse_human_timedelta,
|
2019-05-08 01:37:44 -04:00
|
|
|
parse_js_uri_path_item,
|
2019-06-19 13:10:18 -04:00
|
|
|
parse_past_timedelta,
|
2019-09-17 17:04:14 -04:00
|
|
|
split,
|
2019-10-31 10:11:48 -04:00
|
|
|
TimeRangeEndpoint,
|
2018-07-16 17:27:40 -04:00
|
|
|
validate_json,
|
|
|
|
zlib_compress,
|
2019-08-27 17:23:40 -04:00
|
|
|
zlib_decompress,
|
2020-12-17 21:27:21 -05:00
|
|
|
datetime_eval,
|
2017-11-07 23:23:40 -05:00
|
|
|
)
|
2020-07-13 10:21:02 -04:00
|
|
|
from superset.utils import schema
|
2020-04-29 15:16:47 -04:00
|
|
|
from superset.views.utils import (
|
|
|
|
build_extra_filters,
|
|
|
|
get_form_data,
|
|
|
|
get_time_range_endpoints,
|
|
|
|
)
|
2019-11-20 10:47:06 -05:00
|
|
|
from tests.base_tests import SupersetTestCase
|
2017-01-05 12:56:07 -05:00
|
|
|
|
2020-03-27 13:07:07 -04:00
|
|
|
from .fixtures.certificates import ssl_certificate
|
|
|
|
|
2017-11-10 20:52:34 -05:00
|
|
|
|
2018-07-16 17:27:40 -04:00
|
|
|
def mock_parse_human_datetime(s):
|
2019-06-25 16:34:48 -04:00
|
|
|
if s == "now":
|
2019-05-31 09:55:26 -04:00
|
|
|
return datetime(2016, 11, 7, 9, 30, 10)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "today":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2016, 11, 7)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "yesterday":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2016, 11, 6)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "tomorrow":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2016, 11, 8)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "Last year":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2015, 11, 7)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "Last week":
|
2019-05-31 09:55:26 -04:00
|
|
|
return datetime(2015, 10, 31)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "Last 5 months":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2016, 6, 7)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "Next 5 months":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2017, 4, 7)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s in ["5 days", "5 days ago"]:
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2016, 11, 2)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "2018-01-01T00:00:00":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2018, 1, 1)
|
2019-06-25 16:34:48 -04:00
|
|
|
elif s == "2018-12-31T23:59:59":
|
2018-07-16 17:27:40 -04:00
|
|
|
return datetime(2018, 12, 31, 23, 59, 59)
|
|
|
|
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
def mock_to_adhoc(filt, expressionType="SIMPLE", clause="where"):
|
|
|
|
result = {"clause": clause.upper(), "expressionType": expressionType}
|
2018-07-31 16:52:20 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
if expressionType == "SIMPLE":
|
|
|
|
result.update(
|
|
|
|
{"comparator": filt["val"], "operator": filt["op"], "subject": filt["col"]}
|
|
|
|
)
|
|
|
|
elif expressionType == "SQL":
|
|
|
|
result.update({"sqlExpression": filt[clause]})
|
2018-07-31 16:52:20 -04:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-06-29 18:36:06 -04:00
|
|
|
class TestUtils(SupersetTestCase):
|
2017-01-05 12:56:07 -05:00
|
|
|
def test_json_int_dttm_ser(self):
|
|
|
|
dttm = datetime(2020, 1, 1)
|
|
|
|
ts = 1577836800000.0
|
|
|
|
assert json_int_dttm_ser(dttm) == ts
|
|
|
|
assert json_int_dttm_ser(date(2020, 1, 1)) == ts
|
|
|
|
assert json_int_dttm_ser(datetime(1970, 1, 1)) == 0
|
|
|
|
assert json_int_dttm_ser(date(1970, 1, 1)) == 0
|
|
|
|
assert json_int_dttm_ser(dttm + timedelta(milliseconds=1)) == (ts + 1)
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
2019-06-25 16:34:48 -04:00
|
|
|
json_int_dttm_ser("this is not a date")
|
2017-01-05 12:56:07 -05:00
|
|
|
|
|
|
|
def test_json_iso_dttm_ser(self):
|
|
|
|
dttm = datetime(2020, 1, 1)
|
|
|
|
dt = date(2020, 1, 1)
|
|
|
|
t = time()
|
|
|
|
assert json_iso_dttm_ser(dttm) == dttm.isoformat()
|
|
|
|
assert json_iso_dttm_ser(dt) == dt.isoformat()
|
|
|
|
assert json_iso_dttm_ser(t) == t.isoformat()
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
2019-06-25 16:34:48 -04:00
|
|
|
json_iso_dttm_ser("this is not a date")
|
2017-01-05 12:56:07 -05:00
|
|
|
|
|
|
|
def test_base_json_conv(self):
|
2017-02-08 14:52:58 -05:00
|
|
|
assert isinstance(base_json_conv(numpy.bool_(1)), bool) is True
|
|
|
|
assert isinstance(base_json_conv(numpy.int64(1)), int) is True
|
2020-01-10 13:24:03 -05:00
|
|
|
assert isinstance(base_json_conv(numpy.array([1, 2, 3])), list) is True
|
2017-02-08 14:52:58 -05:00
|
|
|
assert isinstance(base_json_conv(set([1])), list) is True
|
2019-06-25 16:34:48 -04:00
|
|
|
assert isinstance(base_json_conv(Decimal("1.0")), float) is True
|
2017-02-08 14:52:58 -05:00
|
|
|
assert isinstance(base_json_conv(uuid.uuid4()), str) is True
|
2019-09-22 12:37:43 -04:00
|
|
|
assert isinstance(base_json_conv(timedelta(0)), str) is True
|
2017-01-05 12:56:07 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.datetime")
|
2019-06-19 13:10:18 -04:00
|
|
|
def test_parse_human_timedelta(self, mock_datetime):
|
|
|
|
mock_datetime.now.return_value = datetime(2019, 4, 1)
|
|
|
|
mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(parse_human_timedelta("now"), timedelta(0))
|
|
|
|
self.assertEqual(parse_human_timedelta("1 year"), timedelta(366))
|
|
|
|
self.assertEqual(parse_human_timedelta("-1 year"), timedelta(-365))
|
2019-10-23 19:04:46 -04:00
|
|
|
self.assertEqual(parse_human_timedelta(None), timedelta(0))
|
2020-12-17 21:27:21 -05:00
|
|
|
self.assertEqual(
|
|
|
|
parse_human_timedelta("1 month", datetime(2019, 4, 1)), timedelta(30),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
parse_human_timedelta("1 month", datetime(2019, 5, 1)), timedelta(31),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
parse_human_timedelta("1 month", datetime(2019, 2, 1)), timedelta(28),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
parse_human_timedelta("-1 month", datetime(2019, 2, 1)), timedelta(-31),
|
|
|
|
)
|
2019-06-19 13:10:18 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.datetime")
|
2019-06-19 13:10:18 -04:00
|
|
|
def test_parse_past_timedelta(self, mock_datetime):
|
|
|
|
mock_datetime.now.return_value = datetime(2019, 4, 1)
|
|
|
|
mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(parse_past_timedelta("1 year"), timedelta(365))
|
|
|
|
self.assertEqual(parse_past_timedelta("-1 year"), timedelta(365))
|
|
|
|
self.assertEqual(parse_past_timedelta("52 weeks"), timedelta(364))
|
|
|
|
self.assertEqual(parse_past_timedelta("1 month"), timedelta(31))
|
2017-04-06 12:42:43 -04:00
|
|
|
|
|
|
|
def test_zlib_compression(self):
|
2017-11-14 00:06:51 -05:00
|
|
|
json_str = '{"test": 1}'
|
2017-04-06 12:42:43 -04:00
|
|
|
blob = zlib_compress(json_str)
|
2019-08-27 17:23:40 -04:00
|
|
|
got_str = zlib_decompress(blob)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(json_str, got_str)
|
2017-04-06 12:42:43 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2017-10-04 15:43:29 -04:00
|
|
|
def test_merge_extra_filters(self):
|
|
|
|
# does nothing if no extra filters
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"A": 1, "B": 2, "c": "test"}
|
2020-12-18 20:06:37 -05:00
|
|
|
expected = {**form_data, "adhoc_filters": [], "applied_time_extras": {}}
|
2017-10-04 15:43:29 -04:00
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-11-15 00:12:26 -05:00
|
|
|
# empty extra_filters
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"A": 1, "B": 2, "c": "test", "extra_filters": []}
|
2020-10-28 18:46:24 -04:00
|
|
|
expected = {
|
|
|
|
"A": 1,
|
|
|
|
"B": 2,
|
|
|
|
"c": "test",
|
|
|
|
"adhoc_filters": [],
|
|
|
|
"applied_time_extras": {},
|
|
|
|
}
|
2017-10-04 15:43:29 -04:00
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-10-04 15:43:29 -04:00
|
|
|
# copy over extra filters into empty filters
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {
|
|
|
|
"extra_filters": [
|
|
|
|
{"col": "a", "op": "in", "val": "someval"},
|
|
|
|
{"col": "B", "op": "==", "val": ["c1", "c2"]},
|
|
|
|
]
|
|
|
|
}
|
2018-07-31 16:52:20 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
2020-10-28 18:46:24 -04:00
|
|
|
],
|
|
|
|
"applied_time_extras": {},
|
2018-07-31 16:52:20 -04:00
|
|
|
}
|
2017-10-04 15:43:29 -04:00
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-10-04 15:43:29 -04:00
|
|
|
# adds extra filters to existing filters
|
2018-07-31 16:52:20 -04:00
|
|
|
form_data = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [
|
|
|
|
{"col": "a", "op": "in", "val": "someval"},
|
|
|
|
{"col": "B", "op": "==", "val": ["c1", "c2"]},
|
2018-07-31 16:52:20 -04:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{
|
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["G1", "g2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "!=",
|
|
|
|
"subject": "D",
|
|
|
|
}
|
2018-07-31 16:52:20 -04:00
|
|
|
],
|
|
|
|
}
|
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["G1", "g2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "!=",
|
|
|
|
"subject": "D",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
2020-10-28 18:46:24 -04:00
|
|
|
],
|
|
|
|
"applied_time_extras": {},
|
2018-07-31 16:52:20 -04:00
|
|
|
}
|
2017-10-04 15:43:29 -04:00
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-10-04 15:43:29 -04:00
|
|
|
# adds extra filters to existing filters and sets time options
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {
|
|
|
|
"extra_filters": [
|
|
|
|
{"col": "__time_range", "op": "in", "val": "1 year ago :"},
|
|
|
|
{"col": "__time_col", "op": "in", "val": "birth_year"},
|
|
|
|
{"col": "__time_grain", "op": "in", "val": "years"},
|
|
|
|
{"col": "A", "op": "like", "val": "hello"},
|
|
|
|
{"col": "__time_origin", "op": "in", "val": "now"},
|
|
|
|
{"col": "__granularity", "op": "in", "val": "90 seconds"},
|
|
|
|
]
|
|
|
|
}
|
2017-10-04 15:43:29 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{
|
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "hello",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "like",
|
|
|
|
"subject": "A",
|
|
|
|
}
|
2018-07-31 16:52:20 -04:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"time_range": "1 year ago :",
|
|
|
|
"granularity_sqla": "birth_year",
|
|
|
|
"time_grain_sqla": "years",
|
|
|
|
"granularity": "90 seconds",
|
|
|
|
"druid_time_origin": "now",
|
2020-10-28 18:46:24 -04:00
|
|
|
"applied_time_extras": {
|
|
|
|
"__time_range": "1 year ago :",
|
|
|
|
"__time_col": "birth_year",
|
|
|
|
"__time_grain": "years",
|
|
|
|
"__time_origin": "now",
|
|
|
|
"__granularity": "90 seconds",
|
|
|
|
},
|
2017-10-04 15:43:29 -04:00
|
|
|
}
|
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-10-04 15:43:29 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2017-11-11 00:33:31 -05:00
|
|
|
def test_merge_extra_filters_ignores_empty_filters(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {
|
|
|
|
"extra_filters": [
|
|
|
|
{"col": "a", "op": "in", "val": ""},
|
|
|
|
{"col": "B", "op": "==", "val": []},
|
|
|
|
]
|
|
|
|
}
|
2020-10-28 18:46:24 -04:00
|
|
|
expected = {"adhoc_filters": [], "applied_time_extras": {}}
|
2017-11-11 00:33:31 -05:00
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-11-11 00:33:31 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2017-12-20 19:22:43 -05:00
|
|
|
def test_merge_extra_filters_ignores_nones(self):
|
|
|
|
form_data = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{
|
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": None,
|
|
|
|
}
|
2017-12-20 19:22:43 -05:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [{"col": "B", "op": "==", "val": []}],
|
2017-12-20 19:22:43 -05:00
|
|
|
}
|
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{
|
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": None,
|
|
|
|
}
|
2020-10-28 18:46:24 -04:00
|
|
|
],
|
|
|
|
"applied_time_extras": {},
|
2017-12-20 19:22:43 -05:00
|
|
|
}
|
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-12-20 19:22:43 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2017-11-11 00:33:31 -05:00
|
|
|
def test_merge_extra_filters_ignores_equal_filters(self):
|
|
|
|
form_data = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [
|
|
|
|
{"col": "a", "op": "in", "val": "someval"},
|
|
|
|
{"col": "B", "op": "==", "val": ["c1", "c2"]},
|
2019-08-13 16:06:44 -04:00
|
|
|
{"col": "c", "op": "in", "val": ["c1", 1, None]},
|
2017-11-11 00:33:31 -05:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
2019-08-13 16:06:44 -04:00
|
|
|
{
|
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", 1, None],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "c",
|
|
|
|
},
|
2018-07-31 16:52:20 -04:00
|
|
|
],
|
|
|
|
}
|
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
2019-08-13 16:06:44 -04:00
|
|
|
{
|
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", 1, None],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "c",
|
|
|
|
},
|
2020-10-28 18:46:24 -04:00
|
|
|
],
|
|
|
|
"applied_time_extras": {},
|
2017-11-11 00:33:31 -05:00
|
|
|
}
|
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-11-11 00:33:31 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2017-11-11 00:33:31 -05:00
|
|
|
def test_merge_extra_filters_merges_different_val_types(self):
|
|
|
|
form_data = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [
|
|
|
|
{"col": "a", "op": "in", "val": ["g1", "g2"]},
|
|
|
|
{"col": "B", "op": "==", "val": ["c1", "c2"]},
|
2017-11-11 00:33:31 -05:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["g1", "g2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
2020-10-28 18:46:24 -04:00
|
|
|
],
|
|
|
|
"applied_time_extras": {},
|
2017-11-11 00:33:31 -05:00
|
|
|
}
|
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-11-11 00:33:31 -05:00
|
|
|
form_data = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [
|
|
|
|
{"col": "a", "op": "in", "val": "someval"},
|
|
|
|
{"col": "B", "op": "==", "val": ["c1", "c2"]},
|
2017-11-11 00:33:31 -05:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["g1", "g2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["g1", "g2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
2020-10-28 18:46:24 -04:00
|
|
|
],
|
|
|
|
"applied_time_extras": {},
|
2017-11-11 00:33:31 -05:00
|
|
|
}
|
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-11-11 00:33:31 -05:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2017-11-11 00:33:31 -05:00
|
|
|
def test_merge_extra_filters_adds_unequal_lists(self):
|
|
|
|
form_data = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [
|
|
|
|
{"col": "a", "op": "in", "val": ["g1", "g2", "g3"]},
|
|
|
|
{"col": "B", "op": "==", "val": ["c1", "c2", "c3"]},
|
2017-11-11 00:33:31 -05:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["g1", "g2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["g1", "g2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["g1", "g2", "g3"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": ["c1", "c2", "c3"],
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "B",
|
2018-07-31 16:52:20 -04:00
|
|
|
},
|
2020-10-28 18:46:24 -04:00
|
|
|
],
|
|
|
|
"applied_time_extras": {},
|
2017-11-11 00:33:31 -05:00
|
|
|
}
|
|
|
|
merge_extra_filters(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2017-11-11 00:33:31 -05:00
|
|
|
|
2019-11-21 00:07:08 -05:00
|
|
|
def test_merge_request_params_when_url_params_undefined(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"since": "2000", "until": "now"}
|
|
|
|
url_params = {"form_data": form_data, "dashboard_ids": "(1,2,3,4,5)"}
|
2018-03-08 16:19:41 -05:00
|
|
|
merge_request_params(form_data, url_params)
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertIn("url_params", form_data.keys())
|
|
|
|
self.assertIn("dashboard_ids", form_data["url_params"])
|
|
|
|
self.assertNotIn("form_data", form_data.keys())
|
2018-03-08 16:19:41 -05:00
|
|
|
|
2019-11-21 00:07:08 -05:00
|
|
|
def test_merge_request_params_when_url_params_predefined(self):
|
|
|
|
form_data = {
|
|
|
|
"since": "2000",
|
|
|
|
"until": "now",
|
|
|
|
"url_params": {"abc": "123", "dashboard_ids": "(1,2,3)"},
|
|
|
|
}
|
|
|
|
url_params = {"form_data": form_data, "dashboard_ids": "(1,2,3,4,5)"}
|
|
|
|
merge_request_params(form_data, url_params)
|
|
|
|
self.assertIn("url_params", form_data.keys())
|
|
|
|
self.assertIn("abc", form_data["url_params"])
|
2020-06-29 19:38:06 -04:00
|
|
|
self.assertEqual(
|
2019-11-21 00:07:08 -05:00
|
|
|
url_params["dashboard_ids"], form_data["url_params"]["dashboard_ids"]
|
|
|
|
)
|
|
|
|
|
2019-09-22 12:37:43 -04:00
|
|
|
def test_format_timedelta(self):
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(format_timedelta(timedelta(0)), "0:00:00")
|
|
|
|
self.assertEqual(format_timedelta(timedelta(days=1)), "1 day, 0:00:00")
|
|
|
|
self.assertEqual(format_timedelta(timedelta(minutes=-6)), "-0:06:00")
|
|
|
|
self.assertEqual(
|
2019-09-22 12:37:43 -04:00
|
|
|
format_timedelta(timedelta(0) - timedelta(days=1, hours=5, minutes=6)),
|
|
|
|
"-1 day, 5:06:00",
|
|
|
|
)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(
|
2019-09-22 12:37:43 -04:00
|
|
|
format_timedelta(timedelta(0) - timedelta(days=16, hours=4, minutes=3)),
|
|
|
|
"-16 days, 4:03:00",
|
|
|
|
)
|
|
|
|
|
2017-09-26 12:03:03 -04:00
|
|
|
def test_json_encoded_obj(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
obj = {"a": 5, "b": ["a", "g", 5]}
|
2017-09-26 12:03:03 -04:00
|
|
|
val = '{"a": 5, "b": ["a", "g", 5]}'
|
|
|
|
jsonObj = JSONEncodedDict()
|
2019-06-25 16:34:48 -04:00
|
|
|
resp = jsonObj.process_bind_param(obj, "dialect")
|
2017-09-26 12:03:03 -04:00
|
|
|
self.assertIn('"a": 5', resp)
|
|
|
|
self.assertIn('"b": ["a", "g", 5]', resp)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(jsonObj.process_result_value(val, "dialect"), obj)
|
2017-09-26 12:03:03 -04:00
|
|
|
|
|
|
|
def test_validate_json(self):
|
2020-07-13 10:21:02 -04:00
|
|
|
valid = '{"a": 5, "b": [1, 5, ["g", "h"]]}'
|
|
|
|
self.assertIsNone(validate_json(valid))
|
2017-09-26 12:03:03 -04:00
|
|
|
invalid = '{"a": 5, "b": [1, 5, ["g", "h]]}'
|
|
|
|
with self.assertRaises(SupersetException):
|
|
|
|
validate_json(invalid)
|
2017-12-17 13:35:00 -05:00
|
|
|
|
|
|
|
def test_memoized_on_functions(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
watcher = {"val": 0}
|
2017-12-17 13:35:00 -05:00
|
|
|
|
|
|
|
@memoized
|
|
|
|
def test_function(a, b, c):
|
2019-06-25 16:34:48 -04:00
|
|
|
watcher["val"] += 1
|
2017-12-17 13:35:00 -05:00
|
|
|
return a * b * c
|
2019-06-25 16:34:48 -04:00
|
|
|
|
2017-12-17 13:35:00 -05:00
|
|
|
result1 = test_function(1, 2, 3)
|
|
|
|
result2 = test_function(1, 2, 3)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(result1, result2)
|
|
|
|
self.assertEqual(watcher["val"], 1)
|
2017-12-17 13:35:00 -05:00
|
|
|
|
|
|
|
def test_memoized_on_methods(self):
|
|
|
|
class test_class:
|
|
|
|
def __init__(self, num):
|
|
|
|
self.num = num
|
|
|
|
self.watcher = 0
|
|
|
|
|
|
|
|
@memoized
|
|
|
|
def test_method(self, a, b, c):
|
|
|
|
self.watcher += 1
|
|
|
|
return a * b * c * self.num
|
|
|
|
|
|
|
|
instance = test_class(5)
|
|
|
|
result1 = instance.test_method(1, 2, 3)
|
|
|
|
result2 = instance.test_method(1, 2, 3)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(result1, result2)
|
|
|
|
self.assertEqual(instance.watcher, 1)
|
2017-12-17 13:35:00 -05:00
|
|
|
instance.num = 10
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(result2, instance.test_method(1, 2, 3))
|
2017-12-17 13:35:00 -05:00
|
|
|
|
|
|
|
def test_memoized_on_methods_with_watches(self):
|
|
|
|
class test_class:
|
|
|
|
def __init__(self, x, y):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.watcher = 0
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@memoized(watch=("x", "y"))
|
2017-12-17 13:35:00 -05:00
|
|
|
def test_method(self, a, b, c):
|
|
|
|
self.watcher += 1
|
|
|
|
return a * b * c * self.x * self.y
|
|
|
|
|
|
|
|
instance = test_class(3, 12)
|
|
|
|
result1 = instance.test_method(1, 2, 3)
|
|
|
|
result2 = instance.test_method(1, 2, 3)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(result1, result2)
|
|
|
|
self.assertEqual(instance.watcher, 1)
|
2017-12-17 13:35:00 -05:00
|
|
|
result3 = instance.test_method(2, 3, 4)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(instance.watcher, 2)
|
2017-12-17 13:35:00 -05:00
|
|
|
result4 = instance.test_method(2, 3, 4)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(instance.watcher, 2)
|
|
|
|
self.assertEqual(result3, result4)
|
2017-12-17 13:35:00 -05:00
|
|
|
self.assertNotEqual(result3, result1)
|
|
|
|
instance.x = 1
|
|
|
|
result5 = instance.test_method(2, 3, 4)
|
|
|
|
self.assertEqual(instance.watcher, 3)
|
|
|
|
self.assertNotEqual(result5, result4)
|
|
|
|
result6 = instance.test_method(2, 3, 4)
|
|
|
|
self.assertEqual(instance.watcher, 3)
|
|
|
|
self.assertEqual(result6, result5)
|
|
|
|
instance.x = 10
|
|
|
|
instance.y = 10
|
|
|
|
result7 = instance.test_method(2, 3, 4)
|
|
|
|
self.assertEqual(instance.watcher, 4)
|
|
|
|
self.assertNotEqual(result7, result6)
|
|
|
|
instance.x = 3
|
|
|
|
instance.y = 12
|
|
|
|
result8 = instance.test_method(1, 2, 3)
|
|
|
|
self.assertEqual(instance.watcher, 4)
|
|
|
|
self.assertEqual(result1, result8)
|
2018-07-16 17:27:40 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.parse_human_datetime", mock_parse_human_datetime)
|
2018-07-16 17:27:40 -04:00
|
|
|
def test_get_since_until(self):
|
2018-11-07 20:23:52 -05:00
|
|
|
result = get_since_until()
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = None, datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until(" : now")
|
2019-05-31 09:55:26 -04:00
|
|
|
expected = None, datetime(2016, 11, 7, 9, 30, 10)
|
2018-07-16 17:27:40 -04:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("yesterday : tomorrow")
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = datetime(2016, 11, 6), datetime(2016, 11, 8)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("2018-01-01T00:00:00 : 2018-12-31T23:59:59")
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = datetime(2018, 1, 1), datetime(2018, 12, 31, 23, 59, 59)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("Last year")
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = datetime(2015, 11, 7), datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2020-12-17 21:27:21 -05:00
|
|
|
result = get_since_until("Last quarter")
|
|
|
|
expected = datetime(2016, 8, 7), datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("Last 5 months")
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = datetime(2016, 6, 7), datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("Next 5 months")
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = datetime(2016, 11, 7), datetime(2017, 4, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until(since="5 days")
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = datetime(2016, 11, 2), datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until(since="5 days ago", until="tomorrow")
|
2018-07-16 17:27:40 -04:00
|
|
|
expected = datetime(2016, 11, 2), datetime(2016, 11, 8)
|
|
|
|
self.assertEqual(result, expected)
|
2018-07-31 16:52:20 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until(time_range="yesterday : tomorrow", time_shift="1 day")
|
2018-11-07 20:23:52 -05:00
|
|
|
expected = datetime(2016, 11, 5), datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until(time_range="5 days : now")
|
2019-05-31 09:55:26 -04:00
|
|
|
expected = datetime(2016, 11, 2), datetime(2016, 11, 7, 9, 30, 10)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("Last week", relative_end="now")
|
2019-05-31 09:55:26 -04:00
|
|
|
expected = datetime(2016, 10, 31), datetime(2016, 11, 7, 9, 30, 10)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("Last week", relative_start="now")
|
2019-05-31 09:55:26 -04:00
|
|
|
expected = datetime(2016, 10, 31, 9, 30, 10), datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
result = get_since_until("Last week", relative_start="now", relative_end="now")
|
2019-05-31 09:55:26 -04:00
|
|
|
expected = datetime(2016, 10, 31, 9, 30, 10), datetime(2016, 11, 7, 9, 30, 10)
|
2018-11-19 14:29:46 -05:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2021-01-06 09:37:35 -05:00
|
|
|
result = get_since_until("previous calendar week")
|
2021-01-06 13:09:13 -05:00
|
|
|
expected = datetime(2016, 10, 31, 0, 0, 0), datetime(2016, 11, 7, 0, 0, 0)
|
2021-01-06 09:37:35 -05:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = get_since_until("previous calendar month")
|
2021-01-06 13:09:13 -05:00
|
|
|
expected = datetime(2016, 10, 1, 0, 0, 0), datetime(2016, 11, 1, 0, 0, 0)
|
2021-01-06 09:37:35 -05:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = get_since_until("previous calendar year")
|
2021-01-06 13:09:13 -05:00
|
|
|
expected = datetime(2015, 1, 1, 0, 0, 0), datetime(2016, 1, 1, 0, 0, 0)
|
2021-01-06 09:37:35 -05:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2018-11-07 20:23:52 -05:00
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 16:34:48 -04:00
|
|
|
get_since_until(time_range="tomorrow : yesterday")
|
2018-11-07 20:23:52 -05:00
|
|
|
|
2020-12-17 21:27:21 -05:00
|
|
|
@patch("superset.utils.core.parse_human_datetime", mock_parse_human_datetime)
|
|
|
|
def test_datetime_eval(self):
|
|
|
|
result = datetime_eval("datetime('now')")
|
|
|
|
expected = datetime(2016, 11, 7, 9, 30, 10)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetime('today' )")
|
|
|
|
expected = datetime(2016, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
# Parse compact arguments spelling
|
|
|
|
result = datetime_eval("dateadd(datetime('today'),1,year,)")
|
|
|
|
expected = datetime(2017, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('today'), -2, year)")
|
|
|
|
expected = datetime(2014, 11, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('today'), 2, quarter)")
|
|
|
|
expected = datetime(2017, 5, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('today'), 3, month)")
|
|
|
|
expected = datetime(2017, 2, 7)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('today'), -3, week)")
|
|
|
|
expected = datetime(2016, 10, 17)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('today'), 3, day)")
|
|
|
|
expected = datetime(2016, 11, 10)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('now'), 3, hour)")
|
|
|
|
expected = datetime(2016, 11, 7, 12, 30, 10)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('now'), 40, minute)")
|
|
|
|
expected = datetime(2016, 11, 7, 10, 10, 10)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("dateadd(datetime('now'), -11, second)")
|
|
|
|
expected = datetime(2016, 11, 7, 9, 29, 59)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetrunc(datetime('now'), year)")
|
|
|
|
expected = datetime(2016, 1, 1, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetrunc(datetime('now'), month)")
|
|
|
|
expected = datetime(2016, 11, 1, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetrunc(datetime('now'), day)")
|
|
|
|
expected = datetime(2016, 11, 7, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetrunc(datetime('now'), week)")
|
|
|
|
expected = datetime(2016, 11, 7, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetrunc(datetime('now'), hour)")
|
|
|
|
expected = datetime(2016, 11, 7, 9, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetrunc(datetime('now'), minute)")
|
|
|
|
expected = datetime(2016, 11, 7, 9, 30, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("datetrunc(datetime('now'), second)")
|
|
|
|
expected = datetime(2016, 11, 7, 9, 30, 10)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("lastday(datetime('now'), year)")
|
|
|
|
expected = datetime(2016, 12, 31, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("lastday(datetime('today'), month)")
|
|
|
|
expected = datetime(2016, 11, 30, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("holiday('Christmas')")
|
|
|
|
expected = datetime(2016, 12, 25, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval("holiday('Labor day', datetime('2018-01-01T00:00:00'))")
|
|
|
|
expected = datetime(2018, 9, 3, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval(
|
|
|
|
"holiday('Boxing day', datetime('2018-01-01T00:00:00'), 'UK')"
|
|
|
|
)
|
|
|
|
expected = datetime(2018, 12, 26, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
|
|
result = datetime_eval(
|
|
|
|
"lastday(dateadd(datetime('2018-01-01T00:00:00'), 1, month), month)"
|
|
|
|
)
|
|
|
|
expected = datetime(2018, 2, 28, 0, 0, 0)
|
|
|
|
self.assertEqual(result, expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2018-07-31 16:52:20 -04:00
|
|
|
def test_convert_legacy_filters_into_adhoc_where(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"where": "a = 1"}
|
2018-07-31 16:52:20 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{"clause": "WHERE", "expressionType": "SQL", "sqlExpression": "a = 1"}
|
|
|
|
]
|
2018-07-31 16:52:20 -04:00
|
|
|
}
|
|
|
|
convert_legacy_filters_into_adhoc(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2018-07-31 16:52:20 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2018-07-31 16:52:20 -04:00
|
|
|
def test_convert_legacy_filters_into_adhoc_filters(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"filters": [{"col": "a", "op": "in", "val": "someval"}]}
|
2018-07-31 16:52:20 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{
|
|
|
|
"clause": "WHERE",
|
|
|
|
"comparator": "someval",
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "in",
|
|
|
|
"subject": "a",
|
|
|
|
}
|
|
|
|
]
|
2018-07-31 16:52:20 -04:00
|
|
|
}
|
|
|
|
convert_legacy_filters_into_adhoc(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2018-07-31 16:52:20 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2018-07-31 16:52:20 -04:00
|
|
|
def test_convert_legacy_filters_into_adhoc_having(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"having": "COUNT(1) = 1"}
|
2018-07-31 16:52:20 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
2018-07-31 16:52:20 -04:00
|
|
|
{
|
2019-06-25 16:34:48 -04:00
|
|
|
"clause": "HAVING",
|
|
|
|
"expressionType": "SQL",
|
|
|
|
"sqlExpression": "COUNT(1) = 1",
|
|
|
|
}
|
|
|
|
]
|
2018-07-31 16:52:20 -04:00
|
|
|
}
|
|
|
|
convert_legacy_filters_into_adhoc(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2018-07-31 16:52:20 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2018-07-31 16:52:20 -04:00
|
|
|
def test_convert_legacy_filters_into_adhoc_having_filters(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"having_filters": [{"col": "COUNT(1)", "op": "==", "val": 1}]}
|
2018-07-31 16:52:20 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{
|
|
|
|
"clause": "HAVING",
|
|
|
|
"comparator": 1,
|
|
|
|
"expressionType": "SIMPLE",
|
|
|
|
"operator": "==",
|
|
|
|
"subject": "COUNT(1)",
|
|
|
|
}
|
|
|
|
]
|
2018-07-31 16:52:20 -04:00
|
|
|
}
|
|
|
|
convert_legacy_filters_into_adhoc(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2018-07-31 16:52:20 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2018-08-10 13:22:06 -04:00
|
|
|
def test_convert_legacy_filters_into_adhoc_present_and_empty(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
form_data = {"adhoc_filters": [], "where": "a = 1"}
|
2018-08-10 13:22:06 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{"clause": "WHERE", "expressionType": "SQL", "sqlExpression": "a = 1"}
|
|
|
|
]
|
2018-08-10 13:22:06 -04:00
|
|
|
}
|
|
|
|
convert_legacy_filters_into_adhoc(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2018-08-10 13:22:06 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
@patch("superset.utils.core.to_adhoc", mock_to_adhoc)
|
2018-08-10 13:22:06 -04:00
|
|
|
def test_convert_legacy_filters_into_adhoc_present_and_nonempty(self):
|
|
|
|
form_data = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{"clause": "WHERE", "expressionType": "SQL", "sqlExpression": "a = 1"}
|
2018-08-10 13:22:06 -04:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"filters": [{"col": "a", "op": "in", "val": "someval"}],
|
|
|
|
"having": "COUNT(1) = 1",
|
|
|
|
"having_filters": [{"col": "COUNT(1)", "op": "==", "val": 1}],
|
2018-07-31 16:52:20 -04:00
|
|
|
}
|
2018-08-10 13:22:06 -04:00
|
|
|
expected = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"adhoc_filters": [
|
|
|
|
{"clause": "WHERE", "expressionType": "SQL", "sqlExpression": "a = 1"}
|
|
|
|
]
|
2018-08-10 13:22:06 -04:00
|
|
|
}
|
2018-07-31 16:52:20 -04:00
|
|
|
convert_legacy_filters_into_adhoc(form_data)
|
2019-10-21 10:49:12 -04:00
|
|
|
self.assertEqual(form_data, expected)
|
2019-05-08 01:37:44 -04:00
|
|
|
|
|
|
|
def test_parse_js_uri_path_items_eval_undefined(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertIsNone(parse_js_uri_path_item("undefined", eval_undefined=True))
|
|
|
|
self.assertIsNone(parse_js_uri_path_item("null", eval_undefined=True))
|
|
|
|
self.assertEqual("undefined", parse_js_uri_path_item("undefined"))
|
|
|
|
self.assertEqual("null", parse_js_uri_path_item("null"))
|
2019-05-08 01:37:44 -04:00
|
|
|
|
|
|
|
def test_parse_js_uri_path_items_unquote(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual("slashed/name", parse_js_uri_path_item("slashed%2fname"))
|
|
|
|
self.assertEqual(
|
|
|
|
"slashed%2fname", parse_js_uri_path_item("slashed%2fname", unquote=False)
|
|
|
|
)
|
2019-05-08 01:37:44 -04:00
|
|
|
|
|
|
|
def test_parse_js_uri_path_items_item_optional(self):
|
|
|
|
self.assertIsNone(parse_js_uri_path_item(None))
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertIsNotNone(parse_js_uri_path_item("item"))
|
2019-07-12 11:58:44 -04:00
|
|
|
|
|
|
|
def test_get_stacktrace(self):
|
|
|
|
with app.app_context():
|
|
|
|
app.config["SHOW_STACKTRACE"] = True
|
|
|
|
try:
|
|
|
|
raise Exception("NONONO!")
|
|
|
|
except Exception:
|
|
|
|
stacktrace = get_stacktrace()
|
|
|
|
self.assertIn("NONONO", stacktrace)
|
|
|
|
|
|
|
|
app.config["SHOW_STACKTRACE"] = False
|
|
|
|
try:
|
|
|
|
raise Exception("NONONO!")
|
|
|
|
except Exception:
|
|
|
|
stacktrace = get_stacktrace()
|
|
|
|
assert stacktrace is None
|
2019-07-27 20:43:39 -04:00
|
|
|
|
2019-09-17 17:04:14 -04:00
|
|
|
def test_split(self):
|
|
|
|
self.assertEqual(list(split("a b")), ["a", "b"])
|
|
|
|
self.assertEqual(list(split("a,b", delimiter=",")), ["a", "b"])
|
|
|
|
self.assertEqual(list(split("a,(b,a)", delimiter=",")), ["a", "(b,a)"])
|
|
|
|
self.assertEqual(
|
|
|
|
list(split('a,(b,a),"foo , bar"', delimiter=",")),
|
|
|
|
["a", "(b,a)", '"foo , bar"'],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
list(split("a,'b,c'", delimiter=",", quote="'")), ["a", "'b,c'"]
|
|
|
|
)
|
|
|
|
self.assertEqual(list(split('a "b c"')), ["a", '"b c"'])
|
|
|
|
self.assertEqual(list(split(r'a "b \" c"')), ["a", r'"b \" c"'])
|
|
|
|
|
2019-07-27 20:43:39 -04:00
|
|
|
def test_get_or_create_db(self):
|
|
|
|
get_or_create_db("test_db", "sqlite:///superset.db")
|
|
|
|
database = db.session.query(Database).filter_by(database_name="test_db").one()
|
|
|
|
self.assertIsNotNone(database)
|
|
|
|
self.assertEqual(database.sqlalchemy_uri, "sqlite:///superset.db")
|
|
|
|
self.assertIsNotNone(
|
2019-12-03 17:57:24 -05:00
|
|
|
security_manager.find_permission_view_menu("database_access", database.perm)
|
2019-07-27 20:43:39 -04:00
|
|
|
)
|
|
|
|
# Test change URI
|
|
|
|
get_or_create_db("test_db", "sqlite:///changed.db")
|
|
|
|
database = db.session.query(Database).filter_by(database_name="test_db").one()
|
|
|
|
self.assertEqual(database.sqlalchemy_uri, "sqlite:///changed.db")
|
2019-11-20 10:47:06 -05:00
|
|
|
db.session.delete(database)
|
|
|
|
db.session.commit()
|
2019-07-27 20:43:39 -04:00
|
|
|
|
|
|
|
def test_get_or_create_db_invalid_uri(self):
|
|
|
|
with self.assertRaises(ArgumentError):
|
|
|
|
get_or_create_db("test_db", "yoursql:superset.db/()")
|
2019-10-31 10:11:48 -04:00
|
|
|
|
|
|
|
def test_get_time_range_endpoints(self):
|
|
|
|
self.assertEqual(
|
2019-11-06 12:15:58 -05:00
|
|
|
get_time_range_endpoints(form_data={}),
|
2019-10-31 10:11:48 -04:00
|
|
|
(TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.EXCLUSIVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
get_time_range_endpoints(
|
2019-11-06 12:15:58 -05:00
|
|
|
form_data={"time_range_endpoints": ["inclusive", "inclusive"]}
|
2019-10-31 10:11:48 -04:00
|
|
|
),
|
|
|
|
(TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.INCLUSIVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-11-06 12:15:58 -05:00
|
|
|
get_time_range_endpoints(form_data={"datasource": "1_druid"}),
|
2019-10-31 10:11:48 -04:00
|
|
|
(TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.EXCLUSIVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
slc = Mock()
|
|
|
|
slc.datasource.database.get_extra.return_value = {}
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
get_time_range_endpoints(form_data={"datasource": "1__table"}, slc=slc),
|
|
|
|
(TimeRangeEndpoint.UNKNOWN, TimeRangeEndpoint.INCLUSIVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
slc.datasource.database.get_extra.return_value = {
|
|
|
|
"time_range_endpoints": ["inclusive", "inclusive"]
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
get_time_range_endpoints(form_data={"datasource": "1__table"}, slc=slc),
|
|
|
|
(TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.INCLUSIVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIsNone(get_time_range_endpoints(form_data={}, slc=slc))
|
2019-11-04 15:00:41 -05:00
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
app.config["SIP_15_GRACE_PERIOD_END"] = date.today() + timedelta(days=1)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
get_time_range_endpoints(form_data={"datasource": "1__table"}, slc=slc),
|
|
|
|
(TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.INCLUSIVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
app.config["SIP_15_GRACE_PERIOD_END"] = date.today()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
get_time_range_endpoints(form_data={"datasource": "1__table"}, slc=slc),
|
|
|
|
(TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.EXCLUSIVE),
|
|
|
|
)
|
2020-03-13 15:18:22 -04:00
|
|
|
|
|
|
|
def test_get_iterable(self):
|
|
|
|
self.assertListEqual(get_iterable(123), [123])
|
|
|
|
self.assertListEqual(get_iterable([123]), [123])
|
|
|
|
self.assertListEqual(get_iterable("foo"), ["foo"])
|
2020-03-18 11:21:10 -04:00
|
|
|
|
|
|
|
def test_build_extra_filters(self):
|
2020-09-02 19:03:25 -04:00
|
|
|
world_health = db.session.query(Dashboard).filter_by(slug="world_health").one()
|
|
|
|
layout = json.loads(world_health.position_json)
|
|
|
|
filter_ = db.session.query(Slice).filter_by(slice_name="Region Filter").one()
|
|
|
|
world = db.session.query(Slice).filter_by(slice_name="World's Population").one()
|
|
|
|
box_plot = db.session.query(Slice).filter_by(slice_name="Box plot").one()
|
|
|
|
treemap = db.session.query(Slice).filter_by(slice_name="Treemap").one()
|
|
|
|
|
2020-03-18 11:21:10 -04:00
|
|
|
filter_scopes = {
|
2020-09-02 19:03:25 -04:00
|
|
|
str(filter_.id): {
|
|
|
|
"region": {"scope": ["ROOT_ID"], "immune": [treemap.id]},
|
|
|
|
"country_name": {
|
|
|
|
"scope": ["ROOT_ID"],
|
|
|
|
"immune": [treemap.id, box_plot.id],
|
|
|
|
},
|
2020-03-18 11:21:10 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-02 19:03:25 -04:00
|
|
|
|
2020-03-18 11:21:10 -04:00
|
|
|
default_filters = {
|
2020-09-02 19:03:25 -04:00
|
|
|
str(filter_.id): {
|
|
|
|
"region": ["North America"],
|
|
|
|
"country_name": ["United States"],
|
|
|
|
}
|
2020-03-18 11:21:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# immune to all filters
|
2020-09-02 19:03:25 -04:00
|
|
|
assert (
|
|
|
|
build_extra_filters(layout, filter_scopes, default_filters, treemap.id)
|
|
|
|
== []
|
2020-03-18 11:21:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# in scope
|
2020-09-02 19:03:25 -04:00
|
|
|
assert build_extra_filters(
|
|
|
|
layout, filter_scopes, default_filters, world.id
|
|
|
|
) == [
|
|
|
|
{"col": "region", "op": "==", "val": "North America"},
|
2020-03-18 11:21:10 -04:00
|
|
|
{"col": "country_name", "op": "in", "val": ["United States"]},
|
|
|
|
]
|
|
|
|
|
2020-09-02 19:03:25 -04:00
|
|
|
assert build_extra_filters(
|
|
|
|
layout, filter_scopes, default_filters, box_plot.id
|
|
|
|
) == [{"col": "region", "op": "==", "val": "North America"}]
|
2020-03-27 13:07:07 -04:00
|
|
|
|
|
|
|
def test_ssl_certificate_parse(self):
|
|
|
|
parsed_certificate = parse_ssl_cert(ssl_certificate)
|
|
|
|
self.assertEqual(parsed_certificate.serial_number, 12355228710836649848)
|
|
|
|
self.assertRaises(CertificateException, parse_ssl_cert, "abc" + ssl_certificate)
|
|
|
|
|
|
|
|
def test_ssl_certificate_file_creation(self):
|
|
|
|
path = create_ssl_cert_file(ssl_certificate)
|
|
|
|
expected_filename = hashlib.md5(ssl_certificate.encode("utf-8")).hexdigest()
|
|
|
|
self.assertIn(expected_filename, path)
|
|
|
|
self.assertTrue(os.path.exists(path))
|
2020-04-15 09:58:03 -04:00
|
|
|
|
|
|
|
def test_get_email_address_list(self):
|
|
|
|
self.assertEqual(get_email_address_list("a@a"), ["a@a"])
|
|
|
|
self.assertEqual(get_email_address_list(" a@a "), ["a@a"])
|
|
|
|
self.assertEqual(get_email_address_list("a@a\n"), ["a@a"])
|
|
|
|
self.assertEqual(get_email_address_list(",a@a;"), ["a@a"])
|
|
|
|
self.assertEqual(
|
|
|
|
get_email_address_list(",a@a; b@b c@c a-c@c; d@d, f@f"),
|
|
|
|
["a@a", "b@b", "c@c", "a-c@c", "d@d", "f@f"],
|
|
|
|
)
|
2020-04-29 15:16:47 -04:00
|
|
|
|
|
|
|
def test_get_form_data_default(self) -> None:
|
|
|
|
with app.test_request_context():
|
|
|
|
form_data, slc = get_form_data()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
form_data,
|
|
|
|
{"time_range_endpoints": get_time_range_endpoints(form_data={}),},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(slc, None)
|
|
|
|
|
|
|
|
def test_get_form_data_request_args(self) -> None:
|
|
|
|
with app.test_request_context(
|
|
|
|
query_string={"form_data": json.dumps({"foo": "bar"})}
|
|
|
|
):
|
|
|
|
form_data, slc = get_form_data()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
form_data,
|
|
|
|
{
|
|
|
|
"foo": "bar",
|
|
|
|
"time_range_endpoints": get_time_range_endpoints(form_data={}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(slc, None)
|
|
|
|
|
|
|
|
def test_get_form_data_request_form(self) -> None:
|
|
|
|
with app.test_request_context(data={"form_data": json.dumps({"foo": "bar"})}):
|
|
|
|
form_data, slc = get_form_data()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
form_data,
|
|
|
|
{
|
|
|
|
"foo": "bar",
|
|
|
|
"time_range_endpoints": get_time_range_endpoints(form_data={}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(slc, None)
|
|
|
|
|
|
|
|
def test_get_form_data_request_args_and_form(self) -> None:
|
|
|
|
with app.test_request_context(
|
|
|
|
data={"form_data": json.dumps({"foo": "bar"})},
|
|
|
|
query_string={"form_data": json.dumps({"baz": "bar"})},
|
|
|
|
):
|
|
|
|
form_data, slc = get_form_data()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
form_data,
|
|
|
|
{
|
|
|
|
"baz": "bar",
|
|
|
|
"foo": "bar",
|
|
|
|
"time_range_endpoints": get_time_range_endpoints(form_data={}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(slc, None)
|
2020-06-02 19:03:33 -04:00
|
|
|
|
|
|
|
def test_get_form_data_globals(self) -> None:
|
|
|
|
with app.test_request_context():
|
|
|
|
g.form_data = {"foo": "bar"}
|
|
|
|
form_data, slc = get_form_data()
|
|
|
|
delattr(g, "form_data")
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
form_data,
|
|
|
|
{
|
|
|
|
"foo": "bar",
|
|
|
|
"time_range_endpoints": get_time_range_endpoints(form_data={}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(slc, None)
|
2020-07-04 15:51:43 -04:00
|
|
|
|
2021-01-11 08:57:55 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2020-07-04 15:51:43 -04:00
|
|
|
def test_log_this(self) -> None:
|
|
|
|
# TODO: Add additional scenarios.
|
|
|
|
self.login(username="admin")
|
|
|
|
slc = self.get_slice("Girls", db.session)
|
|
|
|
dashboard_id = 1
|
|
|
|
|
|
|
|
resp = self.get_json_resp(
|
|
|
|
f"/superset/explore_json/{slc.datasource_type}/{slc.datasource_id}/"
|
|
|
|
+ f'?form_data={{"slice_id": {slc.id}}}&dashboard_id={dashboard_id}',
|
|
|
|
{"form_data": json.dumps(slc.viz.form_data)},
|
|
|
|
)
|
|
|
|
|
|
|
|
record = (
|
|
|
|
db.session.query(Log)
|
|
|
|
.filter_by(action="explore_json", slice_id=slc.id)
|
|
|
|
.order_by(Log.dttm.desc())
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(record.dashboard_id, dashboard_id)
|
|
|
|
self.assertEqual(json.loads(record.json)["dashboard_id"], str(dashboard_id))
|
|
|
|
self.assertEqual(json.loads(record.json)["form_data"]["slice_id"], slc.id)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
json.loads(record.json)["form_data"]["viz_type"],
|
|
|
|
slc.viz.form_data["viz_type"],
|
|
|
|
)
|
2020-07-13 10:21:02 -04:00
|
|
|
|
|
|
|
def test_schema_validate_json(self):
|
|
|
|
valid = '{"a": 5, "b": [1, 5, ["g", "h"]]}'
|
|
|
|
self.assertIsNone(schema.validate_json(valid))
|
|
|
|
invalid = '{"a": 5, "b": [1, 5, ["g", "h]]}'
|
|
|
|
self.assertRaises(marshmallow.ValidationError, schema.validate_json, invalid)
|
|
|
|
|
|
|
|
def test_schema_one_of_case_insensitive(self):
|
|
|
|
validator = schema.OneOfCaseInsensitive(choices=[1, 2, 3, "FoO", "BAR", "baz"])
|
|
|
|
self.assertEqual(1, validator(1))
|
|
|
|
self.assertEqual(2, validator(2))
|
|
|
|
self.assertEqual("FoO", validator("FoO"))
|
|
|
|
self.assertEqual("FOO", validator("FOO"))
|
|
|
|
self.assertEqual("bar", validator("bar"))
|
|
|
|
self.assertEqual("BaZ", validator("BaZ"))
|
|
|
|
self.assertRaises(marshmallow.ValidationError, validator, "qwerty")
|
|
|
|
self.assertRaises(marshmallow.ValidationError, validator, 4)
|
2020-08-07 01:41:39 -04:00
|
|
|
|
2020-08-12 12:58:14 -04:00
|
|
|
def test_cast_to_num(self) -> None:
|
|
|
|
assert cast_to_num("5") == 5
|
|
|
|
assert cast_to_num("5.2") == 5.2
|
|
|
|
assert cast_to_num(10) == 10
|
|
|
|
assert cast_to_num(10.1) == 10.1
|
|
|
|
assert cast_to_num(None) is None
|
|
|
|
assert cast_to_num("this is not a string") is None
|
|
|
|
|
2020-08-07 01:41:39 -04:00
|
|
|
def test_get_form_data_token(self):
|
|
|
|
assert get_form_data_token({"token": "token_abcdefg1"}) == "token_abcdefg1"
|
|
|
|
generated_token = get_form_data_token({})
|
|
|
|
assert re.match(r"^token_[a-z0-9]{8}$", generated_token) is not None
|