133 lines
4.9 KiB
Python
133 lines
4.9 KiB
Python
"""Tests for Voice API endpoints"""
|
|
import pytest
|
|
|
|
|
|
class TestVoiceAPI:
|
|
"""Test cases for Voice endpoints"""
|
|
|
|
def test_get_voices_empty(self, client):
|
|
"""Test getting voices when database is empty"""
|
|
response = client.get("/api/voices")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "total" in data
|
|
assert "list" in data
|
|
|
|
def test_create_voice(self, client, sample_voice_data):
|
|
"""Test creating a new voice"""
|
|
response = client.post("/api/voices", json=sample_voice_data)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["name"] == sample_voice_data["name"]
|
|
assert data["vendor"] == sample_voice_data["vendor"]
|
|
assert data["gender"] == sample_voice_data["gender"]
|
|
assert data["language"] == sample_voice_data["language"]
|
|
assert "id" in data
|
|
|
|
def test_create_voice_minimal(self, client):
|
|
"""Test creating a voice with minimal data"""
|
|
data = {
|
|
"name": "Minimal Voice",
|
|
"vendor": "Test",
|
|
"gender": "Male",
|
|
"language": "en",
|
|
"description": ""
|
|
}
|
|
response = client.post("/api/voices", json=data)
|
|
assert response.status_code == 200
|
|
|
|
def test_get_voice_by_id(self, client, sample_voice_data):
|
|
"""Test getting a specific voice by ID"""
|
|
# Create first
|
|
create_response = client.post("/api/voices", json=sample_voice_data)
|
|
voice_id = create_response.json()["id"]
|
|
|
|
# Get by ID
|
|
response = client.get(f"/api/voices/{voice_id}")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["id"] == voice_id
|
|
assert data["name"] == sample_voice_data["name"]
|
|
|
|
def test_get_voice_not_found(self, client):
|
|
"""Test getting a non-existent voice"""
|
|
response = client.get("/api/voices/non-existent-id")
|
|
assert response.status_code == 404
|
|
|
|
def test_update_voice(self, client, sample_voice_data):
|
|
"""Test updating a voice"""
|
|
# Create first
|
|
create_response = client.post("/api/voices", json=sample_voice_data)
|
|
voice_id = create_response.json()["id"]
|
|
|
|
# Update
|
|
update_data = {"name": "Updated Voice", "speed": 1.5}
|
|
response = client.put(f"/api/voices/{voice_id}", json=update_data)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["name"] == "Updated Voice"
|
|
assert data["speed"] == 1.5
|
|
|
|
def test_delete_voice(self, client, sample_voice_data):
|
|
"""Test deleting a voice"""
|
|
# Create first
|
|
create_response = client.post("/api/voices", json=sample_voice_data)
|
|
voice_id = create_response.json()["id"]
|
|
|
|
# Delete
|
|
response = client.delete(f"/api/voices/{voice_id}")
|
|
assert response.status_code == 200
|
|
|
|
# Verify deleted
|
|
get_response = client.get(f"/api/voices/{voice_id}")
|
|
assert get_response.status_code == 404
|
|
|
|
def test_list_voices_with_pagination(self, client, sample_voice_data):
|
|
"""Test listing voices with pagination"""
|
|
# Create multiple voices
|
|
for i in range(3):
|
|
data = sample_voice_data.copy()
|
|
data["name"] = f"Voice {i}"
|
|
client.post("/api/voices", json=data)
|
|
|
|
# Test pagination
|
|
response = client.get("/api/voices?page=1&limit=2")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["total"] == 3
|
|
assert len(data["list"]) == 2
|
|
|
|
def test_filter_voices_by_vendor(self, client, sample_voice_data):
|
|
"""Test filtering voices by vendor"""
|
|
# Create voice with specific vendor
|
|
sample_voice_data["vendor"] = "FilterTestVendor"
|
|
client.post("/api/voices", json=sample_voice_data)
|
|
|
|
response = client.get("/api/voices?vendor=FilterTestVendor")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
for voice in data["list"]:
|
|
assert voice["vendor"] == "FilterTestVendor"
|
|
|
|
def test_filter_voices_by_language(self, client, sample_voice_data):
|
|
"""Test filtering voices by language"""
|
|
sample_voice_data["language"] = "en"
|
|
client.post("/api/voices", json=sample_voice_data)
|
|
|
|
response = client.get("/api/voices?language=en")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
for voice in data["list"]:
|
|
assert voice["language"] == "en"
|
|
|
|
def test_filter_voices_by_gender(self, client, sample_voice_data):
|
|
"""Test filtering voices by gender"""
|
|
sample_voice_data["gender"] = "Female"
|
|
client.post("/api/voices", json=sample_voice_data)
|
|
|
|
response = client.get("/api/voices?gender=Female")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
for voice in data["list"]:
|
|
assert voice["gender"] == "Female"
|