Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix promise future dates #8367

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions openlibrary/catalog/add_book/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ def normalize_import_record(rec: dict) -> None:
if not isinstance(rec['source_records'], list):
rec['source_records'] = [rec['source_records']]

publication_year = get_publication_year(rec.get('publish_date'))
if publication_year and published_in_future_year(publication_year):
del rec['publish_date']

# Split subtitle if required and not already present
if ':' in rec.get('title', '') and not rec.get('subtitle'):
title, subtitle = split_subtitle(rec.get('title'))
Expand Down
24 changes: 24 additions & 0 deletions openlibrary/catalog/add_book/tests/test_add_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from copy import deepcopy
from datetime import datetime
from infogami.infobase.client import Nothing

from infogami.infobase.core import Text
Expand All @@ -18,6 +19,7 @@
isbns_from_record,
load,
load_data,
normalize_import_record,
should_overwrite_promise_item,
split_subtitle,
RequiredField,
Expand Down Expand Up @@ -1451,3 +1453,25 @@ def test_passing_edition_to_load_data_overwrites_edition_with_rec_data(
'ia:newlyscannedpromiseitem',
]
assert edition.works[0]['key'] == '/works/OL1W'


class TestNormalizeImportRecord:
@pytest.mark.parametrize(
'year, expected',
[
("2000-11-11", True),
(str(datetime.now().year), True),
(str(datetime.now().year + 1), False),
("9999-01-01", False),
],
)
def test_future_publication_dates_are_deleted(self, year, expected):
"""It should be impossible to import books publish_date in a future year."""
rec = {
'title': 'test book',
'source_records': ['ia:blob'],
'publish_date': year,
}
normalize_import_record(rec=rec)
result = 'publish_date' in rec
assert result == expected