Skip to content

Commit

Permalink
Fix group items by date range function
Browse files Browse the repository at this point in the history
The problem is that `sorted` function can't compare None-value, but ex.
RentAdjustment model's date fields can be nullable.

By default now, the function converts None date values to min and max
values and can be now sorted correctly.
  • Loading branch information
Tomi Järvi committed Mar 8, 2021
1 parent 1c3aa43 commit ae80241
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
18 changes: 13 additions & 5 deletions leasing/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,22 @@ def split_date_range(date_range, count):
return result


def _get_date_range_from_item(item):
def _get_date_range_from_item(item, fill_min_max_values=True):
if isinstance(item, dict):
return item["date_range"]
start_date, end_date = item["date_range"]
else:
if callable(item.date_range):
return item.date_range()
start_date, end_date = item.date_range()
else:
return item.date_range
start_date, end_date = item.date_range

if fill_min_max_values:
if start_date is None:
start_date = datetime.date.min
if end_date is None:
end_date = datetime.date.max

return start_date, end_date


def group_items_in_period_by_date_range(items, min_date, max_date):
Expand All @@ -325,7 +333,7 @@ def group_items_in_period_by_date_range(items, min_date, max_date):
while current_date < max_date:
current_items = []
for item in sorted_items:
item_range = _get_date_range_from_item(item)
item_range = _get_date_range_from_item(item, fill_min_max_values=False)
if (item_range[0] is None or item_range[0] <= current_date) and (
item_range[1] is None or current_date <= item_range[1]
):
Expand Down
54 changes: 54 additions & 0 deletions leasing/tests/models/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

import pytest

from leasing.enums import (
DueDatesType,
RentAdjustmentAmountType,
RentAdjustmentType,
RentCycle,
RentType,
)
from leasing.models.utils import (
combine_ranges,
fix_amount_for_overlap,
Expand Down Expand Up @@ -725,6 +732,53 @@ def test_group_items_in_period_by_date_range(items, expected):
)


@pytest.mark.django_db
def test_group_items_in_period_with_nullable_date_model_fields(
lease_test_data, rent_factory, decision_factory, rent_adjustment_factory
):

lease = lease_test_data["lease"]

rent = rent_factory(
lease=lease,
type=RentType.FIXED,
cycle=RentCycle.JANUARY_TO_DECEMBER,
due_dates_type=DueDatesType.FIXED,
due_dates_per_year=1,
)

decision = decision_factory(lease=lease)

rent_adjustment = rent_adjustment_factory(
rent=rent,
type=RentAdjustmentType.DISCOUNT,
decision=decision,
intended_use_id=1,
start_date=None,
end_date=date(2021, 1, 1),
full_amount=12345,
amount_type=RentAdjustmentAmountType.AMOUNT_TOTAL,
)

rent_adjustment_2 = rent_adjustment_factory(
rent=rent,
type=RentAdjustmentType.DISCOUNT,
decision=decision,
intended_use_id=1,
start_date=date(2021, 1, 1),
end_date=None,
full_amount=12345,
amount_type=RentAdjustmentAmountType.AMOUNT_TOTAL,
)

try:
group_items_in_period_by_date_range(
[rent_adjustment, rent_adjustment_2], date(2015, 9, 1), date(2015, 10, 31)
)
except Exception as exc:
assert False, f"Function raised an exception {exc}"


@pytest.mark.parametrize(
"identifier, expected",
[
Expand Down

0 comments on commit ae80241

Please sign in to comment.