Skip to content

Commit

Permalink
Add DateConverter for handling date types and implement corresponding…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
arthur-debert committed Dec 6, 2024
1 parent 4daf83f commit a1e7bf0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
26 changes: 25 additions & 1 deletion rangy/builtins.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
from rangy import Converter, ConverterRegistry
from datetime import date

# Create converters for built-in types

class DateConverter(Converter):
def __init__(self):
super().__init__(int, self.to_number, self.to_string)

def to_string(self, d):
if isinstance(d, date):
return d.isoformat()
else:
raise TypeError("Not a date")


def to_number(self, d):
if isinstance(d, date):
return d.toordinal()
else:
raise TypeError("Not a date")

def register_builtins():
date_converter = DateConverter()
int_converter = Converter(int)
float_converter = Converter(float)
converters = [int_converter, float_converter]

# Register DateConverter *first*!
ConverterRegistry.register(date_converter) # Changed order
ConverterRegistry.register(float_converter)
ConverterRegistry.register(int_converter) # Changed order
converters = [date_converter,int_converter, float_converter]
for converter in converters:
try:
ConverterRegistry.get(converter._type)
Expand Down
3 changes: 3 additions & 0 deletions rangy/converters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import date

class Converter():
"""
Facilitates conversion of custom data types to numeric and string representations for use within the `Rangy` system.
Expand Down Expand Up @@ -69,6 +71,7 @@ def __init__(self, _type, to_numeric=None, to_string=None):
self.to_string = to_string

def to_number(self, value):

if self.to_numeric:
return self.to_numeric(value)
try:
Expand Down
2 changes: 1 addition & 1 deletion rangy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _split(as_squence):
Returns:
A tuple of two values.
"""
if None in as_squence:
if None == as_squence:
raise ParseRangeError("Invalid range tuple/list")
if len(as_squence) == 1:
# this is valid, as it
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

from rangy.builtins import DateConverter, register_builtins
from rangy.registry import ConverterRegistry


@pytest.fixture(autouse=True) # autouse applies to all tests automatically
def register_converters():
ConverterRegistry.clear() # Start with a clean registry for each test
ConverterRegistry.register(DateConverter()) # Register the DateConverter
register_builtins()
44 changes: 44 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest
from datetime import date
from rangy.builtins import DateConverter
from rangy import parse_range, Rangy
from rangy.registry import ConverterRegistry
from tests.fixtures.register import register_converters


@pytest.mark.usefixtures("register_converters")
def test_date_rangy():
start_date = date(2023, 1, 1)
end_date = date(2023, 12, 31)
res = parse_range(Rangy((start_date, end_date)))
assert res == (start_date.toordinal(), end_date.toordinal())

@pytest.mark.usefixtures("register_converters")
def test_singular_date_rangy():
singular_date = date(2023, 6, 15)
rangy_obj = Rangy((singular_date, singular_date))
res = rangy_obj.value
assert res == singular_date.toordinal()

@pytest.mark.usefixtures("register_converters")
def test_open_range_rangy():
start_date = date(2023, 1, 1)
res = parse_range(Rangy((start_date, "*")))
assert res[0] == start_date.toordinal()
assert res[1] is None

@pytest.mark.usefixtures("register_converters")
def test_range_contains_date():
start_date = date(2023, 1, 1)
end_date = date(2023, 12, 31)
range_obj = Rangy((start_date, end_date))
some_date = date(2023, 6, 15)
assert some_date.toordinal() in range_obj
assert date(2024, 1, 1).toordinal() not in range_obj

@pytest.mark.usefixtures("register_converters")
def test_date_converter():
date_converter = DateConverter()
some_date = date(2023, 6, 15)
assert date_converter.to_number(some_date) == some_date.toordinal()
assert date_converter.to_string(some_date) == some_date.isoformat()

0 comments on commit a1e7bf0

Please sign in to comment.