"""Tests for contributors MySQL queries.""" from contextlib import nullcontext from datetime import datetime, timezone from uuid import UUID from pytest_mock import MockerFixture from contributor.queries.mysql.contributor import Contributor, get, get_by_uuids class TestGetContributor: def test_returns_contributor_dict_when_found(self, mocker: MockerFixture) -> None: session = mocker.Mock() execute_result = mocker.Mock() session.execute.return_value = execute_result created_at = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc) updated_at = datetime(2024, 1, 2, 12, 0, 0, tzinfo=timezone.utc) execute_result.scalar_one_or_none.return_value = Contributor( id=1, name="John Doe", vendor_id=100, subaccount_id=10, spotify_id="spotify123", apple_music_id="apple456", spotify_artist_key="A1B2C-D3E4F-G5H6I-J7K8L-M9N0P", neo4j_participant_uuid="uuid-1234", global_participant_uuid="gp-uuid-5678", artist_info_id=50, created_at=created_at, updated_at=updated_at, ) result = get(session=session, contributor_id=1) session.execute.assert_called_once() execute_result.scalar_one_or_none.assert_called_once_with() assert result == { "uuid": "uuid-1234", "id": 1, "name": "John Doe", "label": { "vendor_id": 100, "subaccount_id": 10, }, "spotify_id": "spotify123", "apple_music_id": "apple456", "spotify_artist_key": "A1B2C-D3E4F-G5H6I-J7K8L-M9N0P", "neo4j_participant_uuid": "uuid-1234", "global_participant": {"id": "gp-uuid-5678"}, "artist_info_ids": [50], "created_at": created_at, "updated_at": updated_at, } stmt = session.execute.call_args.args[0] where_clause = stmt.whereclause assert "id" in str(where_clause) def test_returns_none_when_not_found(self, mocker: MockerFixture) -> None: session = mocker.Mock() execute_result = mocker.Mock() session.execute.return_value = execute_result execute_result.scalar_one_or_none.return_value = None result = get(session=session, contributor_id=999) session.execute.assert_called_once() execute_result.scalar_one_or_none.assert_called_once_with() assert result is None def test_uses_wrapped_session_when_not_passed(self, mocker: MockerFixture) -> None: session = mocker.Mock() execute_result = mocker.Mock() session.execute.return_value = execute_result created_at = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc) updated_at = datetime(2024, 1, 2, 12, 0, 0, tzinfo=timezone.utc) execute_result.scalar_one_or_none.return_value = Contributor( id=2, name="Jane Smith", vendor_id=101, subaccount_id=0, spotify_id=None, apple_music_id="apple789", spotify_artist_key=None, neo4j_participant_uuid=None, global_participant_uuid=None, artist_info_id=None, created_at=created_at, updated_at=updated_at, ) ar_db_session_mock = mocker.patch( "contributor.connectors.mysql.ar_db_session", return_value=nullcontext(session), ) result = get(contributor_id=2) ar_db_session_mock.assert_called_once_with() assert result == { "uuid": None, "id": 2, "name": "Jane Smith", "label": { "vendor_id": 101, "subaccount_id": 0, }, "spotify_id": None, "apple_music_id": "apple789", "spotify_artist_key": None, "neo4j_participant_uuid": None, "global_participant": None, "artist_info_ids": [None], "created_at": created_at, "updated_at": updated_at, } class TestGetByUuids: def test_returns_contributors_with_global_participant_id( self, mocker: MockerFixture ) -> None: session = mocker.Mock() execute_result = mocker.Mock() session.execute.return_value = execute_result created_at = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc) updated_at = datetime(2024, 1, 2, 12, 0, 0, tzinfo=timezone.utc) contributor_with_gp = Contributor( id=1, name="Artist One", vendor_id=100, subaccount_id=10, spotify_id="spotify001", apple_music_id=None, spotify_artist_key="Q1W2E-R3T4Y-U5I6O-P7A8S-D9F0G", neo4j_participant_uuid="uuid-1001", global_participant_uuid="gp-uuid-a1b2c3d4", artist_info_id=50, created_at=created_at, updated_at=updated_at, ) contributor_without_gp = Contributor( id=2, name="Artist Two", vendor_id=100, subaccount_id=10, spotify_id=None, apple_music_id="apple002", spotify_artist_key=None, neo4j_participant_uuid="uuid-1002", global_participant_uuid=None, artist_info_id=51, created_at=created_at, updated_at=updated_at, ) execute_result.scalars.return_value.all.return_value = [ contributor_with_gp, contributor_without_gp, ] uuids = [UUID("11111111-2222-3333-4444-555555555555")] result = get_by_uuids(session=session, contributor_uuids=uuids) assert len(result) == 2 assert result[0]["id"] == 1 assert result[0]["spotify_artist_key"] == "Q1W2E-R3T4Y-U5I6O-P7A8S-D9F0G" assert result[0]["global_participant"] == {"id": "gp-uuid-a1b2c3d4"} assert result[1]["id"] == 2 assert result[1]["spotify_artist_key"] is None assert result[1]["global_participant"] is None