Begin extracting contexts into separate module
This commit is contained in:
@@ -1,18 +1,33 @@
|
||||
# test_main.py
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
from iottb.main import cli # Adjust this import according to your project's structure
|
||||
from pathlib import Path
|
||||
from iottb.main import load_config
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def runner():
|
||||
"""Fixture to return a Click CliRunner instance."""
|
||||
return CliRunner()
|
||||
class TestLoadConfig:
|
||||
|
||||
# Loads configuration from an existing file
|
||||
def test_loads_config_from_existing_file(self, mocker):
|
||||
cfg_file = mocker.Mock()
|
||||
cfg_file.is_file.return_value = True
|
||||
mock_open = mocker.mock_open(
|
||||
read_data=f'{{"DefaultDatabase": "test_db", "DefaultDatabasePath": "{Path.home()}/user"}}')
|
||||
mocker.patch('builtins.open', mock_open)
|
||||
|
||||
def test_debug_option(runner):
|
||||
"""Test if the debug mode sets the appropriate flag in the context."""
|
||||
result = runner.invoke(cli, ['--debug'])
|
||||
assert result.exit_code == 0
|
||||
# If debug mode affects logging or other behavior, validate those:
|
||||
assert 'DEBUG mode on' in result.output
|
||||
result = load_config(cfg_file)
|
||||
|
||||
assert result == {"DefaultDatabase": "test_db", "DefaultDatabasePath": f"{Path.home()}/user"}
|
||||
cfg_file.is_file.assert_called_once()
|
||||
mock_open.assert_called_once_with(cfg_file, 'r')
|
||||
|
||||
# File path is invalid or inaccessible
|
||||
def test_file_path_invalid_or_inaccessible(self, mocker):
|
||||
cfg_file = mocker.Mock()
|
||||
cfg_file.is_file.return_value = False
|
||||
mock_create_default_config = mocker.patch('iottb.main.create_default_config',
|
||||
return_value={"DefaultDatabase": "default_db",
|
||||
"DefaultDatabasePath": f"{Path.home()}/default"})
|
||||
|
||||
result = load_config(cfg_file)
|
||||
|
||||
assert result == {"DefaultDatabase": "default_db", "DefaultDatabasePath": f"{Path.home()}/default"}
|
||||
cfg_file.is_file.assert_called_once()
|
||||
mock_create_default_config.assert_called_once_with(cfg_file)
|
||||
|
||||
Reference in New Issue
Block a user