|
1 | 1 | import datetime |
2 | 2 | import logging |
3 | 3 | import sys |
| 4 | +from collections.abc import Generator |
4 | 5 | from itertools import cycle |
5 | 6 | from unittest.mock import create_autospec |
6 | 7 |
|
@@ -909,14 +910,36 @@ def test_is_upgraded(ws, mock_pyspark): |
909 | 910 | external_locations.resolve_mount.assert_not_called() |
910 | 911 |
|
911 | 912 |
|
912 | | -def test_table_status(): |
913 | | - class FakeDate(datetime.datetime): |
| 913 | +@pytest.fixture |
| 914 | +def datetime_with_epoch_timestamp(monkeypatch) -> Generator[None, None, None]: |
| 915 | + # The timestamp() method on datetime() is immutable, so we can't just patch/mock it. Rather we need to substitute |
| 916 | + # the entire class, which the normal mocking/patching routines don't seem to support. |
| 917 | + # Note: this will not affect any modules that have already initialized and imported the class directly. Similarly, |
| 918 | + # the effect cannot be unwound if this test triggers initializing of modules that import the class directly instead |
| 919 | + # of the module. |
| 920 | + # If you find yourself here debugging an issue, please be aware that the following can be problematic: |
| 921 | + # >>> from datetime import datetime |
| 922 | + # (Why? It's reference to the class directly, and having the name of the module makes inspection tedious.) |
| 923 | + # Prefer instead: |
| 924 | + # >>> import datetime |
| 925 | + # Or even better: |
| 926 | + # >>> import datetime as dt |
| 927 | + |
| 928 | + class FakeDateTime(datetime.datetime): |
914 | 929 |
|
915 | 930 | def timestamp(self): |
916 | 931 | return 0 |
917 | 932 |
|
918 | | - datetime.datetime = FakeDate |
919 | | - errors = {} |
| 933 | + _original_datetime = datetime.datetime |
| 934 | + try: |
| 935 | + datetime.datetime = FakeDateTime # type: ignore[misc] |
| 936 | + yield |
| 937 | + finally: |
| 938 | + datetime.datetime = _original_datetime # type: ignore[misc] |
| 939 | + |
| 940 | + |
| 941 | +def test_table_status(datetime_with_epoch_timestamp) -> None: |
| 942 | + errors: dict[str, str] = {} |
920 | 943 | rows = { |
921 | 944 | "SHOW TBLPROPERTIES `schema1`.`table1`": MockBackend.rows("key", "value")["upgrade_to", "cat1.schema1.dest1"] |
922 | 945 | } |
|
0 commit comments