/
liquid-g
/
liquid-code
Обзор
Документация
Войти
/
liquid-g
/
liquid-code
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop-0.4
tests/unit/container/test_container.py
175 строк
5 KB
User
0.4.2 - покрытие тестами 52%, цель 80-90.
05 июл 2026, 00:06
05 июл 2026, 00:06
f107982
Код
Авторство
О чём код?
"""Unit-тесты для контейнера.""" import pytest from liquidcode.container import Container class TestContainer: """Тесты контейнера.""" def test_register_singleton(self): """Регистрация singleton.""" container = Container() class Service: pass container.set(Service, Service()) assert container.get(Service) is not None def test_register_closure(self): """Регистрация closure.""" container = Container() def create_service(): return "service" container.factory("service", create_service, shared=False) assert container.get("service") == "service" def test_get_nonexistent(self): """Получение несуществующего сервиса.""" container = Container() with pytest.raises(Exception): container.get("nonexistent") def test_has(self): """Проверка наличия сервиса.""" container = Container() class Service: pass container.set(Service, Service()) assert container.has(Service) def test_has_not(self): """Проверка отсутствия сервиса.""" container = Container() assert not container.has("nonexistent") def test_remove(self): """Удаление сервиса.""" container = Container() class Service: pass container.set(Service, Service()) container.remove(Service) assert not container.has(Service) def test_singleton_same_instance(self): """Singleton возвращает тот же экземпляр.""" container = Container() class Service: def __init__(self): self.id = id(self) service = Service() container.set(Service, service) assert container.get(Service).id == service.id def test_closures_same_result(self): """Closure возвращает новый результат.""" container = Container() counter = [0] def create_counter(): counter[0] += 1 return counter[0] container.factory("counter", create_counter, shared=False) assert container.get("counter") == 1 assert container.get("counter") == 2 class TestContainerAutowiring: """Тесты автопроводки (autowiring) в контейнере.""" def test_autowiring_with_dependencies(self): """Автопроводка с зависимостями.""" container = Container() class Repository: pass class Service: def __init__(self, repo): self.repo = repo container.register(Service, Service) service = container.get(Service) assert hasattr(service, 'repo') def test_autowiring_with_primitive_default(self): """Автопроводка с примитивами по умолчанию.""" container = Container() class Service: def __init__(self, name="default"): self.name = name container.register(Service, Service) service = container.get(Service) assert service.name == "default" def test_container_chainable(self): """Методы контейнера возвращают self для цепочки.""" container = Container() class Service1: pass class Service2: pass result = container.register(Service1, Service1).register(Service2, Service2) assert result is container class TestContainerAliases: """Тесты алиасов в контейнере.""" def test_alias_registration(self): """Регистрация алиаса.""" container = Container() class Interface: pass class Implementation(Interface): pass container.register(Interface, Implementation) result = container.get(Interface) assert isinstance(result, Implementation) def test_remove_service(self): """Удаление зарегистрированного сервиса.""" container = Container() class Service: pass container.set(Service, Service()) assert container.has(Service) container.remove(Service) assert not container.has(Service) def test_remove_factory(self): """Удаление фабрики.""" container = Container() def create_service(): return "service" container.factory("my_service", create_service, shared=False) assert container.has("my_service") container.remove("my_service") assert not container.has("my_service")