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.
|
2018-09-17 12:36:47 -04:00
|
|
|
from flask import json
|
|
|
|
|
2019-10-18 17:44:27 -04:00
|
|
|
from superset import app, jinja_context
|
2018-09-17 23:29:14 -04:00
|
|
|
from tests.base_tests import SupersetTestCase
|
2018-09-17 12:36:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MacroTestCase(SupersetTestCase):
|
|
|
|
def test_filter_values_macro(self):
|
|
|
|
form_data1 = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [{"col": "my_special_filter", "op": "in", "val": ["foo"]}],
|
|
|
|
"filters": [{"col": "my_special_filter2", "op": "in", "val": ["bar"]}],
|
2018-09-17 12:36:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
form_data2 = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [
|
|
|
|
{"col": "my_special_filter", "op": "in", "val": ["foo", "bar"]}
|
|
|
|
]
|
2018-09-17 12:36:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
form_data3 = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [
|
|
|
|
{"col": "my_special_filter", "op": "in", "val": ["foo", "bar"]}
|
2018-09-17 12:36:47 -04:00
|
|
|
],
|
2019-06-25 16:34:48 -04:00
|
|
|
"filters": [{"col": "my_special_filter", "op": "in", "val": ["savage"]}],
|
2018-09-17 12:36:47 -04:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:54:07 -04:00
|
|
|
form_data4 = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"extra_filters": [{"col": "my_special_filter", "op": "in", "val": "foo"}],
|
|
|
|
"filters": [{"col": "my_special_filter", "op": "in", "val": "savage"}],
|
2019-06-03 12:54:07 -04:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
data1 = {"form_data": json.dumps(form_data1)}
|
|
|
|
data2 = {"form_data": json.dumps(form_data2)}
|
|
|
|
data3 = {"form_data": json.dumps(form_data3)}
|
|
|
|
data4 = {"form_data": json.dumps(form_data4)}
|
2018-09-17 12:36:47 -04:00
|
|
|
|
|
|
|
with app.test_request_context(data=data1):
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_values = jinja_context.filter_values("my_special_filter")
|
|
|
|
self.assertEqual(filter_values, ["foo"])
|
2018-09-17 12:36:47 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_values = jinja_context.filter_values("my_special_filter2")
|
|
|
|
self.assertEqual(filter_values, ["bar"])
|
2018-09-17 12:36:47 -04:00
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_values = jinja_context.filter_values("")
|
2018-09-17 12:36:47 -04:00
|
|
|
self.assertEqual(filter_values, [])
|
|
|
|
|
|
|
|
with app.test_request_context(data=data2):
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_values = jinja_context.filter_values("my_special_filter")
|
|
|
|
self.assertEqual(filter_values, ["foo", "bar"])
|
2018-09-17 12:36:47 -04:00
|
|
|
|
|
|
|
with app.test_request_context(data=data3):
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_values = jinja_context.filter_values("my_special_filter")
|
|
|
|
self.assertEqual(filter_values, ["savage", "foo", "bar"])
|
2018-09-17 12:36:47 -04:00
|
|
|
|
|
|
|
with app.test_request_context():
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_values = jinja_context.filter_values("nonexistent_filter", "foo")
|
|
|
|
self.assertEqual(filter_values, ["foo"])
|
2019-06-03 12:54:07 -04:00
|
|
|
|
|
|
|
with app.test_request_context(data=data4):
|
2019-06-25 16:34:48 -04:00
|
|
|
filter_values = jinja_context.filter_values("my_special_filter")
|
|
|
|
self.assertEqual(filter_values, ["savage", "foo"])
|