From 9a368cb6abf2f61286ca39dcd8dcb35a50bd3fd9 Mon Sep 17 00:00:00 2001 From: "Hugh A. Miles II" Date: Mon, 23 May 2022 16:11:54 +0000 Subject: [PATCH] added test --- superset/datasets/filters.py | 5 ++- tests/integration_tests/datasets/api_tests.py | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/superset/datasets/filters.py b/superset/datasets/filters.py index 6694d954020b0..d90d66ca3b288 100644 --- a/superset/datasets/filters.py +++ b/superset/datasets/filters.py @@ -42,12 +42,13 @@ class DatasetCertifiedFilter(BaseFilter): # pylint: disable=too-few-public-meth arg_name = "dataset_is_certified" def apply(self, query: Query, value: Any) -> Query: + check_value = '%"certification":%' if value is True: - return query.filter(SqlaTable.extra.ilike("%certification%")) + return query.filter(SqlaTable.extra.ilike(check_value)) if value is False: return query.filter( or_( - SqlaTable.extra.notlike("%certification%"), + SqlaTable.extra.notlike(check_value), SqlaTable.extra.is_(None), ) ) diff --git a/tests/integration_tests/datasets/api_tests.py b/tests/integration_tests/datasets/api_tests.py index 87d0da3cad827..f8a0f3495dcec 100644 --- a/tests/integration_tests/datasets/api_tests.py +++ b/tests/integration_tests/datasets/api_tests.py @@ -128,6 +128,7 @@ def create_datasets(self): main_db = get_main_database() for tables_name in self.fixture_tables_names: datasets.append(self.insert_dataset(tables_name, [admin.id], main_db)) + yield datasets # rollback changes @@ -1811,3 +1812,44 @@ def test_import_dataset_invalid_v0_validation(self): } ] } + + @pytest.mark.usefixtures("create_datasets", "create_virtual_datasets") + def test_get_datasets_is_certified_filter(self): + """ + Dataset API: Test custom dataset_is_certified filter + """ + arguments = { + "filters": [{"col": "id", "opr": "dataset_is_certified", "value": False}] + } + self.login(username="admin") + uri = f"api/v1/dataset/?q={prison.dumps(arguments)}" + rv = self.client.get(uri) + + assert rv.status_code == 200 + response = json.loads(rv.data.decode("utf-8")) + assert response.get("count") == 5 + + table_w_certification = SqlaTable( + table_name="foo", + schema=None, + owners=[], + database=get_main_database(), + sql=None, + extra='{"certification": 1}', + ) + db.session.add(table_w_certification) + db.session.commit() + + arguments = { + "filters": [{"col": "id", "opr": "dataset_is_certified", "value": True}] + } + self.login(username="admin") + uri = f"api/v1/dataset/?q={prison.dumps(arguments)}" + rv = self.client.get(uri) + + assert rv.status_code == 200 + response = json.loads(rv.data.decode("utf-8")) + assert response.get("count") == 1 + + db.session.delete(table_w_certification) + db.session.commit()