"""Test for Collaborator Persister.""" from collections import namedtuple from http import HTTPStatus from unittest.mock import ANY from oto import response, status import pytest from collaborator.constants import error from collaborator.constants.collaborator import CollaboratorType from collaborator.models.rds.collaborator import Collaborator from collaborator.models.rds.collaborator_persister import CollaboratorPersister from collaborator.utils.error import OwsError from collaborator.utils.typing import Account from tests.testutils import db @db.test_schema_default_seed @pytest.mark.parametrize( ( "account, name, subaccount_id, participant_id, collaborator_type, " "currency, description, internal_id, performance_rights, created_by" ), [ ( Account("vendor", "24601"), "New email", None, "6060842", "COLLABORATOR", "USD", "Mastering Engineer", "ABC-123", True, 1, ), ( Account("subaccount", "55555"), "New vendor", 55555, "2", "SUBACCOUNT", "GBP", "Mixing Engineer", "ABC-123", False, 2, ), ], ) def test_create_collaborator_success( account, name, subaccount_id, participant_id, collaborator_type, currency, description, internal_id, performance_rights, created_by, ): """Test successfully creating a collaborator.""" result = CollaboratorPersister.create_collaborator( name, account, subaccount_id, participant_id, collaborator_type, currency, description, internal_id, performance_rights, created_by, ) new_entry = db.get_collaborator(participant_id, account) data, created = result assert created is True assert data == {**new_entry, "vendor_id": str(new_entry["vendor_id"])} @db.test_schema_default_seed @pytest.mark.parametrize( "account, name, subaccount_id, collaborator_type, currency, created_by", [(Account("vendor", "9876"), "New Subaccount", 123456, "SUBACCOUNT", "USD", 1)], ) def test_create_collaborator_exists( account, name, subaccount_id, collaborator_type, currency, created_by ): """Test creating a collaborator that exists.""" result = CollaboratorPersister.create_collaborator( name, account, subaccount_id, None, collaborator_type, currency, None, None, True, created_by, ) entry = db.get_subaccount_collaborator(subaccount_id, account) data, created = result assert created is False assert data == entry SearchByNameTest = namedtuple( "SearchByNameTest", [ "account", "collaborator_ids", "search_term", "has_recipient", "collaborator_type", "match_term", "expected_results", ], ) @db.test_schema_default_seed @pytest.mark.parametrize( SearchByNameTest._fields, [ SearchByNameTest( account=Account("vendor", "24601"), collaborator_ids=None, search_term="person", has_recipient=False, collaborator_type=None, match_term=False, expected_results=[ { "id": 2, "name": "Another Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12346", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), SearchByNameTest( account=Account("vendor", "24601"), collaborator_ids=None, search_term="derp", has_recipient=False, collaborator_type=None, match_term=True, expected_results=[], ), SearchByNameTest( account=Account("vendor", "24601"), collaborator_ids=None, search_term="derp", has_recipient=False, collaborator_type=None, match_term=False, expected_results=[], ), SearchByNameTest( account=Account("vendor", "9876"), collaborator_ids=None, search_term="Subaccount", has_recipient=False, collaborator_type=None, match_term=False, expected_results=[ { "id": 8, "name": "Subaccount", "vendor_id": 9876, "performance_rights": True, "subaccount_id": 123456, "participant_id": None, "recipient_id": 90, "currency": "USD", "collaborator_type": "SUBACCOUNT", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, } ], ), SearchByNameTest( account=Account("vendor", "9876"), collaborator_ids=None, search_term="Subaccount", has_recipient=False, collaborator_type=CollaboratorType.COLLABORATOR, match_term=False, expected_results=[], ), SearchByNameTest( account=Account("subaccount", "55555"), collaborator_ids=None, search_term="son", has_recipient=False, collaborator_type=None, match_term=False, expected_results=[], ), SearchByNameTest( account=Account("subaccount", "55555"), collaborator_ids=None, search_term="son", has_recipient=False, collaborator_type=None, match_term=False, expected_results=[], ), SearchByNameTest( account=Account("vendor", "6666"), collaborator_ids=None, search_term="recipient", has_recipient=True, collaborator_type=None, match_term=False, expected_results=[ { "id": 7, "name": "recipient", "vendor_id": 6666, "subaccount_id": 123456, "participant_id": "10", "performance_rights": True, "recipient_id": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 11, "name": "recipient again", "vendor_id": 6666, "subaccount_id": 123456, "participant_id": None, "performance_rights": True, "recipient_id": 4, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), SearchByNameTest( account=Account("vendor", "6666"), collaborator_ids=None, search_term="recipient", has_recipient=True, collaborator_type=None, match_term=True, expected_results=[ { "id": 7, "name": "recipient", "vendor_id": 6666, "subaccount_id": 123456, "participant_id": "10", "performance_rights": True, "recipient_id": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), SearchByNameTest( account=Account("vendor", "24601"), collaborator_ids=None, search_term="person", has_recipient=False, collaborator_type=None, match_term=True, expected_results=[ { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), SearchByNameTest( account=Account("vendor", "24601"), collaborator_ids=None, search_term="perso", has_recipient=False, collaborator_type=None, match_term=True, expected_results=[], ), SearchByNameTest( account=None, collaborator_ids=None, search_term="person", has_recipient=False, collaborator_type=None, match_term=False, expected_results=[ { "id": 2, "name": "Another Person", "vendor_id": 24601, "subaccount_id": None, "participant_id": "12346", "performance_rights": False, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 4, "name": "Another Person", "vendor_id": 90210, "subaccount_id": None, "participant_id": "12348", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 10, "name": "Another Person", "vendor_id": 120594, "subaccount_id": None, "participant_id": "12348", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 1, "name": "Person", "vendor_id": 24601, "subaccount_id": None, "participant_id": "12345", "performance_rights": False, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 3, "name": "Person", "vendor_id": 90210, "subaccount_id": None, "participant_id": "12347", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-423", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), SearchByNameTest( account=None, collaborator_ids=[1, 4, 10], search_term=None, has_recipient=False, collaborator_type=None, match_term=False, expected_results=[ { "id": 4, "name": "Another Person", "vendor_id": 90210, "subaccount_id": None, "participant_id": "12348", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 10, "name": "Another Person", "vendor_id": 120594, "subaccount_id": None, "participant_id": "12348", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 1, "name": "Person", "vendor_id": 24601, "subaccount_id": None, "participant_id": "12345", "performance_rights": False, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), ], ) def test_search_by_name( account, collaborator_ids, search_term, has_recipient, collaborator_type, match_term, expected_results, ): """Test get collaborators based on an account where entries exist.""" result = CollaboratorPersister.search( vendor_ids=[account.id] if account else None, collaborator_ids=collaborator_ids, search_term=search_term, dimensions=[Collaborator.name], has_recipient=has_recipient, collaborator_type=collaborator_type, offset=0, limit=None, exactly_match_term=match_term, ) items = result.message["items"] assert type(result) is response.Response assert result.status == status.OK assert items == expected_results @db.test_schema_default_seed @pytest.mark.parametrize( "collaborator_id, account, expected_result", [ ( 1, Account("vendor", "24601"), { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ( 6, Account("subaccount", "90210"), { "id": 6, "name": "4", "vendor_id": 90210, "performance_rights": True, "subaccount_id": 1234, "participant_id": "1", "recipient_id": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ], ) def test_get_by_id_and_account_success(collaborator_id, account, expected_result): """Test getting a collaborator based on an ID and account ID.""" result = CollaboratorPersister.get_by_id_and_account(collaborator_id, account) result["created_date"] = "now" assert result == expected_result @db.test_schema_default_seed @pytest.mark.parametrize( "collaborator_id, account", [ (1337, Account("vendor", "24601")), (1, Account("subaccount", "90210")), ], ) def test_get_by_id_and_account_fail(collaborator_id, account): """Test failing to get a collaborator based on an ID and account ID.""" with pytest.raises(OwsError) as exc_info: CollaboratorPersister.get_by_id_and_account(collaborator_id, account) assert exc_info.value.status == HTTPStatus.NOT_FOUND @db.test_schema_default_seed @pytest.mark.parametrize( "recipient_ids, expected", [ ( [11112222], { 11112222: { "id": 7, "name": "recipient", "vendor_id": 6666, "performance_rights": True, "subaccount_id": 123456, "participant_id": "10", "recipient_id": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, } }, ), ( [11112222, 22223333, 33334444, 44445555], { 11112222: { "id": 7, "name": "recipient", "vendor_id": 6666, "performance_rights": True, "subaccount_id": 123456, "participant_id": "10", "recipient_id": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, 22223333: { "id": 6, "name": "4", "vendor_id": 90210, "performance_rights": True, "subaccount_id": 1234, "participant_id": "1", "recipient_id": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, 33334444: { "collaborator_type": "SUBACCOUNT", "currency": "USD", "description": None, "id": 9, "internal_id": None, "name": "The number of the beast", "participant_id": None, "performance_rights": True, "recipient_id": 3, "subaccount_id": 123456, "vendor_id": 10003, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, 44445555: { "id": 11, "name": "recipient again", "vendor_id": 6666, "subaccount_id": 123456, "participant_id": None, "performance_rights": True, "recipient_id": 4, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, }, ), ([1337], {}), ], ) def test_get_by_transferwise_recipient_ids(recipient_ids, expected): """Test getting collaborators by recipient IDs.""" results = CollaboratorPersister.get_by_transferwise_recipient_ids(recipient_ids) for recipient_id in recipient_ids: if ( results and recipient_id in results and "created_date" in results[recipient_id] ): results[recipient_id]["created_date"] = "now" assert results == expected @db.test_schema_default_seed @pytest.mark.parametrize( "account, limit, offset, has_recipient, expected_results", [ ( Account("vendor", "24601"), 0, 0, None, { "items": [ { "id": 2, "name": "Another Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12346", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( Account("vendor", "24601"), 1, 0, None, { "items": [ { "id": 2, "name": "Another Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12346", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( Account("vendor", "24601"), 0, 1, None, { "items": [ { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( Account("subaccount", "55555"), 0, 0, None, { "items": [], "pagination": { "type": "standard", "total_records": 0, }, }, ), ( Account("vendor", "6666"), 0, 0, True, { "items": [ { "id": 7, "name": "recipient", "vendor_id": 6666, "performance_rights": True, "subaccount_id": 123456, "participant_id": "10", "recipient_id": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 11, "name": "recipient again", "vendor_id": 6666, "performance_rights": True, "subaccount_id": 123456, "participant_id": None, "recipient_id": 4, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( Account("vendor", "24601"), 0, 0, True, { "items": [], "pagination": { "type": "standard", "total_records": 0, }, }, ), ], ) def test_get_for_account(account, limit, offset, has_recipient, expected_results): """Test get collaborators based on an account where entries exist.""" result = CollaboratorPersister.get_for_account( account, limit, offset, has_recipient ) assert type(result) is response.Response assert result.status == status.OK assert result.message == expected_results @pytest.mark.parametrize( "subaccount_id, expected", [ ( 123456, { "id": 8, "name": "Subaccount", "vendor_id": 9876, "subaccount_id": 123456, "participant_id": None, "performance_rights": True, "recipient_id": 90, "currency": "USD", "collaborator_type": "SUBACCOUNT", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), (654321, None), ], ) @db.test_schema_default_seed def test_get_subaccount_collaborator(subaccount_id, expected): """Test getting a subaccount collaborator.""" result = CollaboratorPersister.get_subaccount_collaborator( Account("vendor", 9876), subaccount_id ) if result and "created_date" in result: result["created_date"] = "now" assert result == expected @pytest.mark.parametrize( "account, collaborator_ids, expected_result", [ ( Account("vendor", "24601"), None, [ { "id": 1, "name": "Person", "vendor_id": 24601, "splits": 4, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 2, "name": "Another Person", "vendor_id": 24601, "splits": 2, "performance_rights": False, "subaccount_id": None, "participant_id": "12346", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), ( Account("vendor", "90210"), None, [ { "id": 3, "name": "Person", "vendor_id": 90210, "splits": 0, "subaccount_id": None, "participant_id": "12347", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-423", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 4, "name": "Another Person", "vendor_id": 90210, "splits": 1, "subaccount_id": None, "participant_id": "12348", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 5, "name": "Third", "vendor_id": 90210, "splits": 2, "subaccount_id": None, "participant_id": "12349", "performance_rights": False, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-223", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 6, "name": "4", "vendor_id": 90210, "splits": 0, "subaccount_id": 1234, "participant_id": "1", "performance_rights": True, "recipient_id": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), ( Account("vendor", "12345"), None, [], ), ( Account("vendor", "90210"), [3, 6], [ { "id": 3, "name": "Person", "vendor_id": 90210, "splits": 0, "subaccount_id": None, "participant_id": "12347", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-423", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 6, "name": "4", "vendor_id": 90210, "splits": 0, "subaccount_id": 1234, "participant_id": "1", "performance_rights": True, "recipient_id": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), ( None, None, [ { "id": 0, "name": "totally unique name", "vendor_id": 99876, "performance_rights": False, "subaccount_id": None, "participant_id": "99876", "recipient_id": None, "splits": 0, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "splits": 4, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 2, "name": "Another Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "recipient_id": None, "participant_id": "12346", "splits": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 3, "name": "Person", "vendor_id": 90210, "performance_rights": True, "subaccount_id": None, "recipient_id": None, "participant_id": "12347", "splits": 0, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-423", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 4, "name": "Another Person", "vendor_id": 90210, "performance_rights": True, "subaccount_id": None, "participant_id": "12348", "recipient_id": None, "splits": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 5, "name": "Third", "vendor_id": 90210, "performance_rights": False, "subaccount_id": None, "recipient_id": None, "participant_id": "12349", "splits": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-223", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 6, "name": "4", "vendor_id": 90210, "performance_rights": True, "subaccount_id": 1234, "recipient_id": 2, "participant_id": "1", "splits": 0, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 7, "name": "recipient", "participant_id": "10", "performance_rights": True, "recipient_id": 1, "splits": 0, "subaccount_id": 123456, "vendor_id": 6666, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 8, "collaborator_type": "SUBACCOUNT", "currency": "USD", "name": "Subaccount", "participant_id": None, "performance_rights": True, "recipient_id": 90, "splits": 0, "subaccount_id": 123456, "vendor_id": 9876, "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 9, "name": "The number of the beast", "vendor_id": 10003, "performance_rights": True, "subaccount_id": 123456, "participant_id": None, "recipient_id": 3, "currency": "USD", "collaborator_type": "SUBACCOUNT", "splits": 0, "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 10, "name": "Another Person", "vendor_id": 120594, "performance_rights": True, "subaccount_id": None, "participant_id": "12348", "recipient_id": None, "splits": 0, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 11, "name": "recipient again", "participant_id": None, "performance_rights": True, "recipient_id": 4, "splits": 0, "subaccount_id": 123456, "vendor_id": 6666, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ], ), ], ) @db.test_schema_default_seed def test_get_with_split_count(account, collaborator_ids, expected_result): """Test getting collaborators with their splits counts.""" result = CollaboratorPersister.get_with_split_count( account=account, collaborator_ids=collaborator_ids ) assert result == expected_result @pytest.mark.parametrize( "collaborator_id, payload, expected_result", [ ( 5, {"performance_rights": True}, { "id": 5, "name": "Third", "vendor_id": 90210, "subaccount_id": None, "participant_id": "12349", "performance_rights": True, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-223", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ( 3, {"performance_rights": False}, { "id": 3, "name": "Person", "vendor_id": 90210, "subaccount_id": None, "participant_id": "12347", "performance_rights": False, "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-423", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ], ) @db.test_schema_default_seed def test_update_collaborator_success(collaborator_id, payload, expected_result): """Test update collaborator.""" result = CollaboratorPersister.update_collaborator(collaborator_id, payload) result["created_date"] = "now" assert result == expected_result @pytest.mark.parametrize( "collaborator_id, payload", [(99, {"performance_rights": False}), (20, {"performance_rights": False})], ) @db.test_schema_default_seed def test_update_collaborator_failure(collaborator_id, payload): """Test update collaborator with failure.""" with pytest.raises(OwsError) as exc_info: CollaboratorPersister.update_collaborator(collaborator_id, payload) assert exc_info.value.status == HTTPStatus.NOT_FOUND @db.test_schema_default_seed def test_update_collaborators(): """Test updating collaborators.""" data = { 1: {"name": "Newname"}, 2: {"recipient_id": 123}, } result = CollaboratorPersister.update_collaborators(data) assert result == [ { "id": 1, "name": "Newname", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 2, "name": "Another Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12346", "recipient_id": 123, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ] @db.test_schema_default_seed def test_update_collaborators_fail(): """Test failing to update collaborators.""" data = { 1: {"name": "Newname"}, 2: {"recipient_id": 123}, 1337: {"name": "does not exist"}, } with pytest.raises(OwsError) as err: CollaboratorPersister.update_collaborators(data) assert err.value.code == error.ERROR_CODE_COLLABORATOR_NOT_FOUND assert err.value.message == error.ERROR_MESSAGE_COLLABORATOR_NOT_FOUND @db.test_schema_default_seed @pytest.mark.parametrize( "collaborator_id, expected_result", [ ( 1, { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ( 6, { "id": 6, "name": "4", "vendor_id": 90210, "performance_rights": True, "subaccount_id": 1234, "participant_id": "1", "recipient_id": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ], ) def test_get_by_id(collaborator_id, expected_result): """Test getting a collaborator based on an ID.""" result = CollaboratorPersister.get_by_id(collaborator_id) result["created_date"] = "now" assert result == expected_result @db.test_schema_default_seed def test_get_by_id_not_found(): """Test getting a collaborator based on an ID not found.""" collaborator_id = 100 with pytest.raises(OwsError) as err: CollaboratorPersister.get_by_id(collaborator_id) assert err.value.code == error.ERROR_CODE_COLLABORATOR_NOT_FOUND assert err.value.message == error.ERROR_MESSAGE_COLLABORATOR_NOT_FOUND @db.test_schema_default_seed def test_get_by_ids(): """Test getting a collaborator based on IDs.""" collaborator_ids = [1, 6] expected_result = [ { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 6, "name": "4", "vendor_id": 90210, "performance_rights": True, "subaccount_id": 1234, "participant_id": "1", "recipient_id": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ] result = CollaboratorPersister.get_by_ids(collaborator_ids, throw_if_not_found=True) assert result == expected_result @db.test_schema_default_seed def test_get_by_ids_not_found(): """Test getting a collaborator based on IDs not found.""" collaborator_ids = [100] with pytest.raises(OwsError) as err: CollaboratorPersister.get_by_ids(collaborator_ids, throw_if_not_found=True) assert err.value.code == error.ERROR_CODE_COLLABORATOR_NOT_FOUND assert err.value.message == error.ERROR_MESSAGE_COLLABORATOR_NOT_FOUND @db.test_schema_default_seed def test_get_by_recipient_id(): """Test getting a collaborator based on a recipient_id.""" recipient_id = [3] expected_result = [ { "id": 9, "name": "The number of the beast", "vendor_id": 10003, "performance_rights": True, "subaccount_id": 123456, "participant_id": None, "recipient_id": 3, "currency": "USD", "collaborator_type": "SUBACCOUNT", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, } ] result = CollaboratorPersister.get_by_recipient_ids(recipient_id) result[0]["created_date"] = "now" assert result == expected_result @db.test_schema_default_seed def test_get_by_recipient_id_multiple_ids(): """Test getting a collaborator based on a recipient_id.""" recipient_id = [1, 2] expected_result = [ { "id": 7, "name": "recipient", "vendor_id": 6666, "subaccount_id": 123456, "participant_id": "10", "performance_rights": True, "recipient_id": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 6, "name": "4", "vendor_id": 90210, "subaccount_id": 1234, "participant_id": "1", "performance_rights": True, "recipient_id": 2, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ] result = CollaboratorPersister.get_by_recipient_ids(recipient_id) result[0]["created_date"] = "now" result[1]["created_date"] = "now" assert result == expected_result @pytest.mark.parametrize( "vendor_id, expected_result", [ (24601, [1]), (940512, []), ], ) @db.test_schema_default_seed def test_get_collaborators_with_activity(vendor_id, expected_result): """Test fetching collaborators with activity.""" assert ( CollaboratorPersister.get_with_statement_activity(vendor_id) == expected_result )