"""Integration tests for GET /transfer/job//attachments.""" import requests from .helpers import TIMEOUT, create_job, delete_job, url class TestGetTransferJobAttachments: """Tests for GET /transfer/job//attachments.""" def test_returns_upcs_and_isrcs_for_job( self, qa_base_url, auth_headers, qa_project_with_tracks): """Returns 200 with the release UPC and all track ISRCs for the job.""" job = create_job(qa_base_url, auth_headers, qa_project_with_tracks["project_id"]) job_id = job["project_transfer_job_id"] try: r = requests.get( url(qa_base_url, f"/transfer/job/{job_id}/attachments"), headers=auth_headers, timeout=TIMEOUT, ) assert r.status_code == 200 body = r.json() assert body["upcs"] == [qa_project_with_tracks["upc"]] assert set(body["isrcs"]) == set(qa_project_with_tracks["isrcs"]) finally: delete_job(qa_base_url, auth_headers, job_id) def test_returns_multiple_upcs_and_isrcs( self, qa_base_url, auth_headers, qa_project_multiple_releases, db_connection): """Returns 200 with one UPC per release and one ISRC per seeded track.""" job = create_job(qa_base_url, auth_headers, qa_project_multiple_releases["project_id"]) job_id = job["project_transfer_job_id"] release_ids = qa_project_multiple_releases["release_ids"] isrcs = [] try: with db_connection.cursor() as cur: for release_id in release_ids: isrc = f"TST{release_id:09d}" isrcs.append(isrc) cur.execute( "INSERT INTO track (release_id, isrc) VALUES (%s, %s)", (release_id, isrc), ) cur.execute( "SELECT CAST(upc AS CHAR) AS upc FROM releases " "WHERE release_id IN %s AND upc != 0", (tuple(release_ids),), ) expected_upcs = {row["upc"] for row in cur.fetchall()} db_connection.commit() r = requests.get( url(qa_base_url, f"/transfer/job/{job_id}/attachments"), headers=auth_headers, timeout=TIMEOUT, ) assert r.status_code == 200 body = r.json() assert set(body["upcs"]) == expected_upcs assert set(body["isrcs"]) == set(isrcs) finally: with db_connection.cursor() as cur: cur.execute("DELETE FROM track WHERE release_id IN %s", (tuple(release_ids),)) db_connection.commit() delete_job(qa_base_url, auth_headers, job_id) # ------------------------------------------------------------------ # Filtering / exclusion # ------------------------------------------------------------------ def test_release_with_no_tracks_returns_empty_isrcs( self, qa_base_url, auth_headers, qa_project): """Returns 200 with isrcs=[] when the release has no track rows.""" job = create_job(qa_base_url, auth_headers, qa_project["project_id"]) job_id = job["project_transfer_job_id"] try: r = requests.get( url(qa_base_url, f"/transfer/job/{job_id}/attachments"), headers=auth_headers, timeout=TIMEOUT, ) assert r.status_code == 200 body = r.json() assert isinstance(body["upcs"], list) assert body["isrcs"] == [] finally: delete_job(qa_base_url, auth_headers, job_id) def test_tracks_with_null_or_empty_isrc_excluded( self, qa_base_url, auth_headers, qa_project, db_connection): """Returns 200 with isrcs=[] when all tracks have null or empty ISRCs.""" job = create_job(qa_base_url, auth_headers, qa_project["project_id"]) job_id = job["project_transfer_job_id"] release_id = qa_project["release_id"] try: with db_connection.cursor() as cur: cur.execute( "INSERT INTO track (release_id, isrc) VALUES (%s, NULL), (%s, '')", (release_id, release_id), ) db_connection.commit() r = requests.get( url(qa_base_url, f"/transfer/job/{job_id}/attachments"), headers=auth_headers, timeout=TIMEOUT, ) assert r.status_code == 200 body = r.json() assert isinstance(body["upcs"], list) assert body["isrcs"] == [] finally: delete_job(qa_base_url, auth_headers, job_id) def test_soft_deleted_snapshot_rows_excluded( self, qa_base_url, auth_headers, qa_project_with_tracks, db_connection): """Returns 200 with upcs=[] and isrcs=[] when all snapshot rows are soft-deleted.""" job = create_job(qa_base_url, auth_headers, qa_project_with_tracks["project_id"]) job_id = job["project_transfer_job_id"] try: with db_connection.cursor() as cur: cur.execute( "UPDATE product_transfer_history SET deleted_at = NOW() WHERE job_id = %s", (job_id,), ) db_connection.commit() r = requests.get( url(qa_base_url, f"/transfer/job/{job_id}/attachments"), headers=auth_headers, timeout=TIMEOUT, ) assert r.status_code == 200 body = r.json() assert body["upcs"] == [] assert body["isrcs"] == [] finally: delete_job(qa_base_url, auth_headers, job_id) # ------------------------------------------------------------------ # Error cases # ------------------------------------------------------------------ def test_nonexistent_job_returns_404(self, qa_base_url, auth_headers): """Returns 404 when the job does not exist.""" r = requests.get( url(qa_base_url, "/transfer/job/999999999/attachments"), headers=auth_headers, timeout=TIMEOUT, ) assert r.status_code == 404 def test_soft_deleted_job_returns_404(self, qa_base_url, auth_headers, qa_project): """Returns 404 when the job has been soft-deleted.""" job = create_job(qa_base_url, auth_headers, qa_project["project_id"]) job_id = job["project_transfer_job_id"] delete_job(qa_base_url, auth_headers, job_id) r = requests.get( url(qa_base_url, f"/transfer/job/{job_id}/attachments"), headers=auth_headers, timeout=TIMEOUT, ) assert r.status_code == 404