Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PTFE-693: unit test for settings class #309

Merged
merged 22 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
test: [unit, api]

env:
REDIS_OM_URL: redis://localhost:6379
REDIS_OM_URL: redis://localhost:6379/0
GITHUB_BASE_URL: http://localhost:4010

steps:
Expand Down
2 changes: 1 addition & 1 deletion runner_manager/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
A simple settings source that loads variables from a yaml file

"""
config_file: Optional[Path] = settings.__config__.config.config_file

Check failure on line 13 in runner_manager/models/settings.py

View workflow job for this annotation

GitHub Actions / Trunk Check

pyright(reportGeneralTypeIssues)

[new] Cannot access member "config" for type "type[Config]"   Member "config" is unknown
if config_file:
return yaml.full_load(config_file.read_text())
return {}
Expand All @@ -21,7 +21,7 @@


class Settings(BaseSettings):
name: str = "runner-manager"
name: Optional[str] = "runner-manager"
redis_om_url: Optional[RedisDsn] = None
github_base_url: Optional[AnyHttpUrl] = None

Expand Down
55 changes: 55 additions & 0 deletions tests/unit/models/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import tempfile
import pytest
import yaml
from pytest import fixture

from runner_manager.models.settings import Settings


def test_settings_default_values():
settings = Settings()
assert settings.name == "runner-manager"
assert settings.redis_om_url == os.getenv("REDIS_OM_URL")
tcarmet marked this conversation as resolved.
Show resolved Hide resolved
tcarmet marked this conversation as resolved.
Show resolved Hide resolved
assert settings.github_base_url == os.getenv("GITHUB_BASE_URL")


@fixture
Abubakarr99 marked this conversation as resolved.
Show resolved Hide resolved
def temp_yaml_file():
yaml_data = """
name: test-runner-manager
redis_om_url: redis://localhost:6379/0
github_base_url: https://github.com
"""
yaml_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
yaml_file.write(yaml_data)
return yaml_file.name

def test_invalid_redis_url():
with pytest.raises(ValueError):
Settings(redis_om_url="invalid_redis_url")

Check failure on line 30 in tests/unit/models/test_settings.py

View workflow job for this annotation

GitHub Actions / Trunk Check

pyright(reportGeneralTypeIssues)

[new] Argument of type "Literal['invalid_redis_url']" cannot be assigned to parameter "redis_om_url" of type "RedisDsn | None" in function "__init__"   Type "Literal['invalid_redis_url']" cannot be assigned to type "RedisDsn | None"     "Literal['invalid_redis_url']" is incompatible with "RedisDsn"     Type cannot be assigned to type "None"
tcarmet marked this conversation as resolved.
Show resolved Hide resolved

def test_invalid_github_url():
with pytest.raises(ValueError):
Settings(github_base_url="invalid_github_url")

Check failure on line 34 in tests/unit/models/test_settings.py

View workflow job for this annotation

GitHub Actions / Trunk Check

pyright(reportGeneralTypeIssues)

[new] Argument of type "Literal['invalid_github_url']" cannot be assigned to parameter "github_base_url" of type "AnyHttpUrl | None" in function "__init__"   Type "Literal['invalid_github_url']" cannot be assigned to type "AnyHttpUrl | None"     "Literal['invalid_github_url']" is incompatible with "AnyHttpUrl"     Type cannot be assigned to type "None"

def test_yaml_config(temp_yaml_file):
os.environ["CONFIG_FILE"] = temp_yaml_file
tcarmet marked this conversation as resolved.
Show resolved Hide resolved
# settings should automatically look for the config file
# environment variable and load the file if it exists
settings = Settings()
with open(temp_yaml_file) as f:
yaml_data = yaml.safe_load(f)
assert settings.name == yaml_data["name"]
assert settings.redis_om_url == yaml_data["redis_om_url"]
assert settings.github_base_url == yaml_data["github_base_url"]


def test_env_file():
os.environ["REDIS_OM_URL"] = "redis://localhost:6379/0"
os.environ["GITHUB_BASE_URL"] = "https://github.com"
os.environ["NAME"] = "test-runner-manager"
settings = Settings()
assert settings.name == os.getenv("NAME")
assert settings.redis_om_url == os.getenv("REDIS_OM_URL")
assert settings.github_base_url == os.getenv("GITHUB_BASE_URL")
Loading