"""Participation roles router tests.""" from uuid import UUID import pytest from fastapi.testclient import TestClient from pytest_mock import MockerFixture ROLE_UUID = "1f8c7569-5a91-4c67-afd4-14b736afd4a8" @pytest.fixture def role_categories() -> list[dict]: return [ { "uuid": "550e8400-e29b-41d4-a716-446655440000", "name": "Original Artist", }, { "uuid": "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee", "name": "Other Label Personnel", }, ] class TestGetRoleCategories: def test_returns_role_categories( self, mocker: MockerFixture, test_client: TestClient, role_categories: list[dict], identity_uuid: str, ) -> None: get_role_categories_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_role_categories", return_value=role_categories, ) response = test_client.get("/participation-roles/categories") get_role_categories_mock.assert_called_once_with( neo4j_session=mocker.ANY, identity_id=UUID(identity_uuid), ) assert response.status_code == 200 assert response.json() == role_categories @pytest.fixture def role_data() -> dict: return { "uuid": ROLE_UUID, "apple_role_name": "Composer", "category": { "uuid": "550e8400-e29b-41d4-a716-446655440000", "name": "Songwriter", }, "ddex_role_name": "Composer", "name": "Composer", } @pytest.fixture def roles_data(role_data: dict) -> list[dict]: return [ role_data, { "uuid": "2bb43e8f-ae65-47fc-b590-c510172ecbee", "apple_role_name": "Lyricist", "category": role_data["category"], "ddex_role_name": "Lyricist", "name": "Lyricist", }, ] class TestGetRoleByUuid: def test_returns_role( self, mocker: MockerFixture, test_client: TestClient, role_data: dict, identity_uuid: str, ) -> None: get_role_by_uuid_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_role_by_uuid", return_value=role_data, ) response = test_client.get(f"/participation-roles/{ROLE_UUID}") get_role_by_uuid_mock.assert_called_once_with( neo4j_session=mocker.ANY, uuid=UUID(ROLE_UUID), identity_id=UUID(identity_uuid), ) assert response.status_code == 200 assert response.json() == role_data def test_returns_404_when_not_found( self, mocker: MockerFixture, test_client: TestClient, ) -> None: mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_role_by_uuid", return_value=None, ) response = test_client.get(f"/participation-roles/{ROLE_UUID}") assert response.status_code == 404 class TestGetRolesByCategoryUuid: def test_returns_roles( self, mocker: MockerFixture, test_client: TestClient, roles_data: list[dict], identity_uuid: str, ) -> None: get_roles_by_category_uuids_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_roles_by_category_uuids", return_value=[ { "category_uuid": ROLE_UUID, "roles": roles_data, } ], ) get_role_categories_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_role_categories", return_value=[ { "uuid": ROLE_UUID, "name": "Songwriter", } ], ) response = test_client.get(f"/participation-roles/categories/{ROLE_UUID}/roles") get_roles_by_category_uuids_mock.assert_called_once_with( neo4j_session=mocker.ANY, uuids=[ROLE_UUID], identity_id=UUID(identity_uuid), ) get_role_categories_mock.assert_called_once_with( neo4j_session=mocker.ANY, identity_id=UUID(identity_uuid), ) assert response.status_code == 200 assert response.json() == { "category": {"uuid": ROLE_UUID, "name": "Songwriter"}, "roles": roles_data, } def test_returns_empty_list_when_no_roles( self, mocker: MockerFixture, test_client: TestClient, identity_uuid: str, ) -> None: get_roles_by_category_uuids_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_roles_by_category_uuids", return_value=[ { "category_uuid": ROLE_UUID, "roles": [], } ], ) get_role_categories_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_role_categories", return_value=[ { "uuid": ROLE_UUID, "name": "Test Category", } ], ) response = test_client.get(f"/participation-roles/categories/{ROLE_UUID}/roles") get_roles_by_category_uuids_mock.assert_called_once_with( neo4j_session=mocker.ANY, uuids=[ROLE_UUID], identity_id=UUID(identity_uuid), ) get_role_categories_mock.assert_called_once_with( neo4j_session=mocker.ANY, identity_id=UUID(identity_uuid), ) assert response.status_code == 200 assert response.json() == { "category": {"uuid": ROLE_UUID, "name": "Test Category"}, "roles": [], } class TestGetBulkRolesByCategory: def test_returns_multiple_categories( self, mocker: MockerFixture, test_client: TestClient, roles_data: list[dict], identity_uuid: str, ) -> None: second_category_uuid = "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee" second_role = { "uuid": "3cc54f9c-bf76-48cd-9ace-d621cf8da9cf", "apple_role_name": "Producer", "category": { "uuid": second_category_uuid, "name": "Producer", }, "ddex_role_name": "Producer", "name": "Producer", } get_roles_by_category_uuids_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_roles_by_category_uuids", return_value=[ { "category_uuid": ROLE_UUID, "roles": roles_data, }, { "category_uuid": second_category_uuid, "roles": [second_role], }, ], ) get_role_categories_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_role_categories", return_value=[ { "uuid": ROLE_UUID, "name": "Songwriter", }, { "uuid": second_category_uuid, "name": "Producer", }, ], ) response = test_client.post( "/participation-roles/categories/roles/dataloader", json={ "categories": [ {"uuid": ROLE_UUID}, {"uuid": second_category_uuid}, ] }, ) get_roles_by_category_uuids_mock.assert_called_once_with( neo4j_session=mocker.ANY, uuids=[ROLE_UUID, second_category_uuid], identity_id=UUID(identity_uuid), ) get_role_categories_mock.assert_called_once_with( neo4j_session=mocker.ANY, identity_id=UUID(identity_uuid), ) assert response.status_code == 200 assert response.json() == [ { "category": {"uuid": ROLE_UUID, "name": "Songwriter"}, "roles": roles_data, }, { "category": {"uuid": second_category_uuid, "name": "Producer"}, "roles": [second_role], }, ] def test_returns_empty_list_when_no_results( self, mocker: MockerFixture, test_client: TestClient, identity_uuid: str, ) -> None: get_roles_by_category_uuids_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_roles_by_category_uuids", return_value=[], ) get_role_categories_mock = mocker.patch( "contributor.api.routers.participation_roles.participation_role_logic.get_role_categories", return_value=[], ) response = test_client.post( "/participation-roles/categories/roles/dataloader", json={"categories": [{"uuid": ROLE_UUID}]}, ) get_roles_by_category_uuids_mock.assert_called_once_with( neo4j_session=mocker.ANY, uuids=[ROLE_UUID], identity_id=UUID(identity_uuid), ) get_role_categories_mock.assert_called_once_with( neo4j_session=mocker.ANY, identity_id=UUID(identity_uuid), ) assert response.status_code == 200 assert response.json() == []