Full coverage Refactor code structure and improve logging configuration; update tests for activities and connectors
110 lines
3.0 KiB
Python
110 lines
3.0 KiB
Python
import os
|
|
from unittest.mock import patch
|
|
import pytest
|
|
from scouter.utils.connectors_config import (
|
|
build_postgres_config,
|
|
build_kafka_config,
|
|
build_redis_config
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_env_vars():
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
yield
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_env_vars")
|
|
def test_build_postgres_config_defaults():
|
|
"""Test that build_postgres_config returns default values when no env vars are set"""
|
|
config = build_postgres_config()
|
|
|
|
assert config == {
|
|
'host': 'localhost',
|
|
'port': 5432,
|
|
'user': 'sientia',
|
|
'password': 'sientia',
|
|
'dbname': 'sientia',
|
|
'min_connections': 5,
|
|
'max_connections': 20
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_env_vars")
|
|
def test_build_postgres_config_with_env_vars():
|
|
"""Test that build_postgres_config uses env vars when set"""
|
|
with patch.dict(os.environ, {
|
|
'POSTGRES_HOST': 'db.example.com',
|
|
'POSTGRES_PORT': '5433',
|
|
'POSTGRES_USER': 'admin',
|
|
'POSTGRES_PASSWORD': 'secret',
|
|
'POSTGRES_DBNAME': 'test_db',
|
|
'POSTGRES_MIN_CONNECTIONS': '3',
|
|
'POSTGRES_MAX_CONNECTIONS': '15'
|
|
}):
|
|
config = build_postgres_config()
|
|
|
|
assert config == {
|
|
'host': 'db.example.com',
|
|
'port': 5433,
|
|
'user': 'admin',
|
|
'password': 'secret',
|
|
'dbname': 'test_db',
|
|
'min_connections': 3,
|
|
'max_connections': 15
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_env_vars")
|
|
def test_build_kafka_config_defaults():
|
|
"""Test that build_kafka_config returns default values when no env vars are set"""
|
|
config = build_kafka_config()
|
|
|
|
assert config == {
|
|
'bootstrap_servers': 'localhost:9092',
|
|
'polling_time': 1000,
|
|
'group_id': 'scouter-group'
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_env_vars")
|
|
def test_build_kafka_config_with_env_vars():
|
|
"""Test that build_kafka_config uses env vars when set"""
|
|
with patch.dict(os.environ, {
|
|
'KAFKA_BOOTSTRAP_SERVERS': 'kafka.example.com:9092',
|
|
'KAFKA_POLLING_TIME': '5000'
|
|
}):
|
|
config = build_kafka_config()
|
|
|
|
assert config == {
|
|
'bootstrap_servers': 'kafka.example.com:9092',
|
|
'polling_time': 5000,
|
|
'group_id': 'scouter-group'
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_env_vars")
|
|
def test_build_redis_config_defaults():
|
|
"""Test that build_redis_config returns default values when no env vars are set"""
|
|
config = build_redis_config()
|
|
|
|
assert config == {
|
|
'host': 'localhost',
|
|
'port': 6379
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_env_vars")
|
|
def test_build_redis_config_with_env_vars():
|
|
"""Test that build_redis_config uses env vars when set"""
|
|
with patch.dict(os.environ, {
|
|
'REDIS_HOST': 'redis.example.com',
|
|
'REDIS_PORT': '6380'
|
|
}):
|
|
config = build_redis_config()
|
|
|
|
assert config == {
|
|
'host': 'redis.example.com',
|
|
'port': 6380
|
|
}
|