/
Roman_arv
/
UpnpUtil
Обзор
Документация
Войти
/
Roman_arv
/
UpnpUtil
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/test_server.py
165 строк
5 KB
Roman_arv
gigacode generated v2
21 июл 2026, 19:25
21 июл 2026, 19:25
47951bb
Код
Авторство
О чём код?
""" Тесты для DLNA-сервера. """ import pytest import os import tempfile from unittest.mock import Mock, patch, MagicMock from dlna.server import ( ServerConfig, ConfigManager, MediaLibrary, ContentDirectoryService, DLNAServer, ) class TestServerConfig: """Тесты для ServerConfig.""" def test_default_values(self): """Тест значений по умолчанию.""" config = ServerConfig() assert config.host == "0.0.0.0" assert config.port == 8200 assert config.server_name == "My DLNA Server" assert config.media_directory == "./test_music" def test_custom_values(self): """Тест кастомных значений.""" config = ServerConfig( host="127.0.0.1", port=9000, server_name="Test Server" ) assert config.host == "127.0.0.1" assert config.port == 9000 assert config.server_name == "Test Server" class TestConfigManager: """Тесты для ConfigManager.""" def test_load_default_config(self): """Тест загрузки конфигурации по умолчанию.""" manager = ConfigManager() config = manager.load() assert config is not None assert config.port == 8200 def test_save_and_load(self): """Тест сохранения и загрузки конфигурации.""" manager = ConfigManager() with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: temp_path = f.name try: config = ServerConfig(port=8080, server_name="Test") manager._config = config manager.config_path = temp_path manager.save() # Перезагружаем new_manager = ConfigManager(temp_path) new_config = new_manager.load() assert new_config.port == 8080 assert new_config.server_name == "Test" finally: if os.path.exists(temp_path): os.remove(temp_path) class TestMediaLibrary: """Тесты для MediaLibrary.""" def test_init(self): """Тест инициализации.""" with tempfile.TemporaryDirectory() as tmpdir: library = MediaLibrary(tmpdir) assert library.base_path == tmpdir def test_scan_nonexistent_directory(self): """Тест сканирования несуществующей директории.""" with pytest.raises(FileNotFoundError): library = MediaLibrary("/nonexistent/path") library.scan() @patch('dlna.server.media_library.MediaLibrary._generate_id') def test_create_track(self, mock_generate_id): """Тест создания трека.""" with tempfile.TemporaryDirectory() as tmpdir: # Создаем тестовый файл test_file = os.path.join(tmpdir, "artist - song.mp3") with open(test_file, 'w') as f: f.write("test") library = MediaLibrary(tmpdir) from dlna.server.media_library import Folder folder = Folder(id="0", title="Test") track = library._create_track(test_file, folder) assert track.artist == "artist" assert track.title == "song" class TestContentDirectoryService: """Тесты для ContentDirectoryService.""" def test_browse_empty_library(self): """Тест browse с пустой библиотекой.""" from dlna.server.config import ServerConfig from dlna.server.media_library import MediaLibrary, Folder config = ServerConfig() with tempfile.TemporaryDirectory() as tmpdir: library = MediaLibrary(tmpdir) service = ContentDirectoryService(config, library) result = service.browse("0") assert result['NumberReturned'] == 0 assert result['TotalMatches'] == 0 def test_get_search_capabilities(self): """Тест получения возможностей поиска.""" from dlna.server.config import ServerConfig from dlna.server.media_library import MediaLibrary config = ServerConfig() library = MediaLibrary("/tmp") service = ContentDirectoryService(config, library) capabilities = service.get_search_capabilities() assert "dc:title" in capabilities assert "upnp:artist" in capabilities class TestDLNAServer: """Тесты для DLNAServer.""" def test_initialization(self): """Тест инициализации сервера.""" with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: f.write("port: 8200\n") temp_path = f.name try: server = DLNAServer(temp_path) config = server.config_manager.load() assert config.port == 8200 finally: if os.path.exists(temp_path): os.remove(temp_path)