Skip to content

Commit 787a230

Browse files
authored
transplants: mv CODE_FREEZE_OFFSET and test codefreeze_datetime (#220)
move CODE_FREEZE_OFFSET to top-level constant and add tests for mock datetime module
1 parent 7b872c2 commit 787a230

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

landoapi/models/revisions.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ class DiffWarning(Base):
5151
# The "type" of warning. This is mainly to group warnings when querying the API.
5252
group = db.Column(db.Enum(DiffWarningGroup), nullable=False)
5353

54-
# The code freeze dates generally correspond to PST work days.
55-
@property
56-
def code_freeze_offset(self) -> str:
57-
return "-0800"
58-
5954
def serialize(self):
6055
"""Return a JSON serializable dictionary."""
6156
return {

landoapi/transplants.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This Source Code Form is subject to the terms of the Mozilla Publc
1+
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import functools
@@ -41,6 +41,9 @@
4141
defaults=(None, None, None, None, False),
4242
)
4343

44+
# The code freeze dates generally correspond to PST work days.
45+
CODE_FREEZE_OFFSET = "-0800"
46+
4447

4548
def tokens_are_equal(t1, t2):
4649
"""Return whether t1 and t2 are equal.
@@ -330,14 +333,14 @@ def warning_code_freeze(*, repo, **kwargs):
330333

331334
today = datetime.now(tz=timezone.utc)
332335
freeze_date = datetime.strptime(
333-
f"{freeze_date_str} {DiffWarning.code_freeze_offset}",
336+
f"{freeze_date_str} {CODE_FREEZE_OFFSET}",
334337
"%Y-%m-%d %z",
335338
).replace(tzinfo=timezone.utc)
336339
if today < freeze_date:
337340
return
338341

339342
merge_date = datetime.strptime(
340-
f"{merge_date_str} {DiffWarning.code_freeze_offset}",
343+
f"{merge_date_str} {CODE_FREEZE_OFFSET}",
341344
"%Y-%m-%d %z",
342345
).replace(tzinfo=timezone.utc)
343346

tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from landoapi.app import construct_app, load_config, SUBSYSTEMS
2424
from landoapi.cache import cache, cache_subsystem
2525
from landoapi.mocks.auth import MockAuth0, TEST_JWKS
26-
from landoapi.models.revisions import DiffWarning
2726
from landoapi.phabricator import PhabricatorClient
2827
from landoapi.projects import (
2928
CHECKIN_PROJ_SLUG,
@@ -34,7 +33,7 @@
3433
from landoapi.repos import Repo, SCM_LEVEL_3
3534
from landoapi.storage import db as _db, db_subsystem
3635
from landoapi.tasks import celery
37-
from landoapi.transplants import tokens_are_equal
36+
from landoapi.transplants import tokens_are_equal, CODE_FREEZE_OFFSET
3837

3938
from tests.factories import TransResponseFactory
4039
from tests.mocks import PhabricatorDouble, TreeStatusDouble
@@ -446,7 +445,7 @@ def register_codefreeze_uri(request_mocker):
446445

447446
@pytest.fixture
448447
def codefreeze_datetime(request_mocker):
449-
utc_offset = DiffWarning.code_freeze_offset
448+
utc_offset = CODE_FREEZE_OFFSET
450449
dates = {
451450
"today": datetime(2000, 1, 5, 0, 0, 0, tzinfo=timezone.utc),
452451
f"two_days_ago {utc_offset}": datetime(2000, 1, 3, 0, 0, 0),

tests/test_transplants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import os
5+
from datetime import datetime, timezone
56
from unittest.mock import MagicMock
67

78
import pytest
@@ -1061,3 +1062,9 @@ def _create_transplant(
10611062
db.session.add(transplant)
10621063
db.session.commit()
10631064
return transplant
1065+
1066+
1067+
def test_codefreeze_datetime_mock(codefreeze_datetime):
1068+
dt = codefreeze_datetime()
1069+
assert dt.now(tz=timezone.utc) == datetime(2000, 1, 5, 0, 0, 0, tzinfo=timezone.utc)
1070+
assert dt.strptime("tomorrow -0800", fmt="") == datetime(2000, 1, 6, 0, 0, 0)

0 commit comments

Comments
 (0)