diff --git a/swh/deposit/api/private/deposit_read.py b/swh/deposit/api/private/deposit_read.py index a6f4bd45afe8d699023d72a1eb881777a8d5c8b7..4141fdb5f1d0e9b3d5f983506f53a97260dd4198 100644 --- a/swh/deposit/api/private/deposit_read.py +++ b/swh/deposit/api/private/deposit_read.py @@ -9,12 +9,12 @@ import shutil import tempfile from contextlib import contextmanager -from dateutil import parser from django.http import FileResponse from rest_framework import status from swh.core import tarball from swh.model import identifiers +from swh.deposit.utils import normalize_date from . import DepositReadMixin from ...config import SWH_PERSON, ARCHIVE_TYPE @@ -137,29 +137,6 @@ class SWHDepositReadMetadata(SWHGetDepositAPI, SWHPrivateAPIView, if client_domain in metadata[field]: return metadata[field] - def _prepare_date(self, date): - """Prepare date fields as normalized swh date - - If date is a list, elect arbitrarily the first element of that - list - - If date is (then) a string, parse it through - dateutil.parser.parse to extract a datetime. - - Then normalize it through - swh.model.identifiers.normalize_timestamp. - - Returns - The swh date object - - """ - if isinstance(date, list): - date = date[0] - if isinstance(date, str): - date = parser.parse(date) - - return identifiers.normalize_timestamp(date) - def _normalize_dates(self, deposit, metadata): """Normalize the date to use as a tuple of author date, committer date from the incoming metadata. @@ -186,8 +163,8 @@ class SWHDepositReadMetadata(SWHGetDepositAPI, SWHPrivateAPIView, author_date = deposit.complete_date commit_date = deposit.complete_date return ( - self._prepare_date(author_date), - self._prepare_date(commit_date) + normalize_date(author_date), + normalize_date(commit_date) ) def metadata_read(self, deposit): diff --git a/swh/deposit/tests/test_utils.py b/swh/deposit/tests/test_utils.py index aa3ecfa76f26bee5d9d7a6f79917327f16c73a77..1fff7f601f58a0d8cb68deaeeaa8d19109076382 100644 --- a/swh/deposit/tests/test_utils.py +++ b/swh/deposit/tests/test_utils.py @@ -1,10 +1,11 @@ -# Copyright (C) 2018 The Software Heritage developers +# Copyright (C) 2018-2019 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import unittest +from unittest.mock import patch from swh.deposit import utils @@ -130,3 +131,52 @@ class UtilsTestCase(unittest.TestCase): utils.merge(d1) self.assertEqual(utils.merge(d0), d0) + + +@patch('swh.deposit.utils.normalize_timestamp', side_effect=lambda x: x) +def test_normalize_date_0(mock_normalize): + """When date is a list, choose the first date and normalize it + + Note: We do not test swh.model.identifiers which is already tested + in swh.model + + """ + actual_date = utils.normalize_date(['2017-10-12', 'date1']) + + expected_date = '2017-10-12 00:00:00' + + assert str(actual_date) == expected_date + + +@patch('swh.deposit.utils.normalize_timestamp', side_effect=lambda x: x) +def test_normalize_date_1(mock_normalize): + """Providing a date in a reasonable format, everything is fine + + Note: We do not test swh.model.identifiers which is already tested + in swh.model + + """ + mock_normalize.side_effect = lambda x: x + + actual_date = utils.normalize_date('2018-06-11 17:02:02') + + expected_date = '2018-06-11 17:02:02' # <- why? + + assert str(actual_date) == expected_date + + +@patch('swh.deposit.utils.normalize_timestamp', side_effect=lambda x: x) +def test_normalize_date_doing_irrelevant_stuff(mock_normalize): + """Providing a date in an unknown format, it's completely off + + Note: We do not test swh.model.identifiers which is already tested + in swh.model + + """ + mock_normalize.side_effect = lambda x: x + + actual_date = utils.normalize_date('2017') + + expected_date = '2017-04-16 00:00:00' # <- why? + + assert str(actual_date) == expected_date diff --git a/swh/deposit/utils.py b/swh/deposit/utils.py index 7979ec5b9e5411896fc379e2a896da8602495536..4994cb07e88f341ece6dea0c027f88a2ebf494b8 100644 --- a/swh/deposit/utils.py +++ b/swh/deposit/utils.py @@ -1,10 +1,13 @@ -# Copyright (C) 2018 The Software Heritage developers +# Copyright (C) 2018-2019 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from dateutil import parser from types import GeneratorType +from swh.model.identifiers import normalize_timestamp + def merge(*dicts): """Given an iterator of dicts, merge them losing no information. @@ -53,3 +56,27 @@ def merge(*dicts): new_val = _extend([existing_val], value) d[key] = new_val return d + + +def normalize_date(date): + """Normalize date fields as expected by swh workers. + + If date is a list, elect arbitrarily the first element of that + list + + If date is (then) a string, parse it through + dateutil.parser.parse to extract a datetime. + + Then normalize it through + swh.model.identifiers.normalize_timestamp. + + Returns + The swh date object + + """ + if isinstance(date, list): + date = date[0] + if isinstance(date, str): + date = parser.parse(date) + + return normalize_timestamp(date)