Add new API endpoints for media retrieval by country and enhance configuration
Some checks failed
Test Suite / test (push) Has been cancelled
Some checks failed
Test Suite / test (push) Has been cancelled
- Introduced `/api/tmdb` and `/api/collection/missing-locations` endpoints to the backend for improved media management. - Added a new `get_media_by_country` function in the collection API to fetch media items based on country codes. - Updated configuration to allow overriding *arr base URLs via environment variables for better flexibility. - Enhanced frontend with a new `MissingLocations` component and integrated it into the routing structure. - Improved the `CollectionMap` component to handle country selection and display media items accordingly. - Added testing dependencies in `requirements.txt` and updated frontend configuration for testing support.
This commit is contained in:
2
backend/tests/__init__.py
Normal file
2
backend/tests/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
"""Test suite for Movie Map backend"""
|
||||
|
||||
200
backend/tests/conftest.py
Normal file
200
backend/tests/conftest.py
Normal file
@@ -0,0 +1,200 @@
|
||||
"""Pytest configuration and fixtures"""
|
||||
import pytest
|
||||
import asyncio
|
||||
import os
|
||||
from typing import AsyncGenerator
|
||||
from fastapi.testclient import TestClient
|
||||
from httpx import AsyncClient
|
||||
from app.core.database import init_db, close_db, pool as db_pool
|
||||
from app.core.config import settings
|
||||
from app.main import app
|
||||
import psycopg
|
||||
from psycopg_pool import AsyncConnectionPool
|
||||
|
||||
|
||||
# Use test database
|
||||
TEST_DB = os.getenv("TEST_POSTGRES_DB", "moviemap_test")
|
||||
TEST_USER = os.getenv("TEST_POSTGRES_USER", os.getenv("USER", "jawz"))
|
||||
TEST_SOCKET = os.getenv("TEST_POSTGRES_SOCKET_PATH", "/run/postgresql")
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def event_loop():
|
||||
"""Create an instance of the default event loop for the test session."""
|
||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
||||
yield loop
|
||||
loop.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def test_db_pool() -> AsyncGenerator[AsyncConnectionPool, None]:
|
||||
"""Create a test database connection pool"""
|
||||
test_db_url = f"postgresql://{TEST_USER}@/{TEST_DB}?host={TEST_SOCKET}"
|
||||
|
||||
pool = AsyncConnectionPool(
|
||||
conninfo=test_db_url,
|
||||
min_size=1,
|
||||
max_size=5,
|
||||
open=False,
|
||||
)
|
||||
await pool.open()
|
||||
|
||||
# Run migrations
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Create schema if not exists
|
||||
await cur.execute("CREATE SCHEMA IF NOT EXISTS moviemap")
|
||||
|
||||
# Create enums
|
||||
await cur.execute("""
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE moviemap.source_kind AS ENUM ('radarr', 'sonarr', 'lidarr');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
""")
|
||||
|
||||
await cur.execute("""
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE moviemap.media_type AS ENUM ('movie', 'show', 'music');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
""")
|
||||
|
||||
await cur.execute("""
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE moviemap.watched_media_type AS ENUM ('movie', 'show');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
""")
|
||||
|
||||
# Create tables
|
||||
await cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS moviemap.source (
|
||||
id SERIAL PRIMARY KEY,
|
||||
kind moviemap.source_kind NOT NULL UNIQUE,
|
||||
base_url TEXT NOT NULL,
|
||||
enabled BOOLEAN NOT NULL DEFAULT true,
|
||||
last_sync_at TIMESTAMPTZ
|
||||
)
|
||||
""")
|
||||
|
||||
await cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS moviemap.media_item (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
source_kind moviemap.source_kind NOT NULL,
|
||||
source_item_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
year INTEGER,
|
||||
media_type moviemap.media_type NOT NULL,
|
||||
arr_raw JSONB,
|
||||
UNIQUE (source_kind, source_item_id)
|
||||
)
|
||||
""")
|
||||
|
||||
await cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS moviemap.media_country (
|
||||
media_item_id UUID NOT NULL REFERENCES moviemap.media_item(id) ON DELETE CASCADE,
|
||||
country_code CHAR(2) NOT NULL,
|
||||
weight SMALLINT NOT NULL DEFAULT 1,
|
||||
PRIMARY KEY (media_item_id, country_code)
|
||||
)
|
||||
""")
|
||||
|
||||
await cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS moviemap.watched_item (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
media_type moviemap.watched_media_type NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
year INTEGER,
|
||||
country_code CHAR(2) NOT NULL,
|
||||
watched_at TIMESTAMPTZ,
|
||||
notes TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
""")
|
||||
|
||||
await cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS moviemap.manual_pin (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
country_code CHAR(2) NOT NULL,
|
||||
label TEXT,
|
||||
pinned_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
""")
|
||||
|
||||
await conn.commit()
|
||||
|
||||
yield pool
|
||||
|
||||
# Cleanup: drop all data but keep schema
|
||||
async with pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute("TRUNCATE TABLE moviemap.media_country CASCADE")
|
||||
await cur.execute("TRUNCATE TABLE moviemap.media_item CASCADE")
|
||||
await cur.execute("TRUNCATE TABLE moviemap.watched_item CASCADE")
|
||||
await cur.execute("TRUNCATE TABLE moviemap.manual_pin CASCADE")
|
||||
await cur.execute("TRUNCATE TABLE moviemap.source CASCADE")
|
||||
await conn.commit()
|
||||
|
||||
await pool.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def test_client(test_db_pool) -> AsyncGenerator[AsyncClient, None]:
|
||||
"""Create a test client with test database"""
|
||||
# Temporarily replace the global pool
|
||||
import app.core.database
|
||||
original_pool = app.core.database.pool
|
||||
app.core.database.pool = test_db_pool
|
||||
|
||||
async with AsyncClient(app=app, base_url="http://test") as client:
|
||||
yield client
|
||||
|
||||
# Restore original pool
|
||||
app.core.database.pool = original_pool
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_radarr_response():
|
||||
"""Mock Radarr API response"""
|
||||
return [
|
||||
{
|
||||
"id": 1,
|
||||
"title": "Test Movie",
|
||||
"year": 2020,
|
||||
"tmdbId": 12345,
|
||||
"productionCountries": [{"iso_3166_1": "US"}]
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_sonarr_response():
|
||||
"""Mock Sonarr API response"""
|
||||
return [
|
||||
{
|
||||
"id": 1,
|
||||
"title": "Test Show",
|
||||
"year": 2020,
|
||||
"tmdbId": 67890,
|
||||
"seriesMetadata": {"originCountry": ["US"]}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_lidarr_response():
|
||||
"""Mock Lidarr API response"""
|
||||
return [
|
||||
{
|
||||
"id": 1,
|
||||
"artistName": "Test Artist",
|
||||
"country": "US",
|
||||
"foreignArtistId": "test-mbid-123"
|
||||
}
|
||||
]
|
||||
|
||||
261
backend/tests/test_api.py
Normal file
261
backend/tests/test_api.py
Normal file
@@ -0,0 +1,261 @@
|
||||
"""Tests for API endpoints"""
|
||||
import pytest
|
||||
from uuid import uuid4
|
||||
import json
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_health_endpoint(test_client):
|
||||
"""Test health check endpoint"""
|
||||
response = await test_client.get("/api/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ok"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collection_summary_empty(test_client, test_db_pool):
|
||||
"""Test collection summary with no data"""
|
||||
response = await test_client.get("/api/collection/summary")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collection_summary_with_data(test_client, test_db_pool):
|
||||
"""Test collection summary with media items"""
|
||||
# Insert test data
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Insert media item
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('radarr', 1, 'Test Movie', 2020, 'movie', '{}'::jsonb)
|
||||
RETURNING id
|
||||
""")
|
||||
media_id = (await cur.fetchone())[0]
|
||||
|
||||
# Insert country association
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_country (media_item_id, country_code)
|
||||
VALUES (%s, 'US')
|
||||
""", (media_id,))
|
||||
|
||||
await conn.commit()
|
||||
|
||||
response = await test_client.get("/api/collection/summary")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "US" in data
|
||||
assert data["US"]["movie"] == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collection_summary_filtered(test_client, test_db_pool):
|
||||
"""Test collection summary with type filters"""
|
||||
# Insert test data
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Insert movie
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('radarr', 1, 'Test Movie', 2020, 'movie', '{}'::jsonb)
|
||||
RETURNING id
|
||||
""")
|
||||
movie_id = (await cur.fetchone())[0]
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_country (media_item_id, country_code)
|
||||
VALUES (%s, 'US')
|
||||
""", (movie_id,))
|
||||
|
||||
# Insert show
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('sonarr', 1, 'Test Show', 2020, 'show', '{}'::jsonb)
|
||||
RETURNING id
|
||||
""")
|
||||
show_id = (await cur.fetchone())[0]
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_country (media_item_id, country_code)
|
||||
VALUES (%s, 'US')
|
||||
""", (show_id,))
|
||||
|
||||
await conn.commit()
|
||||
|
||||
# Test with movie filter
|
||||
response = await test_client.get("/api/collection/summary?types=movie")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "US" in data
|
||||
assert data["US"]["movie"] == 1
|
||||
assert "show" not in data["US"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watched_list_empty(test_client, test_db_pool):
|
||||
"""Test watched items list with no data"""
|
||||
response = await test_client.get("/api/watched")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watched_create(test_client, test_db_pool):
|
||||
"""Test creating a watched item"""
|
||||
response = await test_client.post(
|
||||
"/api/watched",
|
||||
json={
|
||||
"media_type": "movie",
|
||||
"title": "Test Movie",
|
||||
"year": 2020,
|
||||
"country_code": "US",
|
||||
"notes": "Test notes"
|
||||
}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
assert data["status"] == "created"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watched_list_with_data(test_client, test_db_pool):
|
||||
"""Test watched items list with data"""
|
||||
# Create watched item
|
||||
create_response = await test_client.post(
|
||||
"/api/watched",
|
||||
json={
|
||||
"media_type": "movie",
|
||||
"title": "Test Movie",
|
||||
"country_code": "US"
|
||||
}
|
||||
)
|
||||
assert create_response.status_code == 200
|
||||
|
||||
# List watched items
|
||||
response = await test_client.get("/api/watched")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert len(data) == 1
|
||||
assert data[0]["title"] == "Test Movie"
|
||||
assert data[0]["country_code"] == "US"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watched_delete(test_client, test_db_pool):
|
||||
"""Test deleting a watched item"""
|
||||
# Create watched item
|
||||
create_response = await test_client.post(
|
||||
"/api/watched",
|
||||
json={
|
||||
"media_type": "movie",
|
||||
"title": "Test Movie",
|
||||
"country_code": "US"
|
||||
}
|
||||
)
|
||||
item_id = create_response.json()["id"]
|
||||
|
||||
# Delete it
|
||||
response = await test_client.delete(f"/api/watched/{item_id}")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "deleted"
|
||||
|
||||
# Verify it's gone
|
||||
list_response = await test_client.get("/api/watched")
|
||||
assert len(list_response.json()) == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watched_summary(test_client, test_db_pool):
|
||||
"""Test watched items summary"""
|
||||
# Create watched items
|
||||
await test_client.post(
|
||||
"/api/watched",
|
||||
json={
|
||||
"media_type": "movie",
|
||||
"title": "Test Movie",
|
||||
"country_code": "US",
|
||||
"watched_at": "2024-01-01T00:00:00Z"
|
||||
}
|
||||
)
|
||||
await test_client.post(
|
||||
"/api/watched",
|
||||
json={
|
||||
"media_type": "show",
|
||||
"title": "Test Show",
|
||||
"country_code": "US",
|
||||
"watched_at": "2024-01-01T00:00:00Z"
|
||||
}
|
||||
)
|
||||
|
||||
response = await test_client.get("/api/watched/summary")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "US" in data
|
||||
assert data["US"]["movie"] == 1
|
||||
assert data["US"]["show"] == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pins_list_empty(test_client, test_db_pool):
|
||||
"""Test pins list with no data"""
|
||||
response = await test_client.get("/api/pins")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pins_create(test_client, test_db_pool):
|
||||
"""Test creating a pin"""
|
||||
response = await test_client.post(
|
||||
"/api/pins",
|
||||
json={
|
||||
"country_code": "US",
|
||||
"label": "Test Pin"
|
||||
}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
assert data["status"] == "created"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pins_delete(test_client, test_db_pool):
|
||||
"""Test deleting a pin"""
|
||||
# Create pin
|
||||
create_response = await test_client.post(
|
||||
"/api/pins",
|
||||
json={"country_code": "US"}
|
||||
)
|
||||
pin_id = create_response.json()["id"]
|
||||
|
||||
# Delete it
|
||||
response = await test_client.delete(f"/api/pins/{pin_id}")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "deleted"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_sync_no_auth(test_client, test_db_pool):
|
||||
"""Test admin sync without auth (should work if no token configured)"""
|
||||
# This will fail if admin token is required, but should work if not
|
||||
# We'll mock the sync function to avoid actual API calls
|
||||
response = await test_client.post("/admin/sync")
|
||||
# Either 200 (no auth required) or 401 (auth required)
|
||||
assert response.status_code in [200, 401, 500] # 500 if sync fails due to missing *arr
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_countries_empty(test_client, test_db_pool):
|
||||
"""Test missing countries endpoint with no data"""
|
||||
response = await test_client.get("/admin/missing-countries")
|
||||
# May require auth, but if not, should return empty
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
assert data["total"] == 0
|
||||
assert data["returned"] == 0
|
||||
assert data["items"] == []
|
||||
|
||||
153
backend/tests/test_database.py
Normal file
153
backend/tests/test_database.py
Normal file
@@ -0,0 +1,153 @@
|
||||
"""Tests for database operations"""
|
||||
import pytest
|
||||
import json
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_media_item_insert(test_db_pool):
|
||||
"""Test inserting a media item"""
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('radarr', 1, 'Test Movie', 2020, 'movie', %s::jsonb)
|
||||
RETURNING id, title
|
||||
""", (json.dumps({"test": "data"}),))
|
||||
|
||||
result = await cur.fetchone()
|
||||
assert result is not None
|
||||
assert result[1] == "Test Movie"
|
||||
|
||||
await conn.commit()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_media_country_association(test_db_pool):
|
||||
"""Test associating a country with a media item"""
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Insert media item
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('radarr', 1, 'Test Movie', 2020, 'movie', '{}'::jsonb)
|
||||
RETURNING id
|
||||
""")
|
||||
media_id = (await cur.fetchone())[0]
|
||||
|
||||
# Associate country
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_country (media_item_id, country_code)
|
||||
VALUES (%s, 'US')
|
||||
""", (media_id,))
|
||||
|
||||
# Verify association
|
||||
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"
|
||||
|
||||
await conn.commit()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_media_item_unique_constraint(test_db_pool):
|
||||
"""Test that source_kind + source_item_id is unique"""
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Insert first item
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('radarr', 1, 'Test Movie', 2020, 'movie', '{}'::jsonb)
|
||||
""")
|
||||
|
||||
# Try to insert duplicate
|
||||
with pytest.raises(Exception): # Should raise unique constraint violation
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('radarr', 1, 'Another Movie', 2021, 'movie', '{}'::jsonb)
|
||||
""")
|
||||
|
||||
await conn.rollback()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watched_item_insert(test_db_pool):
|
||||
"""Test inserting a watched item"""
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.watched_item
|
||||
(media_type, title, year, country_code, notes)
|
||||
VALUES ('movie', 'Test Movie', 2020, 'US', 'Test notes')
|
||||
RETURNING id, title
|
||||
""")
|
||||
|
||||
result = await cur.fetchone()
|
||||
assert result is not None
|
||||
assert result[1] == "Test Movie"
|
||||
|
||||
await conn.commit()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_manual_pin_insert(test_db_pool):
|
||||
"""Test inserting a manual pin"""
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.manual_pin (country_code, label)
|
||||
VALUES ('US', 'Test Pin')
|
||||
RETURNING id, country_code
|
||||
""")
|
||||
|
||||
result = await cur.fetchone()
|
||||
assert result is not None
|
||||
assert result[1] == "US"
|
||||
|
||||
await conn.commit()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cascade_delete(test_db_pool):
|
||||
"""Test that deleting a media item cascades to country associations"""
|
||||
async with test_db_pool.connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
# Insert media item with country
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_item
|
||||
(source_kind, source_item_id, title, year, media_type, arr_raw)
|
||||
VALUES ('radarr', 1, 'Test Movie', 2020, 'movie', '{}'::jsonb)
|
||||
RETURNING id
|
||||
""")
|
||||
media_id = (await cur.fetchone())[0]
|
||||
|
||||
await cur.execute("""
|
||||
INSERT INTO moviemap.media_country (media_item_id, country_code)
|
||||
VALUES (%s, 'US')
|
||||
""", (media_id,))
|
||||
|
||||
# Delete media item
|
||||
await cur.execute("""
|
||||
DELETE FROM moviemap.media_item WHERE id = %s
|
||||
""", (media_id,))
|
||||
|
||||
# Verify country association is also deleted
|
||||
await cur.execute("""
|
||||
SELECT COUNT(*) FROM moviemap.media_country
|
||||
WHERE media_item_id = %s
|
||||
""", (media_id,))
|
||||
|
||||
count = (await cur.fetchone())[0]
|
||||
assert count == 0
|
||||
|
||||
await conn.commit()
|
||||
|
||||
204
backend/tests/test_sync.py
Normal file
204
backend/tests/test_sync.py
Normal file
@@ -0,0 +1,204 @@
|
||||
"""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
|
||||
|
||||
Reference in New Issue
Block a user