macro_tests.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from flask import json
  18. from superset import app, jinja_context
  19. from tests.base_tests import SupersetTestCase
  20. class MacroTestCase(SupersetTestCase):
  21. def test_filter_values_macro(self):
  22. form_data1 = {
  23. "extra_filters": [{"col": "my_special_filter", "op": "in", "val": ["foo"]}],
  24. "filters": [{"col": "my_special_filter2", "op": "in", "val": ["bar"]}],
  25. }
  26. form_data2 = {
  27. "extra_filters": [
  28. {"col": "my_special_filter", "op": "in", "val": ["foo", "bar"]}
  29. ]
  30. }
  31. form_data3 = {
  32. "extra_filters": [
  33. {"col": "my_special_filter", "op": "in", "val": ["foo", "bar"]}
  34. ],
  35. "filters": [{"col": "my_special_filter", "op": "in", "val": ["savage"]}],
  36. }
  37. form_data4 = {
  38. "extra_filters": [{"col": "my_special_filter", "op": "in", "val": "foo"}],
  39. "filters": [{"col": "my_special_filter", "op": "in", "val": "savage"}],
  40. }
  41. data1 = {"form_data": json.dumps(form_data1)}
  42. data2 = {"form_data": json.dumps(form_data2)}
  43. data3 = {"form_data": json.dumps(form_data3)}
  44. data4 = {"form_data": json.dumps(form_data4)}
  45. with app.test_request_context(data=data1):
  46. filter_values = jinja_context.filter_values("my_special_filter")
  47. self.assertEqual(filter_values, ["foo"])
  48. filter_values = jinja_context.filter_values("my_special_filter2")
  49. self.assertEqual(filter_values, ["bar"])
  50. filter_values = jinja_context.filter_values("")
  51. self.assertEqual(filter_values, [])
  52. with app.test_request_context(data=data2):
  53. filter_values = jinja_context.filter_values("my_special_filter")
  54. self.assertEqual(filter_values, ["foo", "bar"])
  55. with app.test_request_context(data=data3):
  56. filter_values = jinja_context.filter_values("my_special_filter")
  57. self.assertEqual(filter_values, ["savage", "foo", "bar"])
  58. with app.test_request_context():
  59. filter_values = jinja_context.filter_values("nonexistent_filter", "foo")
  60. self.assertEqual(filter_values, ["foo"])
  61. with app.test_request_context(data=data4):
  62. filter_values = jinja_context.filter_values("my_special_filter")
  63. self.assertEqual(filter_values, ["savage", "foo"])