-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename formatters submodule and classes to converters
resolves #100
- Loading branch information
Showing
18 changed files
with
166 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from undate.converters.base import BaseDateConverter as BaseDateConverter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from undate.converters.edtf.converter import EDTFDateConverter as EDTFDateConverter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
src/undate/dateformat/iso8601.py → src/undate/converters/iso8601.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../test_dateformat/edtf/test_edtf_parser.py → .../test_converters/edtf/test_edtf_parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
..._dateformat/edtf/test_edtf_transformer.py → ..._converters/edtf/test_edtf_transformer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging | ||
|
||
import pytest | ||
from undate.converters.base import BaseDateConverter | ||
|
||
|
||
class TestBaseDateConverter: | ||
def test_available_converters(self): | ||
available_converters = BaseDateConverter.available_converters() | ||
assert isinstance(available_converters, dict) | ||
|
||
# NOTE: import _after_ generating available formatters | ||
# so we can confirm it gets loaded | ||
from undate.converters.iso8601 import ISO8601DateFormat | ||
|
||
assert ISO8601DateFormat.name in available_converters | ||
assert available_converters[ISO8601DateFormat.name] == ISO8601DateFormat | ||
|
||
def test_converters_are_unique(self): | ||
assert len(BaseDateConverter.available_converters()) == len( | ||
BaseDateConverter.__subclasses__() | ||
), "Formatter names have to be unique." | ||
|
||
def test_parse_not_implemented(self): | ||
with pytest.raises(NotImplementedError): | ||
BaseDateConverter().parse("foo bar baz") | ||
|
||
def test_parse_to_string(self): | ||
with pytest.raises(NotImplementedError): | ||
BaseDateConverter().to_string(1991) | ||
|
||
|
||
def test_import_converters_import_only_once(caplog): | ||
# clear the cache, since any instantiation of an Undate | ||
# object anywhere in the test suite will populate it | ||
BaseDateConverter.import_converters.cache_clear() | ||
|
||
# run first, and confirm it runs and loads formatters | ||
with caplog.at_level(logging.DEBUG): | ||
import_count = BaseDateConverter.import_converters() | ||
# should import at least one thing (iso8601) | ||
assert import_count >= 1 | ||
# should have log entry | ||
assert "Loading converters" in caplog.text | ||
|
||
# if we clear the log and run again, should not do anything | ||
caplog.clear() | ||
with caplog.at_level(logging.DEBUG): | ||
BaseDateConverter.import_converters() | ||
assert "Loading converters" not in caplog.text | ||
|
||
|
||
@pytest.mark.last | ||
def test_converters_unique_error(): | ||
# confirm that unique converter check fails when it should | ||
|
||
# run this test last because we can't undefine the subclass | ||
# once it exists... | ||
class ISO8601DateFormat2(BaseDateConverter): | ||
name = "ISO8601" # duplicates existing formatter | ||
|
||
assert len(BaseDateConverter.available_converters()) != len( | ||
BaseDateConverter.__subclasses__() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pytest | ||
from undate.converters.edtf import EDTFDateConverter | ||
from undate.undate import Undate, UndateInterval | ||
|
||
|
||
class TestEDTFDateConverter: | ||
def test_parse_singledate(self): | ||
assert EDTFDateConverter().parse("2002") == Undate(2002) | ||
assert EDTFDateConverter().parse("1991-05") == Undate(1991, 5) | ||
assert EDTFDateConverter().parse("1991-05-03") == Undate(1991, 5, 3) | ||
# unknown dates are not strictly equal, but string comparison should match | ||
assert str(EDTFDateConverter().parse("201X")) == str(Undate("201X")) | ||
assert str(EDTFDateConverter().parse("2004-XX")) == str(Undate(2004, "XX")) | ||
# missing year but month/day known | ||
# assert EDTFDateConverter().parse("--05-03") == Undate(month=5, day=3) | ||
|
||
def test_parse_singledate_unequal(self): | ||
assert EDTFDateConverter().parse("2002") != Undate(2003) | ||
assert EDTFDateConverter().parse("1991-05") != Undate(1991, 6) | ||
assert EDTFDateConverter().parse("1991-05-03") != Undate(1991, 5, 4) | ||
# missing year but month/day known | ||
# - does EDTF not support this or is parsing logic incorrect? | ||
# assert EDTFDateConverter().parse("XXXX-05-03") != Undate(month=5, day=4) | ||
|
||
def test_parse_invalid(self): | ||
with pytest.raises(ValueError): | ||
EDTFDateConverter().parse("1991-5") | ||
|
||
def test_parse_range(self): | ||
assert EDTFDateConverter().parse("1800/1900") == UndateInterval( | ||
Undate(1800), Undate(1900) | ||
) | ||
|
||
def test_to_string(self): | ||
assert EDTFDateConverter().to_string(Undate(900)) == "0900" | ||
assert EDTFDateConverter().to_string(Undate("80")) == "0080" | ||
assert EDTFDateConverter().to_string(Undate(33)) == "0033" | ||
assert EDTFDateConverter().to_string(Undate("20XX")) == "20XX" | ||
assert EDTFDateConverter().to_string(Undate(17000002)) == "Y17000002" | ||
|
||
assert EDTFDateConverter().to_string(Undate(1991, 6)) == "1991-06" | ||
assert EDTFDateConverter().to_string(Undate(1991, 5, 3)) == "1991-05-03" | ||
|
||
assert EDTFDateConverter().to_string(Undate(1991, "0X")) == "1991-0X" | ||
assert EDTFDateConverter().to_string(Undate(1991, None, 3)) == "1991-XX-03" | ||
|
||
# TODO: override missing digit and confirm replacement |
2 changes: 1 addition & 1 deletion
2
tests/test_dateformat/test_iso8601.py → tests/test_converters/test_iso8601.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.