"""Test ows-payment requests.""" import random import typing from faker import Faker import httpx from owsclient.test import OwsClientMock import pytest from src.connectors import ows_payment from src.connectors.exceptions import OwsPaymentException from src.constants import TaxCorrectionStatuses, TaxCorrectionTypes from src.models import ( NewTaxCorrection, NewTaxCorrectionVAT, TaxCorrection, TaxCorrectionVAT, ) from tests.unit.factories import ( NewTaxCorrectionFactory, NewTaxCorrectionVATFactory, TaxCorrectionFactory, TaxCorrectionVATFactory, ) def test_get_tax_corrections_success( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test get_tax_corrections success.""" correction_type: TaxCorrectionTypes = random.choice( # type: ignore list(TaxCorrectionTypes) ) correction_status: TaxCorrectionStatuses = random.choice( # type: ignore list(TaxCorrectionStatuses) ) limit = faker.pyint() offset = faker.pyint() tax_correction: TaxCorrection = TaxCorrectionFactory.build() path = ( f'/tax-corrections/{correction_type.value}/{correction_status.value}' f'/?limit={limit}&offset={offset}' ) ows_client_mock.post( 'ows-payment', path, json={ 'filters': { 'correction_statement_period_id': tax_correction.correction_statement_period_id, 'contract_ids': [tax_correction.contract_id], } }, ).mock( return_value=httpx.Response( 200, json={'items': [tax_correction.model_dump(mode='json')], 'total_count': 1}, ) ) res = ows_payment.get_tax_corrections( correction_type, correction_status, tax_correction.correction_statement_period_id, [tax_correction.contract_id], limit, offset, ) assert res.items == [tax_correction] assert res.total_count == 1 def test_get_tax_corrections_failure( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test get_tax_corrections failure.""" correction_type: TaxCorrectionTypes = random.choice( # type: ignore list(TaxCorrectionTypes) ) correction_status: TaxCorrectionStatuses = random.choice( # type: ignore list(TaxCorrectionStatuses) ) limit = faker.pyint() offset = faker.pyint() tax_correction: TaxCorrection = TaxCorrectionFactory.build() path = ( f'/tax-corrections/{correction_type.value}/{correction_status.value}' f'/?limit={limit}&offset={offset}' # noqa ) ows_client_mock.post('ows-payment', path).mock( return_value=httpx.Response( 400, text='error', ) ) with pytest.raises(OwsPaymentException, match='ERROR in POST /tax-corrections/'): ows_payment.get_tax_corrections( correction_type, correction_status, tax_correction.correction_statement_period_id, [tax_correction.contract_id], limit, offset, ) def test_get_tax_corrections_vat_success( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test get_tax_corrections_vat success.""" correction_status: TaxCorrectionStatuses = random.choice( # type: ignore list(TaxCorrectionStatuses) ) limit = faker.pyint() offset = faker.pyint() tax_correction_vat: TaxCorrectionVAT = TaxCorrectionVATFactory.build() path = ( f'/tax-corrections/vat/{correction_status.value}' f'/?limit={limit}&offset={offset}' # noqa ) ows_client_mock.post( 'ows-payment', path, json={ 'filters': { 'correction_statement_period_id': tax_correction_vat.correction_statement_period_id, 'contract_ids': [tax_correction_vat.contract_id], } }, ).mock( return_value=httpx.Response( 200, json={ 'items': [tax_correction_vat.model_dump(mode='json')], 'total_count': 1, }, ) ) res = ows_payment.get_tax_corrections_vat( correction_status, tax_correction_vat.correction_statement_period_id, [tax_correction_vat.contract_id], limit, offset, ) assert res.items == [tax_correction_vat] assert res.total_count == 1 def test_get_tax_corrections_vat_failure( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test get_tax_corrections_vat failure.""" correction_status: TaxCorrectionStatuses = random.choice( # type: ignore list(TaxCorrectionStatuses) ) limit = faker.pyint() offset = faker.pyint() tax_correction_vat: TaxCorrectionVAT = TaxCorrectionVATFactory.build() path = ( f'/tax-corrections/vat/{correction_status.value}' f'/?limit={limit}&offset={offset}' # noqa ) ows_client_mock.post('ows-payment', path).mock( return_value=httpx.Response( 400, text='error', ) ) with pytest.raises( OwsPaymentException, match='ERROR in POST /tax-corrections/vat/' ): ows_payment.get_tax_corrections_vat( correction_status, tax_correction_vat.correction_statement_period_id, [tax_correction_vat.contract_id], limit, offset, ) def test_delete_tax_corrections_success( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test delete_tax_corrections success.""" worksheet_tax_correction_ids = faker.pylist(value_types=[int]) path = '/tax-corrections/bulk' ows_client_mock.delete( 'ows-payment', path, json={'worksheet_tax_correction_ids': worksheet_tax_correction_ids}, ).mock( return_value=httpx.Response(204), ) ows_payment.delete_tax_corrections(worksheet_tax_correction_ids) def test_delete_tax_corrections_failure( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test delete_tax_corrections failure.""" worksheet_tax_correction_ids = faker.pylist(value_types=[int]) path = '/tax-corrections/bulk' ows_client_mock.delete( 'ows-payment', path, json={'worksheet_tax_correction_ids': worksheet_tax_correction_ids}, ).mock( return_value=httpx.Response(400), ) with pytest.raises( OwsPaymentException, match='ERROR in DELETE /tax-corrections/bulk' ): ows_payment.delete_tax_corrections(worksheet_tax_correction_ids) def test_delete_tax_corrections_vat_success( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test delete_tax_corrections_vat success.""" worksheet_tax_correction_vat_ids = faker.pylist(value_types=[int]) path = '/tax-corrections/vat/bulk' ows_client_mock.delete( 'ows-payment', path, json={'worksheet_tax_correction_vat_ids': worksheet_tax_correction_vat_ids}, ).mock( return_value=httpx.Response(204), ) ows_payment.delete_tax_corrections_vat(worksheet_tax_correction_vat_ids) def test_delete_tax_corrections_vat_failure( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test delete_tax_corrections_vat failure.""" worksheet_tax_correction_vat_ids = faker.pylist(value_types=[int]) path = '/tax-corrections/vat/bulk' ows_client_mock.delete( 'ows-payment', path, json={'worksheet_tax_correction_vat_ids': worksheet_tax_correction_vat_ids}, ).mock( return_value=httpx.Response(400), ) with pytest.raises( OwsPaymentException, match='ERROR in DELETE /tax-corrections/vat/bulk' ): ows_payment.delete_tax_corrections_vat(worksheet_tax_correction_vat_ids) def test_create_tax_corrections_success( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test create_tax_corrections method success.""" tax_corrections: typing.List[NewTaxCorrection] = NewTaxCorrectionFactory.batch( faker.pyint(1, 10) ) path = '/tax-corrections/bulk' ows_client_mock.post( 'ows-payment', path, json=[ correction.model_dump(mode='json', exclude={'correction_type'}) for correction in tax_corrections ], ).mock( return_value=httpx.Response(201, json={'message': 'OK'}), ) ows_payment.create_tax_corrections(tax_corrections) def test_create_tax_corrections_failure( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test create_tax_corrections method failure.""" tax_corrections: typing.List[NewTaxCorrection] = NewTaxCorrectionFactory.batch( faker.pyint(1, 10) ) path = '/tax-corrections/bulk' ows_client_mock.post( 'ows-payment', path, ).mock( return_value=httpx.Response(400), ) with pytest.raises( OwsPaymentException, match='ERROR in POST /tax-corrections/bulk' ): ows_payment.create_tax_corrections(tax_corrections) def test_create_tax_corrections_vat_success( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test create_tax_corrections_vat method success.""" tax_corrections_vat: typing.List[NewTaxCorrectionVAT] = ( NewTaxCorrectionVATFactory.batch(faker.pyint(1, 10)) ) path = '/tax-corrections/vat/bulk' ows_client_mock.post( 'ows-payment', path, json=[ correction.model_dump(mode='json', by_alias=True) for correction in tax_corrections_vat ], ).mock( return_value=httpx.Response(201, json={'message': 'OK'}), ) ows_payment.create_tax_corrections_vat(tax_corrections_vat) def test_create_tax_corrections_vat_failure( ows_client_mock: OwsClientMock, faker: Faker ) -> None: """Test create_tax_corrections_vat method failure.""" tax_corrections_vat: typing.List[NewTaxCorrectionVAT] = ( NewTaxCorrectionVATFactory.batch(faker.pyint(1, 10)) ) path = '/tax-corrections/vat/bulk' ows_client_mock.post('ows-payment', path).mock( return_value=httpx.Response(400), ) with pytest.raises( OwsPaymentException, match='ERROR in POST /tax-corrections/vat/bulk' ): ows_payment.create_tax_corrections_vat(tax_corrections_vat)