mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
fix: Update dataset's last modified date from column/metric update (#33626)
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from datetime import timedelta
|
||||
from io import BytesIO
|
||||
from unittest.mock import ANY, patch
|
||||
from zipfile import is_zipfile, ZipFile
|
||||
@@ -24,6 +25,7 @@ from zipfile import is_zipfile, ZipFile
|
||||
import prison
|
||||
import pytest
|
||||
import yaml
|
||||
from freezegun import freeze_time
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.orm import joinedload
|
||||
@@ -1144,9 +1146,9 @@ class TestDatasetApi(SupersetTestCase):
|
||||
"""
|
||||
Dataset API: Test update dataset create column
|
||||
"""
|
||||
|
||||
# create example dataset by Command
|
||||
dataset = self.insert_default_dataset()
|
||||
current_changed_on = dataset.changed_on
|
||||
|
||||
new_column_data = {
|
||||
"column_name": "new_col",
|
||||
@@ -1188,13 +1190,16 @@ class TestDatasetApi(SupersetTestCase):
|
||||
metric.pop("type_generic", None)
|
||||
|
||||
data["result"]["metrics"].append(new_metric_data)
|
||||
rv = self.client.put(
|
||||
uri,
|
||||
json={
|
||||
"columns": data["result"]["columns"],
|
||||
"metrics": data["result"]["metrics"],
|
||||
},
|
||||
)
|
||||
|
||||
with freeze_time() as frozen:
|
||||
frozen.tick(delta=timedelta(seconds=3))
|
||||
rv = self.client.put(
|
||||
uri,
|
||||
json={
|
||||
"columns": data["result"]["columns"],
|
||||
"metrics": data["result"]["metrics"],
|
||||
},
|
||||
)
|
||||
|
||||
assert rv.status_code == 200
|
||||
|
||||
@@ -1233,6 +1238,10 @@ class TestDatasetApi(SupersetTestCase):
|
||||
assert metrics[1].warning_text == new_metric_data["warning_text"]
|
||||
assert str(metrics[1].uuid) == new_metric_data["uuid"]
|
||||
|
||||
# Validate that the changed_on is updated
|
||||
updated_dataset = db.session.query(SqlaTable).filter_by(id=dataset.id).first()
|
||||
assert updated_dataset.changed_on > current_changed_on
|
||||
|
||||
self.items_to_delete = [dataset]
|
||||
|
||||
def test_update_dataset_delete_column(self):
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import copy
|
||||
import re
|
||||
import time
|
||||
from typing import Any
|
||||
@@ -30,7 +31,7 @@ from superset.common.chart_data import ChartDataResultFormat, ChartDataResultTyp
|
||||
from superset.common.query_context import QueryContext
|
||||
from superset.common.query_context_factory import QueryContextFactory
|
||||
from superset.common.query_object import QueryObject
|
||||
from superset.connectors.sqla.models import SqlMetric
|
||||
from superset.daos.dataset import DatasetDAO
|
||||
from superset.daos.datasource import DatasourceDAO
|
||||
from superset.extensions import cache_manager
|
||||
from superset.superset_typing import AdhocColumn
|
||||
@@ -47,6 +48,7 @@ from tests.integration_tests.conftest import (
|
||||
only_sqlite,
|
||||
with_feature_flags,
|
||||
)
|
||||
from tests.integration_tests.constants import ADMIN_USERNAME
|
||||
from tests.integration_tests.fixtures.birth_names_dashboard import (
|
||||
load_birth_names_dashboard_with_slices, # noqa: F401
|
||||
load_birth_names_data, # noqa: F401
|
||||
@@ -171,17 +173,28 @@ class TestQueryContext(SupersetTestCase):
|
||||
assert cache_key_original != cache_key_new
|
||||
|
||||
def test_query_cache_key_changes_when_metric_is_updated(self):
|
||||
"""
|
||||
Test that the query cache key changes when a metric is updated.
|
||||
"""
|
||||
self.login(ADMIN_USERNAME)
|
||||
payload = get_query_context("birth_names")
|
||||
|
||||
# make temporary change and revert it to refresh the changed_on property
|
||||
datasource = DatasourceDAO.get_datasource(
|
||||
datasource_type=DatasourceType(payload["datasource"]["type"]),
|
||||
datasource_id=payload["datasource"]["id"],
|
||||
)
|
||||
|
||||
datasource.metrics.append(SqlMetric(metric_name="foo", expression="select 1;"))
|
||||
dataset = DatasetDAO.find_by_id(payload["datasource"]["id"])
|
||||
dataset_payload = {
|
||||
"metrics": [
|
||||
{
|
||||
"metric_name": "foo",
|
||||
"expression": "select 1;",
|
||||
}
|
||||
]
|
||||
}
|
||||
DatasetDAO.update(dataset, copy.deepcopy(dataset_payload))
|
||||
db.session.commit()
|
||||
|
||||
# Add metric ID to the payload for future update
|
||||
updated_dataset = DatasetDAO.find_by_id(dataset.id)
|
||||
dataset_payload["metrics"][0]["id"] = updated_dataset.metrics[0].id
|
||||
|
||||
# construct baseline query_cache_key
|
||||
query_context = ChartDataQueryContextSchema().load(payload)
|
||||
query_object = query_context.queries[0]
|
||||
@@ -190,7 +203,8 @@ class TestQueryContext(SupersetTestCase):
|
||||
# wait a second since mysql records timestamps in second granularity
|
||||
time.sleep(1)
|
||||
|
||||
datasource.metrics[0].expression = "select 2;"
|
||||
dataset_payload["metrics"][0]["expression"] = "select 2;"
|
||||
DatasetDAO.update(updated_dataset, copy.deepcopy(dataset_payload))
|
||||
db.session.commit()
|
||||
|
||||
# create new QueryContext with unchanged attributes, extract new query_cache_key
|
||||
@@ -198,7 +212,8 @@ class TestQueryContext(SupersetTestCase):
|
||||
query_object = query_context.queries[0]
|
||||
cache_key_new = query_context.query_cache_key(query_object)
|
||||
|
||||
datasource.metrics = []
|
||||
dataset_payload["metrics"] = []
|
||||
DatasetDAO.update(updated_dataset, dataset_payload)
|
||||
db.session.commit()
|
||||
|
||||
# the new cache_key should be different due to updated datasource
|
||||
|
||||
Reference in New Issue
Block a user