Skip to content

Commit

Permalink
chore: Move parse_date and parse_datetime to getter from helpers, #56
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jan 18, 2023
1 parent 927d961 commit 7bbe146
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 205 deletions.
2 changes: 1 addition & 1 deletion contracting_process/field_level/range/date_time.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import date

from tools.checks import field_quality_check
from tools.helpers import parse_date
from tools.getter import parse_date

name = "date_time"
lower_bound = date(1990, 1, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from tools.checks import get_empty_result_resource
from tools.getter import get_values
from tools.helpers import parse_date
from tools.getter import get_values, parse_date

version = 1.0

Expand Down
3 changes: 1 addition & 2 deletions contracting_process/resource_level/coherent/dates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from tools.checks import get_empty_result_resource
from tools.getter import get_values
from tools.helpers import parse_date
from tools.getter import get_values, parse_date

version = 1.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from tools.checks import get_empty_result_resource
from tools.getter import get_values
from tools.helpers import parse_date
from tools.getter import get_values, parse_date

version = 1.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from tools.checks import get_empty_result_resource
from tools.getter import get_values
from tools.helpers import parse_date
from tools.getter import get_values, parse_date

version = 1.0

Expand Down
3 changes: 1 addition & 2 deletions contracting_process/resource_level/coherent/period.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from tools.checks import get_empty_result_resource
from tools.getter import get_values
from tools.helpers import parse_datetime
from tools.getter import get_values, parse_datetime

version = 1.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from tools.checks import get_empty_result_resource
from tools.currency_converter import convert
from tools.getter import get_values
from tools.helpers import parse_datetime
from tools.getter import get_values, parse_datetime

version = 1.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from tools.checks import get_empty_result_resource
from tools.currency_converter import convert
from tools.getter import get_values
from tools.helpers import parse_datetime
from tools.getter import get_values, parse_datetime

version = 1.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tools.checks import get_empty_result_resource
from tools.currency_converter import convert
from tools.helpers import parse_datetime
from tools.getter import parse_datetime

version = 1.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import math

from tools.checks import get_empty_result_resource
from tools.getter import deep_get, get_values
from tools.helpers import parse_datetime
from tools.getter import deep_get, get_values, parse_datetime

version = 1.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from tools.checks import get_empty_result_resource
from tools.currency_converter import convert
from tools.getter import get_values
from tools.helpers import parse_date
from tools.getter import get_values, parse_date

version = 1.0

Expand Down
3 changes: 1 addition & 2 deletions dataset/distribution/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from tools.checks import get_empty_result_dataset
from tools.currency_converter import convert, currency_available
from tools.getter import get_values
from tools.helpers import parse_date
from tools.getter import get_values, parse_date

version = 1.0

Expand Down
3 changes: 1 addition & 2 deletions dataset/meta_data_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

from tools import settings
from tools.currency_converter import convert
from tools.getter import get_values
from tools.helpers import parse_datetime
from tools.getter import get_values, parse_datetime
from tools.services import get_cursor

DATE_STR_FORMAT = "%b-%-y"
Expand Down
147 changes: 146 additions & 1 deletion tests/tools/test_getter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from tools.getter import get_values
from datetime import datetime, timedelta, timezone

import pytest

from tools.getter import get_values, parse_date, parse_datetime

EMPTY = [None, "", 0, 0.0, False, set(), (), [], {}]
NON_STR = [None, 1, 1.0, True, {1}, (1,), [1], {1}]

item = {
"id": "123",
Expand All @@ -15,6 +22,144 @@
}


@pytest.mark.parametrize("value", EMPTY)
def test_parse_datetime_empty(value):
assert parse_datetime(value) is None


@pytest.mark.parametrize("value", NON_STR)
def test_parse_datetime_type(value):
assert parse_datetime(value) is None


@pytest.mark.parametrize("value", ["x", "200101"])
def test_parse_datetime_invalid(value):
assert parse_datetime(value) is None


@pytest.mark.parametrize(
"value,components",
[
("2001", (2001, 1, 1, 0, 0)),
("2001-02", (2001, 2, 1, 0, 0)),
("2001-02-03", (2001, 2, 3, 0, 0)),
("20010203", (2001, 2, 3, 0, 0)),
],
)
def test_parse_datetime_date(value, components):
assert parse_datetime(value) == datetime(*components)


# The tests serve to document the formats that are accepted. We don't test week formats.
#
# Dateutil can parse truncated times like "2001-02-03T00:5" and "2001-02-03T00:00:6", but these formats don't support
# time zones. This is undocumented behavior.
#
# https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.isoparse
@pytest.mark.parametrize(
"suffix,tz",
[
("", None),
("Z", timezone.utc),
# UTC.
("+00", timezone.utc),
("-00", timezone.utc),
("+0000", timezone.utc),
("-0000", timezone.utc),
("+00:00", timezone.utc),
("-00:00", timezone.utc),
# Non-UTC.
("+07", timezone(timedelta(seconds=25200))),
("-07", timezone(timedelta(seconds=-25200))),
("+0708", timezone(timedelta(seconds=25680))),
("-0708", timezone(timedelta(seconds=-25680))),
("+07:08", timezone(timedelta(seconds=25680))),
("-07:08", timezone(timedelta(seconds=-25680))),
],
)
@pytest.mark.parametrize(
"value,components",
[
# With separators.
("2001-02-03T04", (2001, 2, 3, 4, 0)),
("2001-02-03T04:05", (2001, 2, 3, 4, 5)),
("2001-02-03T04:05:06", (2001, 2, 3, 4, 5, 6)),
("2001-02-03T04:05:06.0", (2001, 2, 3, 4, 5, 6)),
("2001-02-03T04:05:06.123456789", (2001, 2, 3, 4, 5, 6, 123456)),
# Without separators. (Note: If the "separator" is a number, it is discarded!)
("20010203.04", (2001, 2, 3, 4, 0)),
("20010203.0405", (2001, 2, 3, 4, 5)),
("20010203.040506", (2001, 2, 3, 4, 5, 6)),
("20010203.040506,0", (2001, 2, 3, 4, 5, 6)),
("20010203.040506,123456789", (2001, 2, 3, 4, 5, 6, 123456)),
# 24-hour clock.
("2001-02-03T00", (2001, 2, 3, 0, 0)),
("2001-02-03T24", (2001, 2, 4, 0, 0)),
],
)
def test_parse_datetime_dateutil(value, components, suffix, tz):
assert parse_datetime(value + suffix) == datetime(*components, tzinfo=tz)


# The datetime library can handle short components and long timezones.
@pytest.mark.parametrize(
"suffix,tz",
[
("Z", timezone.utc),
# UTC.
("+0000", timezone.utc),
("-0000", timezone.utc),
("+00:00", timezone.utc),
("-00:00", timezone.utc),
("+00:00:00", timezone.utc),
("-00:00:00", timezone.utc),
# Non-UTC.
("+0708", timezone(timedelta(seconds=25680))),
("-0708", timezone(timedelta(seconds=-25680))),
("+07:08", timezone(timedelta(seconds=25680))),
("-07:08", timezone(timedelta(seconds=-25680))),
("+07:08:09", timezone(timedelta(seconds=25689))),
("-07:08:09", timezone(timedelta(seconds=-25689))),
],
)
def test_parse_datetime_library(suffix, tz):
assert parse_datetime("1000-2-3T4:5:6" + suffix) == datetime(1000, 2, 3, 4, 5, 6, tzinfo=tz)


@pytest.mark.parametrize("value", EMPTY)
def test_parse_date_empty(value):
assert parse_date(value) is None


@pytest.mark.parametrize("value", NON_STR)
def test_parse_date_type(value):
assert parse_date(value) is None


@pytest.mark.parametrize("value", ["10000-01-01", "x"])
def test_parse_date_invalid(value):
assert parse_date(value) is None


@pytest.mark.parametrize(
"value,components",
[
# Date only.
("2001", (2001, 1, 1)),
("2001-02", (2001, 2, 1)),
("2001-02-03", (2001, 2, 3)),
("20010203", (2001, 2, 3)),
# Truncated components.
("1000-2-3", (1000, 2, 3)),
# Extra components.
("2001-02-03xxx", (2001, 2, 3)),
("2001-02-03T04:05:06Z", (2001, 2, 3)),
],
)
def test_parse_date(value, components):
assert parse_date(value) == datetime(*components).date()


def test_get_values_invalid():
assert get_values({"tender": {"tenderers": "string"}}, "tender.tenderers.contactPoint.name") == []

Expand Down
Loading

0 comments on commit 7bbe146

Please sign in to comment.