Files
cleveragents-core/features/steps/settings_steps.py
T

269 lines
8.2 KiB
Python

"""Step definitions for settings feature tests."""
import os
from pathlib import Path
from behave import given, then, when
from cleveragents.config.settings import Settings
@given('the environment variable "{key}" is set to "{value}"')
def step_set_env_var(context, key, value):
"""Set an environment variable."""
os.environ[key] = value
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append(key)
@given("no environment variables are set")
def step_clear_env_vars(context):
"""Clear all CleverAgents-related environment variables."""
keys_to_remove = [
"CLEVERAGENTS_LOG_LEVEL",
"CLEVERAGENTS_LOG_DIR",
"CLEVERAGENTS_DATA_DIR",
"CLEVERAGENTS_ENV",
"DATABASE_URL",
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"GEMINI_API_KEY",
"DEEPSEEK_API_KEY",
"COHERE_API_KEY",
"PERPLEXITY_API_KEY",
"GROQ_API_KEY",
"TOGETHER_API_KEY",
]
for key in keys_to_remove:
os.environ.pop(key, None)
@when("I load the settings")
def step_load_settings(context):
"""Load settings from environment."""
# Clear the singleton to force reload
Settings._instance = None
context.settings = Settings()
@when("I load the settings with defaults")
def step_load_settings_defaults(context):
"""Load settings with defaults."""
# Clear the singleton to force reload
Settings._instance = None
context.settings = Settings()
@then('the log level should be "{expected}"')
def step_check_log_level(context, expected):
"""Check the log level."""
assert context.settings.log_level == expected
@then('the log directory should be "{expected}"')
def step_check_log_dir(context, expected):
"""Check the log directory."""
assert str(context.settings.log_dir) == expected
@then('the log directory should contain "{substring}"')
def step_check_log_dir_contains(context, substring):
"""Check the log directory contains a substring."""
assert substring in str(context.settings.log_dir)
@then('the data directory should be "{expected}"')
def step_check_data_dir(context, expected):
"""Check the data directory."""
assert str(context.settings.data_dir) == expected
@then('the data directory should contain "{substring}"')
def step_check_data_dir_contains(context, substring):
"""Check the data directory contains a substring."""
assert substring in str(context.settings.data_dir)
@then('the environment should be "{expected}"')
def step_check_environment(context, expected):
"""Check the environment."""
assert context.settings.env == expected
@then('the database URL should be "{expected}"')
def step_check_database_url(context, expected):
"""Check the database URL."""
assert context.settings.database_url == expected
@then("the database URL should be a SQLite database")
def step_check_sqlite_database(context):
"""Check that the database URL is SQLite."""
assert context.settings.database_url.startswith("sqlite://")
@then("Anthropic should be configured")
def step_check_anthropic_configured(context):
"""Check that Anthropic is configured."""
assert context.settings.anthropic_api_key is not None
@then("no providers should be configured")
def step_check_no_providers(context):
"""Check that no providers are configured."""
assert not context.settings.has_provider_configured()
@given('the data directory is "{directory}"')
def step_set_data_directory(context, directory):
"""Set the data directory for testing."""
context.test_data_dir = directory
@when('I get the storage base path for "{storage_type}"')
def step_get_storage_path(context, storage_type):
"""Get the storage base path."""
settings = Settings(data_dir=Path(context.test_data_dir))
context.storage_path = settings.get_storage_base_path(storage_type)
@then('the storage path should be "{expected}"')
def step_check_storage_path(context, expected):
"""Check the storage path."""
assert str(context.storage_path) == expected
@given('the environment is set to "{env}"')
def step_set_environment(context, env):
"""Set the environment."""
os.environ["CLEVERAGENTS_ENV"] = env
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append("CLEVERAGENTS_ENV")
@when("I check if running in production")
def step_check_is_production(context):
"""Check if running in production."""
Settings._instance = None
settings = Settings()
context.is_production = settings.is_production()
@then("is_production should be {expected}")
def step_verify_is_production(context, expected):
"""Verify the is_production value."""
expected_bool = expected == "True"
assert context.is_production == expected_bool
@given('the {provider} API key is set to "{key}"')
def step_set_provider_key(context, provider, key):
"""Set a provider API key."""
env_key = f"{provider.upper().replace(' ', '_')}_API_KEY"
os.environ[env_key] = key
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append(env_key)
@when('I check if "{provider}" provider is configured')
def step_check_provider_configured(context, provider):
"""Check if a provider is configured."""
Settings._instance = None
settings = Settings()
context.provider_configured = settings.has_provider_configured(
provider.lower().replace(" ", "")
)
@then('has_provider_configured should be {expected} for "{provider}"')
def step_verify_provider_configured(context, expected, provider):
"""Verify provider configuration status."""
expected_bool = expected == "True"
assert context.provider_configured == expected_bool
@then("has_provider_configured should be {expected}")
def step_verify_any_provider_configured(context, expected):
"""Verify any provider configuration status."""
expected_bool = expected == "True"
assert context.provider_configured == expected_bool
@given("no API keys are set")
def step_clear_api_keys(context):
"""Clear all API keys."""
api_keys = [
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"GEMINI_API_KEY",
"DEEPSEEK_API_KEY",
"COHERE_API_KEY",
"PERPLEXITY_API_KEY",
"GROQ_API_KEY",
"TOGETHER_API_KEY",
]
for key in api_keys:
os.environ.pop(key, None)
@when("I check if any provider is configured")
def step_check_any_provider(context):
"""Check if any provider is configured."""
Settings._instance = None
settings = Settings()
context.provider_configured = settings.has_provider_configured()
@given('the database URL is set to "{url}"')
def step_set_database_url(context, url):
"""Set the database URL."""
os.environ["DATABASE_URL"] = url
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append("DATABASE_URL")
@given("no database URL is set")
def step_clear_database_url(context):
"""Clear the database URL."""
os.environ.pop("DATABASE_URL", None)
@when("I get the database URL")
def step_get_database_url(context):
"""Get the database URL."""
Settings._instance = None
if hasattr(context, "test_data_dir"):
settings = Settings(data_dir=Path(context.test_data_dir))
else:
settings = Settings()
context.database_url = settings.get_database_url()
@when("I get the settings instance")
def step_get_settings(context):
"""Get the settings instance."""
context.settings1 = Settings.get_settings()
@when("I get the settings instance again")
def step_get_settings_again(context):
"""Get the settings instance again."""
context.settings2 = Settings.get_settings()
@then("both instances should be the same object")
def step_verify_singleton(context):
"""Verify both instances are the same."""
assert context.settings1 is context.settings2
@then('the {provider} API key should be "{expected}"')
def step_check_provider_api_key(context, provider, expected):
"""Check a provider API key value."""
provider_key = provider.lower().replace(" ", "_") + "_api_key"
assert getattr(context.settings, provider_key) == expected