Skip to content

Commit

Permalink
test: Add ConfigManager fixture to tests
Browse files Browse the repository at this point in the history
This commit adds a new fixture, `config_manager`, to the tests. The fixture is used to initialize the `ConfigManager` class with an environment file specified as `.env.example`. This fixture will be used in future tests to access and manipulate configuration settings.

Refactor the ConfigManager initialization tests

This commit refactors the initialization tests for the `ConfigManager` class. It adds two new tests: `test_init_with_env_variables` and `test_init_with_env_file`.

The `test_init_with_env_variables` test verifies that the `ConfigManager` class can be initialized with environment variables. It sets the required environment variables and asserts that the `ConfigManager` instance is correctly initialized with the expected values.

The `test_init_with_env_file` test verifies that the `ConfigManager` class can be initialized with an environment file. It mocks the existence of the environment file and asserts that the `ConfigManager` instance is correctly initialized with the expected values from the file.

These changes improve the test coverage and ensure that the `ConfigManager` class is properly tested for different initialization scenarios.
  • Loading branch information
MountainGod2 committed Nov 1, 2024
1 parent 83507b4 commit 241178a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pytest_mock import MockerFixture

from chaturbate_poller.chaturbate_client import ChaturbateClient
from chaturbate_poller.config_manager import ConfigManager
from chaturbate_poller.influxdb_handler import InfluxDBHandler
from chaturbate_poller.logging_config import (
CustomFormatter,
Expand Down Expand Up @@ -86,6 +87,12 @@ def setup_logging() -> None:
logging.getLogger().setLevel(logging.DEBUG)


@pytest.fixture(scope="module")
def config_manager() -> ConfigManager:
"""Fixture for the ConfigManager."""
return ConfigManager(env_file=".env.example")


@pytest.fixture
def http_client_mock(mocker: MockerFixture) -> Any:
"""Fixture for mocking the httpx.AsyncClient.get method."""
Expand Down
29 changes: 29 additions & 0 deletions tests/test_config_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os
from pathlib import Path
from unittest import mock

from chaturbate_poller.config_manager import ConfigManager


Expand All @@ -8,3 +12,28 @@ def test_get_with_default(self) -> None:
"""Test the get method with a default value."""
config_manager = ConfigManager()
assert config_manager.get("NON_EXISTENT_KEY", "default_value") == "default_value"

@mock.patch.dict(
os.environ,
{
"CB_USERNAME": "test_user",
"CB_TOKEN": "test_token",
"INFLUXDB_URL": "http://localhost:8086",
"USE_DATABASE": "true",
},
)
def test_init_with_env_variables(self) -> None:
"""Test initialization with environment variables."""
config_manager = ConfigManager()
assert config_manager.config["CB_USERNAME"] == "test_user"
assert config_manager.config["CB_TOKEN"] == "test_token" # noqa: S105
assert config_manager.config["INFLUXDB_URL"] == "http://localhost:8086"
assert config_manager.config["USE_DATABASE"] is True

@mock.patch("chaturbate_poller.config_manager.load_dotenv")
@mock.patch("chaturbate_poller.config_manager.Path.exists", return_value=True)
def test_init_with_env_file(self, mock_exists, mock_load_dotenv) -> None: # noqa: ANN001, ARG002
"""Test initialization with an environment file."""
config_manager = ConfigManager(env_file="test.env")
mock_load_dotenv.assert_called_once_with(dotenv_path=Path("test.env"))
assert config_manager.config == {"USE_DATABASE": False}

0 comments on commit 241178a

Please sign in to comment.