Skip to content

Commit 456deb1

Browse files
committed
fix(mongodb): add custom APIs and tests under expected package path for CI
1 parent 22450c9 commit 456deb1

File tree

6 files changed

+132
-13
lines changed

6 files changed

+132
-13
lines changed

scaleway/mongodb/v1/custom_api.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import annotations
2+
3+
from datetime import datetime, timezone
4+
from typing import Any, Optional
5+
6+
from scaleway.mongodb.v1.api import MongodbV1API
7+
8+
9+
def _ensure_tzaware_utc(value: Optional[datetime]) -> Optional[datetime]:
10+
if value is None:
11+
return None
12+
if value.tzinfo is None or value.tzinfo.utcoffset(value) is None:
13+
return value.replace(tzinfo=timezone.utc)
14+
return value
15+
16+
17+
class MongodbUtilsV1API(MongodbV1API):
18+
"""
19+
Extensions for MongoDB V1 that provide safer ergonomics.
20+
21+
- Naive datetimes for expires_at are assumed to be UTC.
22+
"""
23+
24+
def create_snapshot(self, **kwargs: Any) -> Any:
25+
expires_at = kwargs.get("expires_at")
26+
kwargs["expires_at"] = _ensure_tzaware_utc(expires_at)
27+
return super().create_snapshot(**kwargs)
28+
29+
def update_snapshot(self, **kwargs: Any) -> Any:
30+
expires_at = kwargs.get("expires_at")
31+
kwargs["expires_at"] = _ensure_tzaware_utc(expires_at)
32+
return super().update_snapshot(**kwargs)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import annotations
2+
3+
from datetime import datetime, timezone
4+
from typing import Any, Optional
5+
6+
from scaleway.mongodb.v1alpha1.api import MongodbV1Alpha1API
7+
8+
9+
def _ensure_tzaware_utc(value: Optional[datetime]) -> Optional[datetime]:
10+
if value is None:
11+
return None
12+
if value.tzinfo is None or value.tzinfo.utcoffset(value) is None:
13+
return value.replace(tzinfo=timezone.utc)
14+
return value
15+
16+
17+
class MongodbUtilsV1Alpha1API(MongodbV1Alpha1API):
18+
"""
19+
Extensions for MongoDB V1alpha1 that provide safer ergonomics.
20+
21+
- Naive datetimes for expires_at are assumed to be UTC.
22+
"""
23+
24+
def create_snapshot(self, **kwargs: Any) -> Any:
25+
expires_at = kwargs.get("expires_at")
26+
kwargs["expires_at"] = _ensure_tzaware_utc(expires_at)
27+
return super().create_snapshot(**kwargs)
28+
29+
def update_snapshot(self, **kwargs: Any) -> Any:
30+
expires_at = kwargs.get("expires_at")
31+
kwargs["expires_at"] = _ensure_tzaware_utc(expires_at)
32+
return super().update_snapshot(**kwargs)

scaleway/scaleway/mongodb/v1/custom_api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from __future__ import annotations
22

33
from datetime import datetime, timezone
4-
from typing import Optional, Any
5-
6-
from .api import MongodbV1API
4+
from typing import Any, Optional
75

6+
from scaleway.scaleway.mongodb.v1.api import MongodbV1API
87

98

109
def _ensure_tzaware_utc(value: Optional[datetime]) -> Optional[datetime]:
@@ -31,5 +30,3 @@ def update_snapshot(self, **kwargs: Any) -> Any:
3130
expires_at = kwargs.get("expires_at")
3231
kwargs["expires_at"] = _ensure_tzaware_utc(expires_at)
3332
return super().update_snapshot(**kwargs)
34-
35-

scaleway/scaleway/mongodb/v1/tests/test_custom_api.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import pytest
44

5-
from scaleway.mongodb.v1.custom_api import MongodbUtilsV1API
65
from scaleway.mongodb.v1.api import MongodbV1API
6+
from scaleway.mongodb.v1.custom_api import MongodbUtilsV1API
77
from tests.utils import initialize_client_test
88

99
# mypy: ignore-errors
@@ -60,5 +60,3 @@ def dummy(self, **kwargs): # type: ignore[no-untyped-def]
6060
api.update_snapshot(snapshot_id="sid", expires_at=aware_dt)
6161

6262
assert captured["expires_at"] is aware_dt
63-
64-

scaleway/scaleway/mongodb/v1alpha1/custom_api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from __future__ import annotations
22

33
from datetime import datetime, timezone
4-
from typing import Optional, Any
5-
6-
from .api import MongodbV1Alpha1API
4+
from typing import Any, Optional
75

6+
from scaleway.scaleway.mongodb.v1alpha1.api import MongodbV1Alpha1API
87

98

109
def _ensure_tzaware_utc(value: Optional[datetime]) -> Optional[datetime]:
@@ -31,5 +30,3 @@ def update_snapshot(self, **kwargs: Any) -> Any:
3130
expires_at = kwargs.get("expires_at")
3231
kwargs["expires_at"] = _ensure_tzaware_utc(expires_at)
3332
return super().update_snapshot(**kwargs)
34-
35-

0 commit comments

Comments
 (0)