Skip to content

Commit

Permalink
refactor(tests): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pivoshenko committed Sep 27, 2022
1 parent 7b80be9 commit 0a1af8e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
16 changes: 15 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Fixtures and configuration for the tests."""

import os

from pathlib import Path
from typing import Callable
from typing import Dict
Expand All @@ -9,7 +11,7 @@

@pytest.fixture()
def create_dotenv_file(tmp_path: Path) -> Callable[[str, str], Dict[str, str]]:
"""Get the dotenv filepath and it's content."""
"""Get a dotenv file."""

def create(filename: str, user: str) -> Dict[str, str]:
"""Create dotenv file."""
Expand All @@ -22,3 +24,15 @@ def create(filename: str, user: str) -> Dict[str, str]:
return dotenv_content

return create


@pytest.fixture()
def remove_dotenv_file() -> Callable[[str], None]:
"""Remove a dotenv file."""

def remove(filepath: str) -> None:
"""Remove a dotenv file."""

os.unlink(filepath)

return remove
28 changes: 19 additions & 9 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,65 @@


@mock.patch.dict(os.environ, {"POETRY_DOTENV_LOCATION": ".env.dev"}, clear=True)
def test_dev_dotenv_file(
def test_load_dotenv_non_default_file(
mocker: pytest_mock.MockerFixture,
create_dotenv_file: Callable[[str, str], Dict[str, str]],
remove_dotenv_file: Callable[[str], None],
) -> None:
"""Test for the plugin with the dev dotenv file location."""
"""Test ``load_dotenv`` function with a non default dotenv file."""

event = mocker.MagicMock()
event.command = EnvCommand()

expected_vars = create_dotenv_file(user="root", filename=".env.dev")
# noinspection PyArgumentList
expected_vars = create_dotenv_file("root", ".env.dev")

plugin = DotenvPlugin()
plugin.load_dotenv(event)

assert expected_vars["POSTGRES_USER"] == os.environ["POSTGRES_USER"]

remove_dotenv_file(".env.dev")

def test_default_dotenv_file(

def test_load_dotenv_default_file(
mocker: pytest_mock.MockerFixture,
create_dotenv_file: Callable[[str, str], Dict[str, str]],
remove_dotenv_file: Callable[[str], None],
) -> None:
"""Test for the plugin with the default dotenv file location."""
"""Test ``load_dotenv`` function with a default dotenv file."""

event = mocker.MagicMock()
event.command = EnvCommand()

expected_vars = create_dotenv_file(user="admin", filename=".env")
# noinspection PyArgumentList
expected_vars = create_dotenv_file("admin", ".env")

plugin = DotenvPlugin()
plugin.load_dotenv(event)

assert expected_vars["POSTGRES_USER"] == os.environ["POSTGRES_USER"]

remove_dotenv_file(".env")


@mock.patch.dict(os.environ, {"POETRY_DONT_LOAD_DOTENV": "1"}, clear=True)
def test_without_dotenv_file(
def test_load_dotenv_without_file(
mocker: pytest_mock.MockerFixture,
create_dotenv_file: Callable[[str, str], Dict[str, str]],
) -> None:
"""Test for the plugin without loading the dotenv file."""
"""Test ``load_dotenv`` function without a dotenv file."""

event = mocker.MagicMock()
event.command = EnvCommand()

create_dotenv_file(user="admin", filename=".env")
# noinspection PyArgumentList
create_dotenv_file("admin", ".env")

plugin = DotenvPlugin()
plugin.load_dotenv(event)

# WPS428 - "empty call" is necessary to check the exception
with pytest.raises(KeyError):
# noinspection PyStatementEffect
os.environ["POSTGRES_USER"] # noqa: WPS428

0 comments on commit 0a1af8e

Please sign in to comment.