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