Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh authored May 23, 2022
1 parent 5c365b8 commit 9a368cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions superset/datasets/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
)
Expand Down
42 changes: 42 additions & 0 deletions tests/integration_tests/datasets/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 9a368cb

Please sign in to comment.