Skip to content

Commit

Permalink
fix(api): arruma função de ano/período atual que tinha incosistência …
Browse files Browse the repository at this point in the history
…nos dias 30/31 de dez (#186)

* test(utils): check if boundaries of func works well

* fix(utils): correct 'if' func return at 30/31 dec
  • Loading branch information
mateusvrs authored Jan 2, 2024
1 parent 19b8297 commit c95d870
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
16 changes: 6 additions & 10 deletions api/utils/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,16 @@ def get_session_cookie(session: Session) -> cookies.RequestsCookieJar: # pragma:
return cookie_jar

"""Obtem o ano e o período atual e retorna uma lista com esses valores."""
def get_current_year_and_period(date: datetime | None = None) -> List[str | str]:
current_date = datetime.now()
if date is not None:
current_date = date
def get_current_year_and_period(date: datetime | None = datetime.now()) -> List[str | str]:
if date is None:
date = datetime.now()

current_year = current_date.year
current_year = date.year
period = "1"

# Se a data atual estiver entre 1 de maio e 30 de dezembro, o período é 2.
if datetime(current_year, 5, 1) <= current_date <= datetime(current_year, 12, 30):
# Se a data atual estiver entre 2 de maio e 1 de janeiro do próximo ano, o período é 2.
if datetime(current_year, 5, 2) <= date <= datetime(current_year + 1, 1, 1):
period = "2"
# elif current_date > datetime(current_year, 10, 30):
# current_year += 1
# period = "1"

return [str(current_year), period]

Expand Down
52 changes: 43 additions & 9 deletions api/utils/tests/test_date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,61 @@
from utils import sessions as sns
from datetime import datetime


class DateUtilsTest(APITestCase):

def compare_date(self, date: datetime):
before_dec = datetime(date.year, 12, 30) >= date
after_may = datetime(date.year, 5, 1) <= date
before_dec = datetime(date.year + 1, 1, 1) >= date
after_may = datetime(date.year, 5, 2) <= date

return before_dec and after_may

def change_date(self, date: datetime):
month = 2 if self.compare_date(date) else 6
return datetime(date.year + self.compare_date(date), month=month, day=1)

def test_get_current_year_and_period(self):
year, period = sns.get_current_year_and_period()
def test_get_current_year_and_period_with_right_may_limits(self):
year, period = sns.get_current_year_and_period(datetime(2023, 5, 1))

date = datetime.now()
self.assertEqual(year, str(date.year))
self.assertEqual("2023", year)
self.assertEqual("1", period)

year, period = sns.get_current_year_and_period(datetime(2023, 5, 2))

self.assertEqual("2023", year)
self.assertEqual("2", period)

def test_get_current_year_and_period_with_left_jan_limits(self):
year, period = sns.get_current_year_and_period(datetime(2023, 12, 31))

self.assertEqual("2023", year)
self.assertEqual("2", period)

year, period = sns.get_current_year_and_period(datetime(2024, 1, 1))

self.assertEqual("2024", year)
self.assertEqual("1", period)

def test_get_current_year_and_period_with_middle_date(self):
year, period = sns.get_current_year_and_period(datetime(2023, 2, 18))

self.assertEqual((period == '2'), self.compare_date(date))
self.assertEqual((period == '1'), not self.compare_date(date))
self.assertEqual("2023", year)
self.assertEqual("1", period)

year, period = sns.get_current_year_and_period(datetime(2023, 9, 20))

self.assertEqual("2023", year)
self.assertEqual("2", period)

year, period = sns.get_current_year_and_period(datetime(2024, 4, 23))

self.assertEqual("2024", year)
self.assertEqual("1", period)

year, period = sns.get_current_year_and_period(datetime(2024, 7, 9))

self.assertEqual("2024", year)
self.assertEqual("2", period)

def test_get_next_period(self):
_, period = sns.get_next_period()
Expand Down Expand Up @@ -70,4 +105,3 @@ def test_get_previous_period_with_year(self):
year, period = sns.get_previous_period(date)

self.assertEqual(str(date.year - (period == '2')), year)

0 comments on commit c95d870

Please sign in to comment.