"""Functional tests for Ledger Correction.""" from ledger.constants.constants import CORRECTION_TYPES from ledger.constants.error import ( ERROR_LEDGER_CORRECTION_RECORD_ALREADY_EXISTS, ERROR_POST_REQUEST_DUPLICATE_RECORDS, ) from tests.utils.factories import LedgerCorrectionFactory def test_bulk_create_ledger_correction( fixture_client, mock_event_fixtures, mock_worksheet_correction, mock_bulk_ledger_correction_request_body, ): """Test to create bulk Ledger Correction entries.""" post_body = mock_bulk_ledger_correction_request_body res = fixture_client.post('/ledger-correction/bulk', json=post_body) assert res.status_code == 201 items = sorted(res.json, key=lambda x: x['ledger_correction_id']) assert items == [ { 'ledger_correction_id': 1, 'abacus_event_id': 10, 'worksheet_correction_id': 1, 'account_id': 1, 'contract_id': 1, 'statement_period_id': 1, 'correction_statement_period_id': 2, 'correction_type': CORRECTION_TYPES.ROYALTY_REVERSAL, 'gross_revenue': '-900.00', 'distribution_fee': '10.00', 'mechanical_deduction_total': None, 'mechanical_deduction_admin_fee_total': None, 'net_revenue': '-890.00', 'currency_code': 'USD', 'note': None, }, { 'ledger_correction_id': 2, 'abacus_event_id': 10, 'worksheet_correction_id': 2, 'account_id': 50, 'contract_id': 2, 'statement_period_id': 1, 'correction_statement_period_id': 2, 'correction_type': CORRECTION_TYPES.ROYALTY_REVERSAL, 'gross_revenue': '-671.10', 'distribution_fee': '0.00', 'mechanical_deduction_total': '-56.11', 'mechanical_deduction_admin_fee_total': '-10.09', 'net_revenue': '-671.10', 'currency_code': 'USD', 'note': None, }, ] def test_bulk_create_ledger_record_already_exist( fixture_client, mock_event_fixtures, mock_worksheet_correction, mock_bulk_ledger_correction_request_body, ): """Test to create Ledger Correction if record already exist.""" LedgerCorrectionFactory.create() post_body = mock_bulk_ledger_correction_request_body res = fixture_client.post('/ledger-correction/bulk', json=post_body) assert res.status_code == 400 assert res.json['message'] == ERROR_LEDGER_CORRECTION_RECORD_ALREADY_EXISTS.format( [1] ) def test_bulk_create_ledger_record_non_unique_data(fixture_client): """Test to create Ledger Correction. if POST request body contains non-unique data. """ post_body = [ { 'abacus_event_id': 10, 'worksheet_correction_id': 1, 'account_id': 1, 'contract_id': 1, 'statement_period_id': 1, 'correction_statement_period_id': 2, 'correction_type': 'royalty_reversal', 'currency_code': 'USD', 'gross_revenue': -900.00, 'distribution_fee': 10.00, 'net_revenue': -890.00, }, { 'abacus_event_id': 10, 'worksheet_correction_id': 2, 'account_id': 50, 'contract_id': 2, 'statement_period_id': 1, 'correction_statement_period_id': 2, 'correction_type': 'royalty_reversal', 'currency_code': 'USD', 'gross_revenue': -671.10, 'distribution_fee': 0.00, 'net_revenue': -671.10, }, { 'abacus_event_id': 10, 'worksheet_correction_id': 1, 'account_id': 1, 'contract_id': 1, 'statement_period_id': 1, 'correction_statement_period_id': 2, 'correction_type': 'royalty_reversal', 'currency_code': 'USD', 'gross_revenue': -900.00, 'distribution_fee': 10.00, 'net_revenue': -890.00, }, ] res = fixture_client.post('/ledger-correction/bulk', json=post_body) assert res.status_code == 400 assert res.json['message'] == ERROR_POST_REQUEST_DUPLICATE_RECORDS.format( {'worksheet_correction_id': 1} )