Skip to content

Commit

Permalink
refine unit test for deadline
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Apr 1, 2024
1 parent bcc5001 commit fadaf14
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions tests/test_dev.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import warnings
import datetime
from unittest.mock import MagicMock, patch
from unittest.mock import patch

import pytest
from monty.dev import deprecated, install_excepthook, requires
Expand Down Expand Up @@ -91,9 +91,9 @@ def classmethod_b(cls):

def test_deprecated_deadline(self, monkeypatch):
with pytest.raises(DeprecationWarning):
monkeypatch.setenv("CI", "true") # mock CI env

with patch("subprocess.run") as mock_run:
monkeypatch.setenv("CI", "true") # mock CI env

# Mock "GITHUB_REPOSITORY"
monkeypatch.setenv("GITHUB_REPOSITORY", "TESTOWNER/TESTREPO")
mock_run.return_value.stdout.decode.return_value = (
Expand All @@ -104,30 +104,52 @@ def test_deprecated_deadline(self, monkeypatch):
def func_old():
pass

@pytest.fixture()
def test_deprecated_deadline_no_warn(self, monkeypatch):
"""Test cases where no warning should be raised."""

# No warn case 1: date before deadline
with warnings.catch_warnings():
# Mock date to 1999-01-01
datetime_mock = MagicMock(wrap=datetime.datetime)
datetime_mock.now.return_value = datetime.datetime(1999, 1, 1)
monkeypatch.setattr(datetime, "datetime", datetime_mock)
with patch("subprocess.run") as mock_run:
monkeypatch.setenv("CI", "true") # mock CI env

@deprecated(deadline=(2000, 1, 1))
def func_old_0():
pass
# Mock date to 1999-01-01
monkeypatch.setattr(
datetime.datetime, "now", datetime.datetime(1999, 1, 1)
)

# Mock "GITHUB_REPOSITORY"
monkeypatch.setenv("GITHUB_REPOSITORY", "TESTOWNER/TESTREPO")
mock_run.return_value.stdout.decode.return_value = (
"git@github.com:TESTOWNER/TESTREPO.git"
)

@deprecated(deadline=(2000, 1, 1))
def func_old():
pass

monkeypatch.undo()

# No warn case 2: not in CI env
with warnings.catch_warnings():
monkeypatch.delenv("CI", raising=False)
with patch("subprocess.run") as mock_run:
monkeypatch.delenv("CI", raising=False)

@deprecated(deadline=(2000, 1, 1))
def func_old_1():
pass
# Mock "GITHUB_REPOSITORY"
monkeypatch.setenv("GITHUB_REPOSITORY", "TESTOWNER/TESTREPO")
mock_run.return_value.stdout.decode.return_value = (
"git@github.com:TESTOWNER/TESTREPO.git"
)

@deprecated(deadline=(2000, 1, 1))
def func_old_1():
pass

monkeypatch.undo()

# No warn case 3: not in code owner repo
with warnings.catch_warnings():
monkeypatch.setenv("CI", "true")
monkeypatch.delenv("GITHUB_REPOSITORY", raising=False)

@deprecated(deadline=(2000, 1, 1))
Expand Down

0 comments on commit fadaf14

Please sign in to comment.