"""Tests for ensure-artists HTTP clients.""" import pytest from pytest_mock import MockerFixture from src import config from src.client import ( bulk_ensure_artists, get_transfer_job, get_transfer_job_products, set_destination_artists, update_transfer_job, ) def test_get_transfer_job_calls_correct_service_and_path(mocker: MockerFixture) -> None: mock_get = mocker.patch.object(config.ows_client, "get") mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = {"destination_vendor_id": 99, "originating_artist_id": 42} result = get_transfer_job(42) assert result == {"destination_vendor_id": 99, "originating_artist_id": 42} mock_get.assert_called_once_with("ows-project-manager", path="/transfer/job/42") def test_get_transfer_job_returns_null_originating_artist_id(mocker: MockerFixture) -> None: mock_get = mocker.patch.object(config.ows_client, "get") mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = {"destination_vendor_id": 99, "originating_artist_id": None} result = get_transfer_job(42) assert result["originating_artist_id"] is None def test_get_transfer_job_raises_on_invalid_originating_artist_id(mocker: MockerFixture) -> None: mock_get = mocker.patch.object(config.ows_client, "get") mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = {"destination_vendor_id": 99, "originating_artist_id": "bad"} with pytest.raises(RuntimeError, match="originating_artist_id"): get_transfer_job(42) def test_get_transfer_job_products_calls_correct_service_and_path(mocker: MockerFixture) -> None: mock_get = mocker.patch.object(config.ows_client, "get") mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = [ { "release_id": 10, "source_artist_id": 101, "destination_artist_id": None, "source_video_artist_id": 500, "destination_video_artist_id": None, }, { "release_id": 11, "source_artist_id": 102, "destination_artist_id": 202, "source_video_artist_id": None, "destination_video_artist_id": None, }, ] result = get_transfer_job_products(42, 99) assert result == [ { "release_id": 10, "source_artist_id": 101, "destination_artist_id": None, "source_video_artist_id": 500, "destination_video_artist_id": None, }, { "release_id": 11, "source_artist_id": 102, "destination_artist_id": 202, "source_video_artist_id": None, "destination_video_artist_id": None, }, ] mock_get.assert_called_once_with( "ows-project-manager", path="/transfer/job/42/products", headers={ "Grass-Account-Type": "vendor", "Grass-Account-Id": "99", }, ) def test_bulk_ensure_artists_calls_correct_service_and_payload(mocker: MockerFixture) -> None: mock_post = mocker.patch.object(config.ows_client, "post") mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = {"mapping": {"101": 201, "102": 202}} result = bulk_ensure_artists([101, 102], 99) assert result == {"101": 201, "102": 202} mock_post.assert_called_once_with( "ows-artist", path="/artist/bulk-ensure", headers={ "Grass-Account-Type": "vendor", "Grass-Account-Id": "99", }, json={ "source_artist_ids": [101, 102], "destination_vendor_id": 99, }, ) def test_set_destination_artists_calls_correct_service_and_payload(mocker: MockerFixture) -> None: mock_patch = mocker.patch.object(config.ows_client, "patch") mock_patch.return_value.status_code = 200 mock_patch.return_value.json.return_value = {"updated_count": 2} result = set_destination_artists( 42, [ {"release_id": 10, "destination_artist_id": 201, "destination_video_artist_id": None}, {"release_id": 11, "destination_artist_id": 202, "destination_video_artist_id": 300}, ], ) assert result == {"updated_count": 2} mock_patch.assert_called_once_with( "ows-project-manager", path="/transfer/job/42/products", json={ "updates": [ {"release_id": 10, "destination_artist_id": 201, "destination_video_artist_id": None}, {"release_id": 11, "destination_artist_id": 202, "destination_video_artist_id": 300}, ] }, ) def test_bulk_ensure_artists_raises_on_error_status(mocker: MockerFixture) -> None: mock_post = mocker.patch.object(config.ows_client, "post") mock_post.return_value.status_code = 404 mock_post.return_value.text = "not found" mock_post.return_value.json.return_value = {"code": "not_found"} with pytest.raises(RuntimeError, match="status=404"): bulk_ensure_artists([101], 99) def test_update_transfer_job_calls_correct_service_and_payload(mocker: MockerFixture) -> None: mock_patch = mocker.patch.object(config.ows_client, "patch") mock_patch.return_value.status_code = 200 update_transfer_job(42, 201) mock_patch.assert_called_once_with( "ows-project-manager", path="/transfer/job/42", json={"destination_artist_id": 201}, ) def test_update_transfer_job_raises_on_error_status(mocker: MockerFixture) -> None: mock_patch = mocker.patch.object(config.ows_client, "patch") mock_patch.return_value.status_code = 404 mock_patch.return_value.text = "not found" with pytest.raises(RuntimeError, match="status=404"): update_transfer_job(42, 201)