Skip to content

Commit

Permalink
fix: simplified the api, removing class
Browse files Browse the repository at this point in the history
  • Loading branch information
castilhera committed Nov 28, 2024
1 parent b6b57d2 commit 3468beb
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 75 deletions.
16 changes: 11 additions & 5 deletions src/brazil_holidays/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from .models.holiday import Holiday
from .services.holiday import HolidayService
"""
Brazilian Holidays API
~~~~~~~~~~~~~~~~~~~~~
__all__ = [
"HolidayService", "Holiday"
]
Library to get the brazilian holidays.
"""
from .models import Holiday

from .api import (
get_easter_date,
get_national_holidays
)
60 changes: 60 additions & 0 deletions src/brazil_holidays/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from datetime import date, timedelta
from brazil_holidays.models import Holiday


def get_easter_date(year: int) -> date:
""" Get the easter date for a given year.
Meeus/Jones/Butcher Algorithm
Ref: https://pt.wikipedia.org/wiki/C%C3%A1lculo_da_P%C3%A1scoa
Args:
year (int): the year to get the easter date.
Returns:
date: the easter date.
"""
a = year % 19
b, c = divmod(year, 100)
h = (19 * a + b - b // 4 - (b - (b + 8) // 25 + 1) // 3 + 15) % 30
i, k = divmod(c, 4)
l = (32 + 2 * (b % 4) + 2 * i - h - k) % 7
m = (a + 11 * h + 22 * l) // 451
month, day = divmod(h + l - 7 * m + 114, 31)

return date(year, month, day+1)


def get_national_holidays(year: int) -> list[Holiday]:
""" Get a list of holidays in Brazil for a given year.
Args:
year (int): the year to get the holidays.
Returns:
list[Holiday]: a list of holiday (date, name).
"""
easter_date = get_easter_date(year)

holidays = [
Holiday(date(year, 1, 1), "Ano Novo"),
Holiday(date(year, 4, 21), "Tiradentes"),
Holiday(date(year, 5, 1), "Dia do Trabalho"),
Holiday(date(year, 9, 7), "Independência do Brasil"),
Holiday(date(year, 10, 12), "Nossa Senhora Aparecida"),
Holiday(date(year, 11, 2), "Finados"),
Holiday(date(year, 11, 15), "Proclamação da República"),
Holiday(date(year, 12, 25), "Natal"),
Holiday(easter_date, "Páscoa"),
Holiday(easter_date + timedelta(days=-47), "Carnaval"),
Holiday(easter_date + timedelta(days=-2), "Sexta-feira Santa"),
Holiday(easter_date + timedelta(days=60), "Corpus Christi")
]

if year > 2023:
holidays.append(Holiday(date(year, 11, 20), "Dia da Consciência Negra"))

holidays.sort(key=lambda h: h.date)

return holidays
File renamed without changes.
Empty file.
61 changes: 0 additions & 61 deletions src/brazil_holidays/services/holiday.py

This file was deleted.

22 changes: 13 additions & 9 deletions tests/test_holidays.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import unittest
from datetime import date
from brazil_holidays import HolidayService, Holiday
import brazil_holidays

class TestBrazilHolidays(unittest.TestCase):

def __init__(self, methodName: str = "runTest") -> None:
super().__init__(methodName)
self.service = HolidayService()

def test_get_brazil_national_holidays_2023_should_return_12(self):
holidays = self.service.get_brazil_national_holidays(2023)
holidays = brazil_holidays.get_national_holidays(2023)
self.assertEqual(len(holidays), 12)


def test_get_brazil_national_holidays_2024_should_return_13(self):
holidays = self.service.get_brazil_national_holidays(2024)
holidays = brazil_holidays.get_national_holidays(2024)
self.assertEqual(len(holidays), 13)


def test_get_brazil_national_holidays_should_contain_consciencia_negra(self):
year = 2024
holidays = self.service.get_brazil_national_holidays(year)
holidays = brazil_holidays.get_national_holidays(year)

self.assertIn(Holiday(date(year, 11, 20), "Dia da Consciência Negra"),
holidays)
self.assertIn(
brazil_holidays.Holiday(
date=date(year, 11, 20),
name="Dia da Consciência Negra"
),
holidays
)

if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 3468beb

Please sign in to comment.