Skip to content
Snippets Groups Projects
Verified Commit c1832762 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Refactor: Extract date utils manipulation function into utils module

parent 00a8abd4
No related branches found
No related tags found
No related merge requests found
......@@ -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):
......
# 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
# 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment