|
| 1 | +from datetime import datetime, timezone |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from scaleway.mongodb.v1.custom_api import MongodbUtilsV1API |
| 6 | +from scaleway.mongodb.v1.api import MongodbV1API |
| 7 | +from tests.utils import initialize_client_test |
| 8 | + |
| 9 | + |
| 10 | +# mypy: ignore-errors |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize("method_name", ["create_snapshot", "update_snapshot"]) |
| 14 | +def test_utils_api_coerces_naive_datetime_to_utc( |
| 15 | + monkeypatch: pytest.MonkeyPatch, method_name: str |
| 16 | +) -> None: |
| 17 | + captured = {} |
| 18 | + |
| 19 | + def dummy(self, **kwargs): # type: ignore[no-untyped-def] |
| 20 | + captured.update(kwargs) |
| 21 | + return kwargs |
| 22 | + |
| 23 | + monkeypatch.setattr(MongodbV1API, method_name, dummy, raising=True) |
| 24 | + |
| 25 | + api = MongodbUtilsV1API(client=initialize_client_test(), bypass_validation=True) |
| 26 | + |
| 27 | + # Build naive datetime without triggering DTZ001 (construct aware then strip tz) |
| 28 | + naive_dt = datetime(2030, 1, 1, 12, 0, 0, tzinfo=timezone.utc).replace(tzinfo=None) |
| 29 | + |
| 30 | + if method_name == "create_snapshot": |
| 31 | + api.create_snapshot(instance_id="iid", name="n", expires_at=naive_dt) |
| 32 | + else: |
| 33 | + api.update_snapshot(snapshot_id="sid", expires_at=naive_dt) |
| 34 | + |
| 35 | + assert "expires_at" in captured |
| 36 | + coerced = captured["expires_at"] |
| 37 | + assert isinstance(coerced, datetime) |
| 38 | + assert coerced.tzinfo is not None |
| 39 | + assert coerced.utcoffset() == timezone.utc.utcoffset(coerced) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("method_name", ["create_snapshot", "update_snapshot"]) |
| 43 | +def test_utils_api_preserves_aware_datetime( |
| 44 | + monkeypatch: pytest.MonkeyPatch, method_name: str |
| 45 | +) -> None: |
| 46 | + captured = {} |
| 47 | + |
| 48 | + def dummy(self, **kwargs): # type: ignore[no-untyped-def] |
| 49 | + captured.update(kwargs) |
| 50 | + return kwargs |
| 51 | + |
| 52 | + monkeypatch.setattr(MongodbV1API, method_name, dummy, raising=True) |
| 53 | + |
| 54 | + api = MongodbUtilsV1API(client=initialize_client_test(), bypass_validation=True) |
| 55 | + |
| 56 | + aware_dt = datetime(2030, 1, 1, 12, 0, 0, tzinfo=timezone.utc) |
| 57 | + |
| 58 | + if method_name == "create_snapshot": |
| 59 | + api.create_snapshot(instance_id="iid", name="n", expires_at=aware_dt) |
| 60 | + else: |
| 61 | + api.update_snapshot(snapshot_id="sid", expires_at=aware_dt) |
| 62 | + |
| 63 | + assert captured["expires_at"] is aware_dt |
0 commit comments