Skip to content

Commit

Permalink
asp.models: Commune.end_date is inclusive
Browse files Browse the repository at this point in the history
 code  | start_date |  end_date
-------+------------+------------
 01001 | 1900-01-01 | 1999-12-31
 01001 | 2000-01-01 | [NULL]
  • Loading branch information
rsebille authored and francoisfreitag committed Dec 12, 2024
1 parent e8494b1 commit 36a63b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion itou/asp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def by_insee_code_and_period(self, insee_code, period):
"Lookup a Commune object by INSEE code and valid at the given period"
return (
self.filter(code=insee_code, start_date__lte=period)
.filter(Q(end_date=None) | Q(end_date__gt=period))
.filter(Q(end_date=None) | Q(end_date__gte=period))
.get()
)

Expand Down
16 changes: 9 additions & 7 deletions tests/asp/test_communes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
from collections import Counter

from django.utils import timezone

from itou.asp.models import Commune


Expand Down Expand Up @@ -62,14 +64,14 @@ def test_by_insee_code_and_period(self):
old_commune = Commune(
code=99999,
name="ENNUI-SUR-BLASÉ",
start_date=datetime.datetime(1940, 1, 1),
end_date=datetime.datetime(2021, 12, 31),
start_date=datetime.date(1940, 1, 1),
end_date=datetime.date(2021, 12, 31),
)
new_commune = Commune(code=99999, name="ENNUI-SUR-BLASÉ", start_date=datetime.datetime(2022, 1, 1))
new_commune = Commune(code=99999, name="ENNUI-SUR-BLASÉ", start_date=datetime.date(2022, 1, 1))
Commune.objects.bulk_create([old_commune, new_commune])

result = Commune.objects.by_insee_code_and_period(99999, datetime.datetime(1988, 4, 28))
assert old_commune == result
for period in [old_commune.start_date, datetime.date(1988, 4, 28), old_commune.end_date]:
assert Commune.objects.by_insee_code_and_period(99999, period) == old_commune

result = Commune.objects.by_insee_code_and_period(99999, datetime.datetime(2022, 11, 28))
assert new_commune == result
for period in [new_commune.start_date, datetime.date(2022, 11, 28), timezone.localdate()]:
assert Commune.objects.by_insee_code_and_period(99999, period) == new_commune
24 changes: 21 additions & 3 deletions tests/www/apply/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bs4 import BeautifulSoup
from dateutil.relativedelta import relativedelta
from django.contrib import messages
from django.db.models import Exists, OuterRef, Q
from django.template.defaultfilters import urlencode as urlencode_filter
from django.urls import reverse
from django.utils import timezone
Expand Down Expand Up @@ -2706,9 +2707,26 @@ def test_accept_updated_birthdate_invalidating_birth_place(self, client, mocker)
employer = self.company.members.first()
client.force_login(employer)

birth_place = Commune.objects.filter(start_date__gt=datetime.date(1901, 12, 1)).first()
assert birth_place is not None # required by test

birthdate = self.job_seeker.jobseeker_profile.birthdate
birth_place = (
Commune.objects.filter(
# The birthdate must be strictly greater than 1900-01-01, and we’re removing 1 day from start_date.
Q(start_date__gt=datetime.date(1900, 1, 2)),
# Must be a valid choice for the user current birthdate.
Q(start_date__lte=birthdate),
Q(end_date__gte=birthdate) | Q(end_date=None),
)
.exclude(
Exists(
# Must have a different code prior to start_date.
Commune.objects.exclude(pk=OuterRef("pk")).filter(
code=OuterRef("code"),
start_date__lt=OuterRef("start_date"),
)
)
)
.first()
)
early_date = birth_place.start_date - datetime.timedelta(days=1)
post_data = {
"birth_place": birth_place.pk,
Expand Down

0 comments on commit 36a63b5

Please sign in to comment.