"""Functional tests for reference_transaction_type_group.""" from abacus_common_logic.constants.error import ERROR_ENTITY_DOES_NOT_EXIST from abacus_contract.constants.constants import REFERENCE_TRANSACTION_TYPE_GROUP_ADMIN from abacus_contract.constants.error import ERROR_INVALID_GROUP_ADMIN from tests.utils.factories import ReferenceTransactionTypeGroupFactory from tests.utils.factories import ReferenceTransactionTypeGroupTransactionTypeFactory def test_get_groups_by_group_admin(fixture_client): """Test getting a list of reference_transaction_type_groups by group_admin.""" contract_admin = REFERENCE_TRANSACTION_TYPE_GROUP_ADMIN.CONTRACT_ADMIN group_names = ['Transaction Type Group 1', 'Transaction Type Group 2'] for group_name in group_names: group = ReferenceTransactionTypeGroupFactory.create( transaction_type_group_name=group_name ) ReferenceTransactionTypeGroupTransactionTypeFactory.create( reference_transaction_type_group=group, reference_transaction_type_group_admin=contract_admin ) res = fixture_client.get( f'/reference-transaction-type-group/group-admin/{contract_admin}' ) assert res.status_code == 200 assert len(res.json) == len(group_names) assert all(grp['reference_transaction_type_group_id'] for grp in res.json) assert all(grp['transaction_type_group_name'] for grp in res.json) def test_get_by_group_admin_error(fixture_client): """Test getting a list of transaction type groups with invalid group_admin.""" fake_group_admin = 'fake' res = fixture_client.get( f'/reference-transaction-type-group/group-admin/{fake_group_admin}' ) assert res.status_code == 404 assert res.json['message'] == ERROR_INVALID_GROUP_ADMIN.format( group_admin=fake_group_admin, group_admins=', '.join(REFERENCE_TRANSACTION_TYPE_GROUP_ADMIN) ) def test_get_transaction_types_by_group(fixture_client): """Test getting a list of transaction types that belong to a particular group.""" group = ReferenceTransactionTypeGroupFactory.create() group_id = group.reference_transaction_type_group_id transaction_type_groups = ReferenceTransactionTypeGroupTransactionTypeFactory \ .create_batch(5, reference_transaction_type_group=group) res = fixture_client.get( f'/reference-transaction-type-group/{group_id}/reference-transaction-types' ) assert res.status_code == 200 assert len(res.json) == len(transaction_type_groups) assert all(tt['txn_type_id'] for tt in res.json) assert all(tt['txn_type_code'] for tt in res.json) assert all(tt['txn_type_name'] for tt in res.json) def test_get_transaction_types_by_group_error(fixture_client): """Test getting list of transaction types with invalid group_id.""" group_id = 123 res = fixture_client.get( f'/reference-transaction-type-group/{group_id}/reference-transaction-types' ) assert res.status_code == 404 assert res.json['message'] == ERROR_ENTITY_DOES_NOT_EXIST.format( object_type='ReferenceTransactionTypeGroup', object_id=group_id ) def test_get_transaction_type_group_transaction_types(fixture_client): """Test getting a list of transaction types and their transaction group.""" transaction_type_groups = ReferenceTransactionTypeGroupTransactionTypeFactory \ .create_batch(10) res = fixture_client.get( '/reference-transaction-type-group/reference_transaction_type_group_transaction_types' # noqa ) assert res.status_code == 200 assert len(res.json) == len(transaction_type_groups) assert all( tt['reference_transaction_type_group_transaction_type_id'] for tt in res.json ) assert all(tt['reference_transaction_type_group_id'] for tt in res.json) assert all(tt['reference_transaction_type_id'] for tt in res.json) assert all(tt['reference_transaction_type_group_admin'] for tt in res.json) def test_get_transaction_type_groups(fixture_client): """Test getting a list of all transaction type groups.""" transaction_type_groups = ReferenceTransactionTypeGroupFactory \ .create_batch(10) res = fixture_client.get( '/reference-transaction-type-groups' ) assert res.status_code == 200 assert len(res.json) == len(transaction_type_groups) assert all(tt['reference_transaction_type_group_id'] for tt in res.json) assert all(tt['transaction_type_group_name'] for tt in res.json)