|
| 1 | +import dataclasses |
| 2 | + |
| 3 | +from opentelemetry.sdk.trace.export import ConsoleSpanExporter |
| 4 | + |
| 5 | +from lite_bootstrap import FastAPIConfig |
| 6 | +from lite_bootstrap.instruments.base import BaseConfig |
| 7 | +from tests.conftest import CustomInstrumentor |
| 8 | + |
| 9 | + |
| 10 | +def test_config_from_dict() -> None: |
| 11 | + raw_config = { |
| 12 | + "service_name": "microservice", |
| 13 | + "service_version": "2.0.0", |
| 14 | + "service_environment": "test", |
| 15 | + "service_debug": False, |
| 16 | + "cors_allowed_origins": ["http://test"], |
| 17 | + "health_checks_path": "/custom-health/", |
| 18 | + "logging_buffer_capacity": 0, |
| 19 | + "opentelemetry_endpoint": "otl", |
| 20 | + "opentelemetry_instrumentors": [CustomInstrumentor()], |
| 21 | + "opentelemetry_span_exporter": ConsoleSpanExporter(), |
| 22 | + "prometheus_metrics_path": "/custom-metrics/", |
| 23 | + "sentry_dsn": "https://testdsn@localhost/1", |
| 24 | + "swagger_offline_docs": True, |
| 25 | + "extra_key": "extra_value", |
| 26 | + } |
| 27 | + config = FastAPIConfig.from_dict(raw_config) |
| 28 | + |
| 29 | + for field in dataclasses.fields(FastAPIConfig): |
| 30 | + if field.name in raw_config: |
| 31 | + assert getattr(config, field.name) == raw_config[field.name] |
| 32 | + |
| 33 | + |
| 34 | +def test_config_from_object() -> None: |
| 35 | + big_config = FastAPIConfig( |
| 36 | + service_name="microservice", |
| 37 | + service_version="2.0.0", |
| 38 | + service_environment="test", |
| 39 | + service_debug=False, |
| 40 | + cors_allowed_origins=["http://test"], |
| 41 | + health_checks_path="/custom-health/", |
| 42 | + logging_buffer_capacity=0, |
| 43 | + opentelemetry_endpoint="otl", |
| 44 | + opentelemetry_instrumentors=[CustomInstrumentor()], |
| 45 | + opentelemetry_span_exporter=ConsoleSpanExporter(), |
| 46 | + prometheus_metrics_path="/custom-metrics/", |
| 47 | + sentry_dsn="https://testdsn@localhost/1", |
| 48 | + swagger_offline_docs=True, |
| 49 | + ) |
| 50 | + |
| 51 | + short_config = BaseConfig.from_object(big_config) |
| 52 | + for field in dataclasses.fields(BaseConfig): |
| 53 | + assert getattr(short_config, field.name) == getattr(big_config, field.name) |
0 commit comments