Skip to content

Commit

Permalink
test: ✅ rework tests to not start otel by default
Browse files Browse the repository at this point in the history
  • Loading branch information
miragecentury committed Aug 23, 2024
1 parent 20fb2fc commit 9dbb512
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ packages = [
]

[tool.poetry.dependencies]
python = "^3.12"
python = "~3.12"
structlog = "^24.1.0"
typer = "^0.12.3"
injector = "^0.22.0"
Expand Down Expand Up @@ -45,7 +45,7 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
addopts = "tests/units -n auto --color=yes"
addopts = "tests/units -n auto --color=yes --import-mode=importlib"
filterwarnings = [
"ignore:.*Type google._upb._message.MessageMapContainer:DeprecationWarning",
"ignore:.*Type google._upb._message.ScalarMapContainer:DeprecationWarning",
Expand All @@ -66,7 +66,6 @@ packages = "python_factory"
mypy_path = "src:tests"
namespace_packages = true
plugins = ["pydantic.mypy"]

follow_imports = "silent"
warn_redundant_casts = true
warn_unused_ignores = true
Expand Down
3 changes: 3 additions & 0 deletions src/python_factory/example/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ application:
plugins:
activate:
- opentelemetry_plugin

opentelemetry:
activate: true
25 changes: 25 additions & 0 deletions tests/units/python_factory/example/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""The test class TestExampleClassParent is a test class for the class App."""

import injector

from python_factory.core.plugins.opentelemetry_plugin.configs import OpenTelemetryConfig
from python_factory.example import App, AppModule, application_factory


class TestExampleClassParent:
"""The test class for the class App."""

def binder_configure(self, binder: injector.Binder) -> None:
"""Configure the binder for the test."""
binder.bind(
interface=OpenTelemetryConfig, to=OpenTelemetryConfig(activate=False)
)

def application_factory_for_test(self) -> App:
"""Create an application for testing."""
injector_instance = injector.Injector()
injector_instance.binder.install(module=AppModule)
self.binder_configure(injector_instance.binder)
application: App = application_factory(injector_instance=injector_instance)

return application
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

from fastapi.testclient import TestClient
from httpx import Response
from units.python_factory.example import TestExampleClassParent

from python_factory.example import application_factory
from python_factory.example.app.app import App


class TestBooksRoutes:
class TestBooksRoutes(TestExampleClassParent):
"""Tests for the routes of the books API."""

def test_get_books(self) -> None:
"""Test get_books."""
application: App = application_factory()
application: App = self.application_factory_for_test()

with TestClient(application) as client:
response: Response = client.get("/api/v1/books")
Expand Down
7 changes: 3 additions & 4 deletions tests/units/python_factory/example/api/v1/sys/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

from fastapi.testclient import TestClient
from httpx import Response
from units.python_factory.example import TestExampleClassParent

from python_factory.example import application_factory


class TestApiV1SysHealth:
class TestApiV1SysHealth(TestExampleClassParent):
"""Test the health endpoint."""

def test_get_api_v1_sys_health(self) -> None:
"""Test the get_api_v1_sys_health function."""
with TestClient(app=application_factory()) as client:
with TestClient(app=self.application_factory_for_test()) as client:
response: Response = client.get(url="/api/v1/sys/health")

assert response.status_code == HTTPStatus.OK.value
Expand Down

0 comments on commit 9dbb512

Please sign in to comment.