fix(Tags filter): Filter assets by tag ID (#29412)

This commit is contained in:
Vitor Avila
2024-07-11 12:26:36 -03:00
committed by GitHub
parent dd74757032
commit 33b934cbb3
18 changed files with 636 additions and 109 deletions

View File

@@ -32,6 +32,7 @@ from superset import db
from superset.models.core import Database
from superset.models.core import FavStar
from superset.models.sql_lab import SavedQuery
from superset.tags.models import ObjectType, Tag, TaggedObject
from superset.utils.database import get_example_database
from superset.utils import json
@@ -42,6 +43,10 @@ from tests.integration_tests.fixtures.importexport import (
saved_queries_config,
saved_queries_metadata_config,
)
from tests.integration_tests.fixtures.tags import (
create_custom_tags, # noqa: F401
get_filter_params,
)
SAVED_QUERIES_FIXTURE_COUNT = 10
@@ -123,6 +128,73 @@ class TestSavedQueryApi(SupersetTestCase):
db.session.delete(fav_saved_query)
db.session.commit()
@pytest.fixture
def create_saved_queries_some_with_tags(self, create_custom_tags): # noqa: F811
"""
Fixture that creates 4 saved queries:
- ``first_query`` is associated with ``first_tag``
- ``second_query`` is associated with ``second_tag``
- ``third_query`` is associated with both ``first_tag`` and ``second_tag``
- ``fourth_query`` is not associated with any tag
Relies on the ``create_custom_tags`` fixture for the tag creation.
"""
with self.create_app().app_context():
tags = {
"first_tag": db.session.query(Tag)
.filter(Tag.name == "first_tag")
.first(),
"second_tag": db.session.query(Tag)
.filter(Tag.name == "second_tag")
.first(),
}
query_labels = [
"first_query",
"second_query",
"third_query",
"fourth_query",
]
queries = [
self.insert_default_saved_query(label=name) for name in query_labels
]
tag_associations = [
TaggedObject(
object_id=queries[0].id,
object_type=ObjectType.chart,
tag=tags["first_tag"],
),
TaggedObject(
object_id=queries[1].id,
object_type=ObjectType.chart,
tag=tags["second_tag"],
),
TaggedObject(
object_id=queries[2].id,
object_type=ObjectType.chart,
tag=tags["first_tag"],
),
TaggedObject(
object_id=queries[2].id,
object_type=ObjectType.chart,
tag=tags["second_tag"],
),
]
for association in tag_associations:
db.session.add(association)
db.session.commit()
yield queries
# rollback changes
for association in tag_associations:
db.session.delete(association)
for chart in queries:
db.session.delete(chart)
db.session.commit()
@pytest.mark.usefixtures("create_saved_queries")
def test_get_list_saved_query(self):
"""
@@ -366,6 +438,55 @@ class TestSavedQueryApi(SupersetTestCase):
data = json.loads(rv.data.decode("utf-8"))
assert data["count"] == len(all_queries)
@pytest.mark.usefixtures("create_saved_queries_some_with_tags")
def test_get_saved_queries_tag_filters(self):
"""
Saved Query API: Test get saved queries with tag filters
"""
# Get custom tags relationship
tags = {
"first_tag": db.session.query(Tag).filter(Tag.name == "first_tag").first(),
"second_tag": db.session.query(Tag)
.filter(Tag.name == "second_tag")
.first(),
"third_tag": db.session.query(Tag).filter(Tag.name == "third_tag").first(),
}
saved_queries_tag_relationship = {
tag.name: db.session.query(SavedQuery.id)
.join(SavedQuery.tags)
.filter(Tag.id == tag.id)
.all()
for tag in tags.values()
}
# Validate API results for each tag
for tag_name, tag in tags.items():
expected_saved_queries = saved_queries_tag_relationship[tag_name]
# Filter by tag ID
filter_params = get_filter_params("saved_query_tag_id", tag.id)
response_by_id = self.get_list("saved_query", filter_params)
self.assertEqual(response_by_id.status_code, 200)
data_by_id = json.loads(response_by_id.data.decode("utf-8"))
# Filter by tag name
filter_params = get_filter_params("saved_query_tags", tag.name)
response_by_name = self.get_list("saved_query", filter_params)
self.assertEqual(response_by_name.status_code, 200)
data_by_name = json.loads(response_by_name.data.decode("utf-8"))
# Compare results
self.assertEqual(
data_by_id["count"],
data_by_name["count"],
len(expected_saved_queries),
)
self.assertEqual(
set(query["id"] for query in data_by_id["result"]),
set(query["id"] for query in data_by_name["result"]),
set(query.id for query in expected_saved_queries),
)
@pytest.mark.usefixtures("create_saved_queries")
def test_get_saved_query_favorite_filter(self):
"""