Exemplo n.º 1
0
    def test_create_period_report(self, mocker, report_input_dto,
                                  ad_entry_dto):
        ad_repository_mock = mocker.patch(
            'src.business.adbusiness.AdRepository')
        report_dto_mock = mocker.patch('src.business.adbusiness.ReportDTO')
        calculate_total_mock = mocker.patch(
            'src.business.adbusiness.calculate_total')
        calculate_total_mock.return_value = 16000, 11200, 1344, 192,

        entries = [ad_entry_dto for i in range(5)]
        ad_repository_mock().find_ads_by_period.return_value = entries

        report_input_dto.client = None

        adreg = AdReg()
        result = adreg.create_report(report_input_dto)

        adreg.data.find_all_ads.assert_not_called()
        adreg.data.find_ads_by_client.assert_not_called()
        adreg.data.find_ads_by_client_and_period.assert_not_called()

        adreg.data.find_ads_by_period.assert_called_once_with(report_input_dto)

        # calculate total per entry
        calculate_total_calls_args = [
            call[0][0] for call in calculate_total_mock.call_args_list
        ]
        for m in entries:
            assert m in calculate_total_calls_args

        # Return list of ReportDTO
        assert result == [report_dto_mock.from_ad_entry() for i in range(5)]
Exemplo n.º 2
0
class AdService:
    def __init__(self):
        self.adreg = AdReg()

    def add(self,
            name=None,
            client=None,
            start=None,
            end=None,
            investment=None):
        add_schema.validate({
            'name': name,
            'client': client,
            'start': start,
            'end': end,
            'investment': investment,
        })

        ad_input_dto = AdInputDTO(name=name,
                                  client=client,
                                  start=datetime.strptime(start,
                                                          '%d-%m-%Y').date(),
                                  end=datetime.strptime(end,
                                                        '%d-%m-%Y').date(),
                                  investment=investment)

        self.adreg.create_adentry(ad_input_dto)

    def report(self, client=None, start=None, end=None):
        report_schema.validate({
            'client': client,
            'start': start,
            'end': end,
        })

        if start is None and end is None:
            report_input_dto = ReportInputDTO(client=client, )
        elif start is None and end is not None:
            report_input_dto = ReportInputDTO(
                client=client,
                end=datetime.strptime(end, '%d-%m-%Y').date(),
            )
        elif start is not None and end is None:
            report_input_dto = ReportInputDTO(
                client=client,
                start=datetime.strptime(start, '%d-%m-%Y').date(),
            )
        else:
            report_input_dto = ReportInputDTO(
                client=client,
                start=datetime.strptime(start, '%d-%m-%Y').date(),
                end=datetime.strptime(end, '%d-%m-%Y').date(),
            )

        reports = self.adreg.create_report(report_input_dto)

        return reports