"""Test Abacus State by-target /dataloader route functional.""" import pytest from abacus_state.constants.constants import DATALOADER_BATCH_LIMIT from tests.utils.factories import AbacusStateFactory pytestmark = [pytest.mark.db('mysql')] PARENT = 'statement_period_adjustment_file' def test_dataload_states_by_target_returns_ordered_states(fixture_client): """Dataloader returns one ordered entry per requested parent id.""" AbacusStateFactory.create( parent_table_name=PARENT, parent_table_id=1, action_name='upload_file', action_status='complete', ) AbacusStateFactory.create( parent_table_name=PARENT, parent_table_id=1, action_name='approve_file', action_status='init', ) AbacusStateFactory.create( parent_table_name=PARENT, parent_table_id=2, action_name='upload_file', action_status='complete', ) # Parent id not requested must not appear. AbacusStateFactory.create( parent_table_name=PARENT, parent_table_id=99, action_name='upload_file', action_status='complete', ) # kebab-case in the URL is normalised to snake_case by the route. res = fixture_client.post( '/abacus-state/statement-period-adjustment-file/dataloader', json=[1, 2, 3] ) assert res.status_code == 200 items = res.json['items'] # One entry per requested id, in order: id 1 has two states, id 2 one, id 3 none. assert len(items) == 3 assert [s['parent_table_id'] for s in items[0]['data']] == [1, 1] assert [s['parent_table_id'] for s in items[1]['data']] == [2] assert items[2]['data'] is None def test_dataload_states_by_target_empty_body_returns_400(fixture_client): """An empty id list is rejected.""" res = fixture_client.post( '/abacus-state/statement-period-adjustment-file/dataloader', json=[] ) assert res.status_code == 400 def test_dataload_states_by_target_over_limit_returns_400(fixture_client): """More than the batch limit of ids is rejected with 400.""" res = fixture_client.post( '/abacus-state/statement-period-adjustment-file/dataloader', json=list(range(DATALOADER_BATCH_LIMIT + 1)), ) assert res.status_code == 400