Skip to content

Commit

Permalink
Merge pull request #30 from dbatten5/refactor-tests
Browse files Browse the repository at this point in the history
add fixtures for creating `pyproject.toml`
  • Loading branch information
dbatten5 authored Oct 29, 2021
2 parents f749237 + 3279db7 commit f21259b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
41 changes: 39 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""Store the classes and fixtures used throughout the tests."""
from pathlib import Path
from typing import Any
from typing import Callable
from typing import Dict
from typing import Optional

import pytest
import toml


@pytest.fixture()
def create_tmp_file(tmp_path: Path) -> Callable[..., Path]:
@pytest.fixture(name="create_tmp_file")
def create_tmp_file_fixture(tmp_path: Path) -> Callable[..., Path]:
"""Fixture for creating a temporary file."""

def _create_tmp_file(content: str = "", filename: str = "file.txt") -> Path:
Expand All @@ -15,3 +19,36 @@ def _create_tmp_file(content: str = "", filename: str = "file.txt") -> Path:
return tmp_file

return _create_tmp_file


@pytest.fixture(name="create_toml")
def create_toml_fixture(create_tmp_file: Callable[..., Path]) -> Callable[..., Path]:
"""Fixture for creating a `.toml` file."""

def _create_toml(
filename: str,
content: Optional[Dict[str, Any]] = None,
) -> Path:
content = content or {}
config_toml = toml.dumps(content)
toml_path = create_tmp_file(content=config_toml, filename=filename)
return toml_path

return _create_toml


@pytest.fixture()
def create_pyproject_toml(create_toml: Callable[..., Path]) -> Callable[..., Path]:
"""Fixture for creating a `pyproject.toml`."""

def _create_pyproject_toml(
section_name: str = "foo",
content: Optional[Dict[str, Any]] = None,
filename: str = "pyproject.toml",
) -> Path:
content = content or {"bar": "baz"}
config_dict = {"tool": {section_name: content}}
pyproject_path = create_toml(filename=filename, content=config_dict)
return pyproject_path

return _create_pyproject_toml
61 changes: 24 additions & 37 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Callable

import pytest
import toml
from pydantic import ValidationError

from maison.config import ProjectConfig
Expand Down Expand Up @@ -37,32 +36,29 @@ def test_repr_no_config_path(self, create_tmp_file: Callable[..., Path]) -> None

assert str(config) == "ProjectConfig (config_path=None)"

def test_to_dict(self, create_tmp_file: Callable[..., Path]) -> None:
def test_to_dict(self, create_pyproject_toml: Callable[..., Path]) -> None:
"""
Given an instance of `ProjectConfig`,
When the `to_dict` method is invoked,
Then a dictionary of all config options is returned
"""
config_dict = {"tool": {"foo": {"bar": "baz"}}}
config_toml = toml.dumps(config_dict)
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_pyproject_toml()

config = ProjectConfig(project_name="foo", starting_path=pyproject_path)

assert config.to_dict() == config_dict["tool"]["foo"]
assert config.to_dict() == {"bar": "baz"}


class TestGetOption:
"""Tests for the `get_option` method."""

def test_valid_pyproject(self, create_tmp_file: Callable[..., Path]) -> None:
def test_valid_pyproject(self, create_pyproject_toml: Callable[..., Path]) -> None:
"""
Given a valid `pyproject.toml`,
When the `ProjectConfig` class is instantiated,
Then a config value can be retrieved
"""
config_toml = toml.dumps({"tool": {"foo": {"bar": "baz"}}})
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_pyproject_toml()
config = ProjectConfig(project_name="foo", starting_path=pyproject_path)

result = config.get_option("bar")
Expand Down Expand Up @@ -94,15 +90,14 @@ def test_default(self) -> None:
assert result == "baz"

def test_valid_pyproject_with_no_project_section(
self, create_tmp_file: Callable[..., Path]
self, create_toml: Callable[..., Path]
) -> None:
"""
Given a valid `pyproject.toml`,
Given a valid `pyproject.toml` without a [tool.{project_name}] section,
When the `ProjectConfig` class is instantiated,
Then a config value can be retrieved
"""
config_toml = toml.dumps({"foo": "bar"})
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_toml(filename="pyproject.toml", content={"foo": "bar"})
config = ProjectConfig(project_name="baz", starting_path=pyproject_path)

result = config.get_option("foo")
Expand All @@ -125,15 +120,14 @@ def test_not_found(self) -> None:
assert config.to_dict() == {}

def test_single_valid_toml_source(
self, create_tmp_file: Callable[..., Path]
self, create_pyproject_toml: Callable[..., Path]
) -> None:
"""
Given a `toml` source file other than `pyproject.toml`,
When the `ProjectConfig` is instantiated with the source,
Then the source is retrieved correctly
"""
config_toml = toml.dumps({"tool": {"foo": {"bar": "baz"}}})
source_path = create_tmp_file(content=config_toml, filename="another.toml")
source_path = create_pyproject_toml(filename="another.toml")

config = ProjectConfig(
project_name="foo",
Expand All @@ -146,19 +140,17 @@ def test_single_valid_toml_source(
assert result == "baz"

def test_multiple_valid_toml_sources(
self, create_tmp_file: Callable[..., Path]
self, create_pyproject_toml: Callable[..., Path]
) -> None:
"""
Given multiple `toml` source files,
When the `ProjectConfig` is instantiated with the sources,
Then first source to be found is retrieved correctly
"""
config_toml_1 = toml.dumps({"tool": {"foo": {"bar": "baz"}}})
source_path_1 = create_tmp_file(content=config_toml_1, filename="another.toml")
source_path_1 = create_pyproject_toml(filename="another.toml")

config_toml_2 = toml.dumps({"tool": {"oof": {"rab": "zab"}}})
source_path_2 = create_tmp_file(
content=config_toml_2, filename="pyproject.toml"
source_path_2 = create_pyproject_toml(
section_name="oof", content={"rab": "zab"}
)

config = ProjectConfig(
Expand Down Expand Up @@ -222,7 +214,7 @@ def test_no_schema(self) -> None:

def test_one_schema_with_valid_config(
self,
create_tmp_file: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""
Given an instance of `ProjectConfig` with a given schema,
Expand All @@ -235,8 +227,7 @@ class Schema(ConfigSchema):

bar: str

config_toml = toml.dumps({"tool": {"foo": {"bar": "baz"}}})
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_pyproject_toml()
config = ProjectConfig(
project_name="foo",
starting_path=pyproject_path,
Expand All @@ -249,7 +240,7 @@ class Schema(ConfigSchema):

def test_use_schema_values(
self,
create_tmp_file: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""
Given an instance of `ProjectConfig` with a given schema,
Expand All @@ -264,8 +255,7 @@ class Schema(ConfigSchema):
bar: str
other: str = "hello"

config_toml = toml.dumps({"tool": {"foo": {"bar": 1}}})
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_pyproject_toml(content={"bar": 1})
config = ProjectConfig(
project_name="foo",
starting_path=pyproject_path,
Expand All @@ -279,7 +269,7 @@ class Schema(ConfigSchema):

def test_not_use_schema_values(
self,
create_tmp_file: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""
Given an instance of `ProjectConfig` with a given schema,
Expand All @@ -293,8 +283,7 @@ class Schema(ConfigSchema):
bar: str
other: str = "hello"

config_toml = toml.dumps({"tool": {"foo": {"bar": 1}}})
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_pyproject_toml(content={"bar": 1})
config = ProjectConfig(
project_name="foo",
starting_path=pyproject_path,
Expand All @@ -308,7 +297,7 @@ class Schema(ConfigSchema):

def test_schema_override(
self,
create_tmp_file: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""
Given an instance of `ProjectConfig` with a given schema,
Expand All @@ -326,8 +315,7 @@ class Schema2(ConfigSchema):

bar: str = "schema_2"

config_toml = toml.dumps({"tool": {"foo": {}}})
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_pyproject_toml(content={"baz": "baz"})
config = ProjectConfig(
project_name="foo",
starting_path=pyproject_path,
Expand All @@ -340,7 +328,7 @@ class Schema2(ConfigSchema):

def test_invalid_configuration(
self,
create_tmp_file: Callable[..., Path],
create_pyproject_toml: Callable[..., Path],
) -> None:
"""
Given a configuration which doesn't conform to the schema,
Expand All @@ -353,8 +341,7 @@ class Schema(ConfigSchema):

bar: str

config_toml = toml.dumps({"tool": {"foo": {}}})
pyproject_path = create_tmp_file(content=config_toml, filename="pyproject.toml")
pyproject_path = create_pyproject_toml(content={"baz": "baz"})
config = ProjectConfig(
project_name="foo",
starting_path=pyproject_path,
Expand Down

0 comments on commit f21259b

Please sign in to comment.