49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
import importlib.util
|
|
import pathlib
|
|
import unittest
|
|
from datetime import datetime
|
|
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
|
MODULE_PATH = ROOT / "src" / "parser" / "publish_time.py"
|
|
|
|
|
|
def load_module():
|
|
if not MODULE_PATH.exists():
|
|
raise AssertionError("缺少文章发布时间规范化模块")
|
|
spec = importlib.util.spec_from_file_location("publish_time", MODULE_PATH)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
class ArticlePublishTimeTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.module = load_module()
|
|
self.now = datetime(2026, 8, 3, 15, 0, 0)
|
|
|
|
def test_keeps_normal_publish_time_within_timezone_tolerance(self):
|
|
value = self.module.normalize_published_at(
|
|
"2026-08-03 23:01:04",
|
|
now=self.now,
|
|
)
|
|
|
|
self.assertEqual(value, "2026-08-03 23:01:04")
|
|
|
|
def test_rejects_clearly_future_publish_time(self):
|
|
value = self.module.normalize_published_at(
|
|
"2031-07-21 04:50:09",
|
|
now=self.now,
|
|
)
|
|
|
|
self.assertEqual(value, "")
|
|
|
|
def test_rejects_unparseable_publish_time(self):
|
|
value = self.module.normalize_published_at("not-a-date", now=self.now)
|
|
|
|
self.assertEqual(value, "")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|