From 7ea4fcddf8b94ba821ea8e3386ecaf0b673ca1b2 Mon Sep 17 00:00:00 2001 From: Jukka Ahonen Date: Fri, 29 Nov 2024 12:19:50 +0200 Subject: [PATCH] rent model: set_start_price_index_point_figure method --- leasing/models/rent.py | 4 +-- leasing/tests/conftest.py | 19 +++++++++++- leasing/tests/models/test_rent.py | 48 ++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/leasing/models/rent.py b/leasing/models/rent.py index 74b4b434..39a4a398 100644 --- a/leasing/models/rent.py +++ b/leasing/models/rent.py @@ -834,9 +834,9 @@ def set_start_price_index_point_figure(self): if self.old_dwellings_in_housing_companies_price_index: start_index_number_yearly = IndexPointFigureYearly.objects.get( index=self.old_dwellings_in_housing_companies_price_index, - year=self.start_date.year - 1, + year=self.lease.start_date.year - 1, ) - self.start_price_index_point_figure = start_index_number_yearly.point_figure + self.start_price_index_point_figure = start_index_number_yearly.value class RentDueDate(TimeStampedSafeDeleteModel): diff --git a/leasing/tests/conftest.py b/leasing/tests/conftest.py index 4a1a1797..7a1b15ca 100755 --- a/leasing/tests/conftest.py +++ b/leasing/tests/conftest.py @@ -67,7 +67,10 @@ LandUseAgreementType, ) from leasing.models.receivable_type import ReceivableType -from leasing.models.rent import OldDwellingsInHousingCompaniesPriceIndex +from leasing.models.rent import ( + IndexPointFigureYearly, + OldDwellingsInHousingCompaniesPriceIndex, +) from leasing.models.service_unit import ServiceUnitGroupMapping from leasing.models.tenant import TenantRentShare @@ -145,6 +148,20 @@ class Meta: model = Rent +@register +class OldDwellingsInHousingCompaniesPriceIndexFactory( + factory.django.DjangoModelFactory +): + class Meta: + model = OldDwellingsInHousingCompaniesPriceIndex + + +@register +class IndexPointFigureYearlyFactory(factory.django.DjangoModelFactory): + class Meta: + model = IndexPointFigureYearly + + @register class ContractRentFactory(factory.django.DjangoModelFactory): class Meta: diff --git a/leasing/tests/models/test_rent.py b/leasing/tests/models/test_rent.py index 18697502..f3834f03 100644 --- a/leasing/tests/models/test_rent.py +++ b/leasing/tests/models/test_rent.py @@ -12,7 +12,7 @@ RentCycle, RentType, ) -from leasing.models import Index, RentAdjustment, RentDueDate +from leasing.models import Index, Rent, RentAdjustment, RentDueDate from leasing.models.utils import DayMonth @@ -2841,3 +2841,49 @@ def test_is_the_last_billing_period( rent.save() assert rent.is_the_last_billing_period(billing_period) == expected + + +@pytest.mark.django_db +def test_set_start_price_index_point_figure_without_index(rent_factory, lease_factory): + """The point figure should be None if the rent has no old_dwellings_in_housing_companies_price_index.""" + lease = lease_factory() + rent: Rent = rent_factory(lease=lease) + + rent.set_start_price_index_point_figure() + + assert rent.start_price_index_point_figure is None + + +@pytest.mark.django_db +def test_set_start_price_index_point_figure_with_last_year_index( + rent_factory, + lease_factory, + old_dwellings_in_housing_companies_price_index_factory, + index_point_figure_yearly_factory, +): + """The point figure should be set if rent has old_dwellings_in_housing_companies_price_index + and it should be the year previous to the LEASE's start date.""" + lease = lease_factory(start_date=date(year=2024, month=1, day=1)) + old_dwellings_in_housing_companies_price_index = ( + old_dwellings_in_housing_companies_price_index_factory() + ) + index_point_figure_yearly_factory( + value=101, year=2021, index=old_dwellings_in_housing_companies_price_index + ) + index_point_figure_yearly_factory( + value=102, year=2022, index=old_dwellings_in_housing_companies_price_index + ) + expected_point_figure = index_point_figure_yearly_factory( + value=103, year=2023, index=old_dwellings_in_housing_companies_price_index + ) + index_point_figure_yearly_factory( + value=104, year=2024, index=old_dwellings_in_housing_companies_price_index + ) + rent: Rent = rent_factory( + lease=lease, + old_dwellings_in_housing_companies_price_index=old_dwellings_in_housing_companies_price_index, + ) + + rent.set_start_price_index_point_figure() + + assert rent.start_price_index_point_figure == expected_point_figure.value