"""Functional tests for statement_period_payment_entity.""" from unittest.mock import patch from royalties.tests.utils.factories import ( StatementPeriodFactory, StatementPeriodPaymentEntityFactory, ) def test_get_by_statement_period(fixture_client, reference_payment_entity_fixtures): """Test that we get the correct records for the given statement period.""" statement_period = StatementPeriodFactory.create() records = [ StatementPeriodPaymentEntityFactory.create( reference_payment_entity_id=1, statement_period=statement_period ), StatementPeriodPaymentEntityFactory.create( reference_payment_entity_id=2, statement_period=statement_period ), ] object_id = statement_period.statement_period_id response = fixture_client.get(f'/statement-period/{object_id}/payment-entities') res_json = response.json assert response.status_code == 200 assert len(res_json) == 2 assert res_json[0] == { 'statement_period_payment_entity_id': records[ 0 ].statement_period_payment_entity_id, 'statement_period_id': statement_period.statement_period_id, 'reference_payment_entity_id': records[0].reference_payment_entity_id, 'is_visible_to_customer': False, } def test_set_statement_period_payment_entity_is_visible( fixture_client, reference_payment_entity_fixtures ): """Test that we can update a record's is_visible_to_customer.""" record = StatementPeriodPaymentEntityFactory.create(payment_entity_id=1) response = fixture_client.put( f'/statement-period/{record.statement_period_id}/payment-entity/' f'{record.payment_entity_id}/visible' ) assert response.status_code == 200 assert response.json == { 'statement_period_payment_entity_id': record.statement_period_payment_entity_id, 'statement_period_id': record.statement_period_id, 'reference_payment_entity_id': record.reference_payment_entity_id, 'is_visible_to_customer': True, } def test_set_statement_period_payment_entity_is_visible( fixture_client, reference_payment_entity_fixtures, ): """Test that we can update the is_visible_to_customer field to True.""" record = StatementPeriodPaymentEntityFactory.create(reference_payment_entity_id=3) response = fixture_client.put( f'/statement-period/{record.statement_period_id}/payment-entity/' f'{record.reference_payment_entity_id}/visible' ) assert response.status_code == 200 assert response.json == { 'statement_period_payment_entity_id': record.statement_period_payment_entity_id, 'statement_period_id': record.statement_period_id, 'reference_payment_entity_id': record.reference_payment_entity_id, 'is_visible_to_customer': True, } def test_get_statement_period_payment_entity( fixture_client, reference_payment_entity_fixtures ): """Test get statement period payment entity by ID.""" record = StatementPeriodPaymentEntityFactory.create(reference_payment_entity_id=1) response = fixture_client.get( f'/statement-period-payment-entity/{record.statement_period_payment_entity_id}' ) assert response.status_code == 200 assert response.json == { 'statement_period_payment_entity_id': record.statement_period_payment_entity_id, 'statement_period_id': record.statement_period_id, 'reference_payment_entity_id': record.reference_payment_entity_id, 'is_visible_to_customer': record.is_visible_to_customer, } def test_get_states_by_statement_period( fixture_client, reference_payment_entity_fixtures, payment_entities_actions_fixtures, ): """Test getting statement_period_payment_entity states by statement_period.""" statement_period_id = 300 statement_period = StatementPeriodFactory.create( statement_period_id=statement_period_id ) for i in [1, 2, 3, 4]: StatementPeriodPaymentEntityFactory.create( statement_period_payment_entity_id=i, reference_payment_entity_id=i, statement_period=statement_period, ) res = fixture_client.get( f'/statement-period/{statement_period_id}/payment-entities/states' ) assert res.status_code == 200 assert all(item['statement_period_payment_entity_id'] for item in res.json) assert all(item['reference_payment_entity_id'] for item in res.json) assert all(item['statement_period_id'] == statement_period_id for item in res.json) assert all(item['abacus_state_id'] for item in res.json) assert all(item['action_name'] for item in res.json) assert all(item['action_status'] for item in res.json)