"""Tests for sync service""" import pytest from unittest.mock import AsyncMock, patch from app.services.sync import ( extract_country_from_radarr, extract_country_from_sonarr, extract_country_from_lidarr, upsert_media_item, ) def test_extract_country_from_radarr_with_production_countries(): """Test extracting country from Radarr movie with productionCountries""" movie = { "id": 1, "title": "Test Movie", "productionCountries": [{"iso_3166_1": "US"}] } country = extract_country_from_radarr(movie) assert country == "US" def test_extract_country_from_radarr_with_metadata(): """Test extracting country from Radarr movie with movieMetadata""" movie = { "id": 1, "title": "Test Movie", "movieMetadata": { "productionCountries": [{"iso_3166_1": "GB"}] } } country = extract_country_from_radarr(movie) assert country == "GB" def test_extract_country_from_radarr_no_country(): """Test extracting country from Radarr movie with no country""" movie = { "id": 1, "title": "Test Movie" } country = extract_country_from_radarr(movie) assert country is None def test_extract_country_from_sonarr_with_metadata(): """Test extracting country from Sonarr series with seriesMetadata""" series = { "id": 1, "title": "Test Show", "seriesMetadata": { "originCountry": ["US"] } } country = extract_country_from_sonarr(series) assert country == "US" def test_extract_country_from_sonarr_string_country(): """Test extracting country from Sonarr series with string country""" series = { "id": 1, "title": "Test Show", "seriesMetadata": { "originCountry": "US" } } country = extract_country_from_sonarr(series) assert country == "US" def test_extract_country_from_lidarr_with_country(): """Test extracting country from Lidarr artist with country field""" artist = { "id": 1, "artistName": "Test Artist", "country": "US" } country = extract_country_from_lidarr(artist) assert country == "US" def test_extract_country_from_lidarr_no_country(): """Test extracting country from Lidarr artist with no country""" artist = { "id": 1, "artistName": "Test Artist" } country = extract_country_from_lidarr(artist) assert country is None @pytest.mark.asyncio async def test_upsert_media_item_new(test_db_pool): """Test upserting a new media item""" # Temporarily replace the global pool import app.core.database original_pool = app.core.database.pool app.core.database.pool = test_db_pool try: media_id = await upsert_media_item( source_kind="radarr", source_item_id=1, title="Test Movie", year=2020, media_type="movie", arr_raw={"id": 1, "title": "Test Movie", "productionCountries": [{"iso_3166_1": "US"}]} ) assert media_id is not None # Verify it was inserted async with test_db_pool.connection() as conn: async with conn.cursor() as cur: await cur.execute(""" SELECT title, year, media_type FROM moviemap.media_item WHERE id = %s """, (media_id,)) result = await cur.fetchone() assert result is not None assert result[0] == "Test Movie" assert result[1] == 2020 assert result[2] == "movie" finally: app.core.database.pool = original_pool @pytest.mark.asyncio async def test_upsert_media_item_with_country(test_db_pool): """Test upserting a media item with country association""" # Temporarily replace the global pool import app.core.database original_pool = app.core.database.pool app.core.database.pool = test_db_pool try: media_id = await upsert_media_item( source_kind="radarr", source_item_id=1, title="Test Movie", year=2020, media_type="movie", arr_raw={"id": 1, "title": "Test Movie", "productionCountries": [{"iso_3166_1": "US"}]} ) # Verify country was associated async with test_db_pool.connection() as conn: async with conn.cursor() as cur: await cur.execute(""" SELECT country_code FROM moviemap.media_country WHERE media_item_id = %s """, (media_id,)) result = await cur.fetchone() assert result is not None assert result[0] == "US" finally: app.core.database.pool = original_pool @pytest.mark.asyncio async def test_upsert_media_item_update(test_db_pool): """Test updating an existing media item""" # Temporarily replace the global pool import app.core.database original_pool = app.core.database.pool app.core.database.pool = test_db_pool try: # Insert first media_id = await upsert_media_item( source_kind="radarr", source_item_id=1, title="Test Movie", year=2020, media_type="movie", arr_raw={"id": 1, "title": "Test Movie"} ) # Update with new title updated_id = await upsert_media_item( source_kind="radarr", source_item_id=1, title="Updated Movie", year=2021, media_type="movie", arr_raw={"id": 1, "title": "Updated Movie"} ) assert updated_id == media_id # Same ID # Verify it was updated async with test_db_pool.connection() as conn: async with conn.cursor() as cur: await cur.execute(""" SELECT title, year FROM moviemap.media_item WHERE id = %s """, (media_id,)) result = await cur.fetchone() assert result[0] == "Updated Movie" assert result[1] == 2021 finally: app.core.database.pool = original_pool