|
14 | 14 | # KIND, either express or implied. See the License for the |
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
| 17 | +import time |
| 18 | +from datetime import datetime |
17 | 19 | from unittest.mock import patch |
18 | 20 |
|
19 | 21 | import pytest |
@@ -370,22 +372,56 @@ class TestChartsUpdateCommand(SupersetTestCase): |
370 | 372 | @patch("superset.utils.core.g") |
371 | 373 | @patch("superset.security.manager.g") |
372 | 374 | @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""" |
375 | 377 | pk = db.session.query(Slice).all()[0].id |
376 | 378 | user = security_manager.find_user(username="admin") |
377 | 379 | 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) |
386 | 419 | command.run() |
| 420 | + |
387 | 421 | 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 | + ) |
389 | 425 | assert chart.last_saved_by == user |
390 | 426 |
|
391 | 427 | @patch("superset.utils.core.g") |
|
0 commit comments