Skip to content

Commit

Permalink
Test fallback on invalid metadata fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jgraichen committed Apr 12, 2023
1 parent 3710348 commit e80339f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mkdocs_blogging_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,5 +353,6 @@ def _parse_time(self, value):
return value.timestamp()
if isinstance(value, date):
return datetime.combine(value, datetime.min.time()).timestamp()
if self.meta_time_format:
if self.meta_time_format and isinstance(value, str):
return datetime.strptime(value, self.meta_time_format).timestamp()
return None
15 changes: 15 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ def test_datetime_object(self):
page = self.plugin.with_timestamp(page, False)

assert page.meta["localized-time"] == "2023/04/12 09:15:30"

def test_datetime_object_fallback(self):
"""
If meta.time is present but is an invalid type, we want to fall
back to using meta.date if available and valid.
"""
page = SimpleNamespace(
meta={
"time": 500,
"date": datetime.datetime(2023, 4, 12, 9, 15, 30),
}
)
page = self.plugin.with_timestamp(page, False)

assert page.meta["localized-time"] == "2023/04/12 09:15:30"

0 comments on commit e80339f

Please sign in to comment.