"""Functional tests for POST /artist/bulk-ensure.""" import json import pytest from artist.constants import errors from artist.models.artist import Artist from tests.testutils import db SOURCE_VENDOR_ID = 10 DEST_VENDOR_ID = 99 @pytest.fixture def source_artists(): return [ Artist(artist_id=101, name='Artist One', artist_type='artist', vendor_id=SOURCE_VENDOR_ID), Artist(artist_id=102, name='Artist Two', artist_type='film_collection', vendor_id=SOURCE_VENDOR_ID), ] @pytest.fixture def valid_bulk_ensure_body(): return { 'source_artist_ids': [101, 102], 'destination_vendor_id': DEST_VENDOR_ID, } @db.test_schema def test_bulk_ensure_returns_mapping( client, valid_headers_for_vendor, source_artists, valid_bulk_ensure_body): """Returns 200 with a source→destination artist ID mapping.""" db.seed_models(source_artists) res = client.post( '/artist/bulk-ensure', headers=valid_headers_for_vendor, data=json.dumps(valid_bulk_ensure_body)) res_json = json.loads(res.data.decode('utf-8')) assert res.status_code == 200 mapping = res_json.get('mapping') assert isinstance(mapping, dict) assert set(mapping.keys()) == {'101', '102'} for dest_id in mapping.values(): assert isinstance(dest_id, int) @db.test_schema def test_bulk_ensure_is_idempotent( client, valid_headers_for_vendor, source_artists, valid_bulk_ensure_body): """Calling twice returns the same mapping without creating duplicates.""" db.seed_models(source_artists) res1 = client.post( '/artist/bulk-ensure', headers=valid_headers_for_vendor, data=json.dumps(valid_bulk_ensure_body)) res2 = client.post( '/artist/bulk-ensure', headers=valid_headers_for_vendor, data=json.dumps(valid_bulk_ensure_body)) mapping1 = json.loads(res1.data.decode('utf-8')).get('mapping') mapping2 = json.loads(res2.data.decode('utf-8')).get('mapping') assert res1.status_code == 200 assert res2.status_code == 200 assert mapping1 == mapping2 @db.test_schema def test_bulk_ensure_source_artist_not_found( client, valid_headers_for_vendor): """Returns 404 when a source artist ID does not exist.""" res = client.post( '/artist/bulk-ensure', headers=valid_headers_for_vendor, data=json.dumps({'source_artist_ids': [99999], 'destination_vendor_id': DEST_VENDOR_ID})) assert res.status_code == 404 def test_bulk_ensure_missing_source_artist_ids(client, valid_headers_for_vendor): """Returns 400 when source_artist_ids is absent.""" res = client.post( '/artist/bulk-ensure', headers=valid_headers_for_vendor, data=json.dumps({'destination_vendor_id': DEST_VENDOR_ID})) res_json = json.loads(res.data.decode('utf-8')) assert res.status_code == 400 assert res_json.get('code') == errors.VALIDATION_ERROR def test_bulk_ensure_empty_source_artist_ids(client, valid_headers_for_vendor): """Returns 400 when source_artist_ids is an empty list.""" res = client.post( '/artist/bulk-ensure', headers=valid_headers_for_vendor, data=json.dumps({'source_artist_ids': [], 'destination_vendor_id': DEST_VENDOR_ID})) res_json = json.loads(res.data.decode('utf-8')) assert res.status_code == 400 assert res_json.get('code') == errors.VALIDATION_ERROR def test_bulk_ensure_missing_destination_vendor_id(client, valid_headers_for_vendor): """Returns 400 when destination_vendor_id is absent.""" res = client.post( '/artist/bulk-ensure', headers=valid_headers_for_vendor, data=json.dumps({'source_artist_ids': [101]})) res_json = json.loads(res.data.decode('utf-8')) assert res.status_code == 400 assert res_json.get('code') == errors.VALIDATION_ERROR