Skip to content

Commit

Permalink
#880 patch -> monkeypatch
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallwhiteorg committed Oct 9, 2023
1 parent c74a342 commit f557fe7
Showing 1 changed file with 77 additions and 73 deletions.
150 changes: 77 additions & 73 deletions src/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
import sys
from unittest.mock import Mock, patch
from unittest.mock import Mock

from sql.magic import load_ipython_extension
from sql.connection import ConnectionManager
Expand Down Expand Up @@ -91,39 +91,41 @@ def test_start_ini_default_connection_if_any(tmp_empty, ip_no_magics):
assert ConnectionManager.current.dialect == "sqlite"


def test_load_home_toml_if_no_pyproject_toml(tmp_empty, ip_no_magics, capsys):
with patch.object(
def test_load_home_toml_if_no_pyproject_toml(
tmp_empty, ip_no_magics, capsys, monkeypatch
):
monkeypatch.setattr(
Path, "expanduser", lambda path: Path(str(path).replace("~", tmp_empty))
):
home_toml = Path("~/.jupysql/config").expanduser()
home_toml.parent.mkdir(exist_ok=True)
home_toml.write_text(
"""
[tool.jupysql.SqlMagic]
autocommit = false
autolimit = 1
style = "RANDOM"
"""
)
)
home_toml = Path("~/.jupysql/config").expanduser()
home_toml.parent.mkdir(exist_ok=True)
home_toml.write_text(
"""
[tool.jupysql.SqlMagic]
autocommit = false
autolimit = 1
style = "RANDOM"
"""
)

expect = [
"Settings changed:",
r"autocommit\s*\|\s*False",
r"autolimit\s*\|\s*1",
r"style\s*\|\s*RANDOM",
]
expect = [
"Settings changed:",
r"autocommit\s*\|\s*False",
r"autolimit\s*\|\s*1",
r"style\s*\|\s*RANDOM",
]

config_expected = {"autocommit": False, "autolimit": 1, "style": "RANDOM"}
config_expected = {"autocommit": False, "autolimit": 1, "style": "RANDOM"}

os.mkdir("sub")
os.chdir("sub")
os.mkdir("sub")
os.chdir("sub")

load_ipython_extension(ip_no_magics)
magic = ip_no_magics.find_magic("sql").__self__
combined = {**get_default_testing_configs(magic), **config_expected}
out, _ = capsys.readouterr()
assert all(re.search(substring, out) for substring in expect)
assert get_current_configs(magic) == combined
load_ipython_extension(ip_no_magics)
magic = ip_no_magics.find_magic("sql").__self__
combined = {**get_default_testing_configs(magic), **config_expected}
out, _ = capsys.readouterr()
assert all(re.search(substring, out) for substring in expect)
assert get_current_configs(magic) == combined


def test_start_ini_default_connection_using_toml_if_any(tmp_empty, ip_no_magics):
Expand Down Expand Up @@ -185,23 +187,25 @@ def test_magic_initialization_with_corrupted_pyproject_toml(
assert "Could not load configuration file" in captured.out


def test_magic_initialization_with_corrupted_home_toml(tmp_empty, ip_no_magics, capsys):
with patch.object(
def test_magic_initialization_with_corrupted_home_toml(
tmp_empty, ip_no_magics, capsys, monkeypatch
):
monkeypatch.setattr(
Path, "expanduser", lambda path: Path(str(path).replace("~", tmp_empty))
):
home_toml = Path("~/.jupysql/config").expanduser()
home_toml.parent.mkdir(exist_ok=True)
home_toml.write_text(
"""
[tool.jupysql.SqlMagic]
dsn_filename = myconnections.ini
"""
)
)
home_toml = Path("~/.jupysql/config").expanduser()
home_toml.parent.mkdir(exist_ok=True)
home_toml.write_text(
"""
[tool.jupysql.SqlMagic]
dsn_filename = myconnections.ini
"""
)

load_ipython_extension(ip_no_magics)
load_ipython_extension(ip_no_magics)

captured = capsys.readouterr()
assert "Could not load configuration file" in captured.out
captured = capsys.readouterr()
assert "Could not load configuration file" in captured.out


def test_loading_valid_pyproject_toml_shows_feedback_and_modifies_config(
Expand Down Expand Up @@ -241,42 +245,42 @@ def test_loading_valid_pyproject_toml_shows_feedback_and_modifies_config(


def test_loading_valid_home_toml_shows_feedback_and_modifies_config(
tmp_empty, ip_no_magics, capsys
tmp_empty, ip_no_magics, capsys, monkeypatch
):
with patch.object(
monkeypatch.setattr(
Path, "expanduser", lambda path: Path(str(path).replace("~", tmp_empty))
):
home_toml = Path("~/.jupysql/config").expanduser()
home_toml.parent.mkdir(exist_ok=True)
home_toml.write_text(
"""
[tool.jupysql.SqlMagic]
autocommit = false
autolimit = 1
style = "RANDOM"
"""
)
)
home_toml = Path("~/.jupysql/config").expanduser()
home_toml.parent.mkdir(exist_ok=True)
home_toml.write_text(
"""
[tool.jupysql.SqlMagic]
autocommit = false
autolimit = 1
style = "RANDOM"
"""
)

expect = [
"Loading configurations from {path}",
"Settings changed:",
r"autocommit\s*\|\s*False",
r"autolimit\s*\|\s*1",
r"style\s*\|\s*RANDOM",
]
expect = [
"Loading configurations from {path}",
"Settings changed:",
r"autocommit\s*\|\s*False",
r"autolimit\s*\|\s*1",
r"style\s*\|\s*RANDOM",
]

config_expected = {"autocommit": False, "autolimit": 1, "style": "RANDOM"}
config_expected = {"autocommit": False, "autolimit": 1, "style": "RANDOM"}

os.mkdir("sub")
os.chdir("sub")
os.mkdir("sub")
os.chdir("sub")

load_ipython_extension(ip_no_magics)
magic = ip_no_magics.find_magic("sql").__self__
combined = {**get_default_testing_configs(magic), **config_expected}
out, _ = capsys.readouterr()
expect[0] = expect[0].format(path=re.escape(str(home_toml)))
assert all(re.search(substring, out) for substring in expect)
assert get_current_configs(magic) == combined
load_ipython_extension(ip_no_magics)
magic = ip_no_magics.find_magic("sql").__self__
combined = {**get_default_testing_configs(magic), **config_expected}
out, _ = capsys.readouterr()
expect[0] = expect[0].format(path=re.escape(str(home_toml)))
assert all(re.search(substring, out) for substring in expect)
assert get_current_configs(magic) == combined


@pytest.mark.parametrize(
Expand Down

0 comments on commit f557fe7

Please sign in to comment.