Skip to content

Commit a0c29cc

Browse files
sadpandajoeclaude
andauthored
test: fix flaky MySQL integration test in test_update_v1_response (#36176)
Co-authored-by: Claude <[email protected]>
1 parent e7c5437 commit a0c29cc

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

tests/integration_tests/charts/commands_tests.py

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
import time
18+
from datetime import datetime
1719
from unittest.mock import patch
1820

1921
import pytest
@@ -370,22 +372,56 @@ class TestChartsUpdateCommand(SupersetTestCase):
370372
@patch("superset.utils.core.g")
371373
@patch("superset.security.manager.g")
372374
@pytest.mark.usefixtures("load_energy_table_with_slice")
373-
def test_update_v1_response(self, mock_sm_g, mock_c_g, mock_u_g):
374-
"""Test that a chart command updates properties"""
375+
def test_update_sets_last_saved_at(self, mock_sm_g, mock_c_g, mock_u_g):
376+
"""Test that update sets last_saved_at when previously unset"""
375377
pk = db.session.query(Slice).all()[0].id
376378
user = security_manager.find_user(username="admin")
377379
mock_u_g.user = mock_c_g.user = mock_sm_g.user = user
378-
model_id = pk
379-
json_obj = {
380-
"description": "test for update",
381-
"cache_timeout": None,
382-
"owners": [user.id],
383-
}
384-
command = UpdateChartCommand(model_id, json_obj)
385-
last_saved_before = db.session.query(Slice).get(pk).last_saved_at
380+
381+
# Explicitly set last_saved_at to None to test None -> datetime transition
382+
chart_to_update = db.session.query(Slice).get(pk)
383+
chart_to_update.last_saved_at = None
384+
db.session.commit()
385+
386+
command = UpdateChartCommand(
387+
pk,
388+
{"description": "test", "owners": [user.id]},
389+
)
390+
command.run()
391+
392+
chart = db.session.query(Slice).get(pk)
393+
assert chart.last_saved_at is not None
394+
assert chart.last_saved_by == user
395+
396+
@patch("superset.commands.chart.update.g")
397+
@patch("superset.utils.core.g")
398+
@patch("superset.security.manager.g")
399+
@pytest.mark.usefixtures("load_energy_table_with_slice")
400+
def test_update_changes_last_saved_at(self, mock_sm_g, mock_c_g, mock_u_g):
401+
"""Test that update changes last_saved_at when it already has a value"""
402+
pk = db.session.query(Slice).all()[0].id
403+
user = security_manager.find_user(username="admin")
404+
mock_u_g.user = mock_c_g.user = mock_sm_g.user = user
405+
406+
chart_to_update = db.session.query(Slice).get(pk)
407+
chart_to_update.last_saved_at = datetime.now()
408+
db.session.commit()
409+
# Refresh to get the database value with MySQL's truncated microseconds
410+
db.session.refresh(chart_to_update)
411+
last_saved_before = chart_to_update.last_saved_at
412+
413+
command = UpdateChartCommand(
414+
pk,
415+
{"description": "test", "owners": [user.id]},
416+
)
417+
# Sleep to ensure timestamp differs at MySQL's second precision (DATETIME(0))
418+
time.sleep(1)
386419
command.run()
420+
387421
chart = db.session.query(Slice).get(pk)
388-
assert chart.last_saved_at != last_saved_before
422+
assert chart.last_saved_at.replace(microsecond=0) != last_saved_before.replace(
423+
microsecond=0
424+
)
389425
assert chart.last_saved_by == user
390426

391427
@patch("superset.utils.core.g")

0 commit comments

Comments
 (0)