Skip to content

Commit 692f478

Browse files
committed
test(mongodb): convert monkeypatch tests to VCR integration for snapshot creation
1 parent db3144e commit 692f478

File tree

1 file changed

+19
-51
lines changed

1 file changed

+19
-51
lines changed
Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,32 @@
1-
from datetime import datetime, timezone
1+
import os
2+
from datetime import datetime, timedelta, timezone
23

34
import pytest
45

5-
from scaleway.mongodb.v1.api import MongodbV1API
6+
from vcr_config import scw_vcr
7+
from tests.utils import initialize_client_test
68
from scaleway.mongodb.v1.custom_api import MongodbUtilsV1API
79

8-
class _DummyClient:
9-
pass
1010

1111
# mypy: ignore-errors
1212

1313

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)
1918

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")
2322

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)
2525

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+
)
2731

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

Comments
 (0)