class DatesApplication(IDatesApplication): @inject def __init__(self, period_gateway: IPeriodsGateway): self.period_gateway = period_gateway self.date_utils = DateUtils() def get_initial_and_missing_dates(self): try: time_period = self.period_gateway.get() period_dates = self._get_period_dates(time_period.begin_date, time_period.end_date) time_period.output_dates = period_dates except GatewayException as e: raise DatesApplicationException() from e else: return time_period def _get_period_dates(self, begin_date: str, end_date: str): generator = DatesGenerator( self.date_utils.get_date_from_date_str(begin_date), self.date_utils.get_date_from_date_str(end_date)) return generator.generate()
class TestDateUtils(TestCase): def setUp(self): self.utils = DateUtils() def test_get_date_from_date_str_when_date_str_then_return_datetime_obj( self): date_str = '1970-02-01' result = self.utils.get_date_from_date_str(date_str) self.assertEqual(1, result.day) self.assertEqual(2, result.month) self.assertEqual(1970, result.year) def test_sum_one_month_to_date_when_date_then_return_that_date_one_month_on_future( self): input_date = datetime(1970, 1, 1) result = self.utils.sum_one_month_to_date(input_date) self.assertEqual(1, result.day) self.assertEqual(2, result.month) self.assertEqual(1970, result.year)
class PeriodsGateway(IPeriodsGateway): @inject def __init__(self, previred_api: IPreviredApi): self.previred_api = previred_api self.date_utils = DateUtils() def get(self): try: result = self.previred_api.get_periodo() input_dates = [] for date_str in result['fechas']: input_dates.append(self.date_utils.get_date_from_date_str(date_str)) except InfrastructureException as e: raise GatewayException() from e return TimePeriod( result['id'], result['fechaCreacion'], result['fechaFin'], input_dates )