Skip to content

Commit 9c7507b

Browse files
refactor: replace with-patch context managers with @patch decorators in tests
1 parent 9dc608f commit 9c7507b

2 files changed

Lines changed: 190 additions & 244 deletions

File tree

tests/test_generate_sql.py

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def write_file(tmp_path, source):
1616
return str(path)
1717

1818

19-
def test_returns_sql_for_valid_migration(tmp_path):
19+
@patch("subprocess.run")
20+
def test_returns_sql_for_valid_migration(mock_run, tmp_path):
2021
path = write_file(
2122
tmp_path,
2223
"""
@@ -28,11 +29,8 @@ def upgrade():
2829
""",
2930
)
3031
expected_sql = "CREATE TABLE foo (id int);\n"
31-
with patch(
32-
"subprocess.run",
33-
return_value=make_result(stdout=expected_sql),
34-
):
35-
assert generate_sql(path) == expected_sql
32+
mock_run.return_value = make_result(stdout=expected_sql)
33+
assert generate_sql(path) == expected_sql
3634

3735

3836
def test_returns_none_for_unparseable_file(tmp_path):
@@ -67,7 +65,8 @@ def upgrade():
6765
assert generate_sql(path) is None
6866

6967

70-
def test_uses_base_when_down_revision_is_none(tmp_path):
68+
@patch("subprocess.run")
69+
def test_uses_base_when_down_revision_is_none(mock_run, tmp_path):
7170
path = write_file(
7271
tmp_path,
7372
"""
@@ -78,16 +77,14 @@ def upgrade():
7877
pass
7978
""",
8079
)
81-
with patch(
82-
"subprocess.run",
83-
return_value=make_result(stdout="CREATE TABLE foo (id int);\n"),
84-
) as mock_run:
85-
generate_sql(path)
86-
cmd = mock_run.call_args[0][0]
87-
assert "base:first001" in cmd
80+
mock_run.return_value = make_result(stdout="CREATE TABLE foo (id int);\n")
81+
generate_sql(path)
82+
cmd = mock_run.call_args[0][0]
83+
assert "base:first001" in cmd
8884

8985

90-
def test_raises_on_alembic_failure(tmp_path):
86+
@patch("subprocess.run")
87+
def test_raises_on_alembic_failure(mock_run, tmp_path):
9188
path = write_file(
9289
tmp_path,
9390
"""
@@ -98,15 +95,13 @@ def upgrade():
9895
pass
9996
""",
10097
)
101-
with patch(
102-
"subprocess.run",
103-
return_value=make_result(returncode=1, stderr="some error"),
104-
):
105-
with pytest.raises(GenerateSqlError, match="alembic upgrade --sql failed"):
106-
generate_sql(path)
98+
mock_run.return_value = make_result(returncode=1, stderr="some error")
99+
with pytest.raises(GenerateSqlError, match="alembic upgrade --sql failed"):
100+
generate_sql(path)
107101

108102

109-
def test_raises_when_alembic_not_found(tmp_path):
103+
@patch("subprocess.run")
104+
def test_raises_when_alembic_not_found(mock_run, tmp_path):
110105
path = write_file(
111106
tmp_path,
112107
"""
@@ -117,12 +112,13 @@ def upgrade():
117112
pass
118113
""",
119114
)
120-
with patch("subprocess.run", side_effect=FileNotFoundError):
121-
with pytest.raises(GenerateSqlError, match="alembic not found"):
122-
generate_sql(path)
115+
mock_run.side_effect = FileNotFoundError
116+
with pytest.raises(GenerateSqlError, match="alembic not found"):
117+
generate_sql(path)
123118

124119

125-
def test_provides_dummy_database_url_when_unset(tmp_path, monkeypatch):
120+
@patch("subprocess.run")
121+
def test_provides_dummy_database_url_when_unset(mock_run, tmp_path, monkeypatch):
126122
monkeypatch.delenv("DATABASE_URL", raising=False)
127123
path = write_file(
128124
tmp_path,
@@ -134,16 +130,14 @@ def upgrade():
134130
pass
135131
""",
136132
)
137-
with patch(
138-
"subprocess.run",
139-
return_value=make_result(stdout="SQL;\n"),
140-
) as mock_run:
141-
generate_sql(path)
142-
env = mock_run.call_args[1]["env"]
143-
assert env["DATABASE_URL"] == "postgresql://localhost/lint"
133+
mock_run.return_value = make_result(stdout="SQL;\n")
134+
generate_sql(path)
135+
env = mock_run.call_args[1]["env"]
136+
assert env["DATABASE_URL"] == "postgresql://localhost/lint"
144137

145138

146-
def test_preserves_existing_database_url(tmp_path, monkeypatch):
139+
@patch("subprocess.run")
140+
def test_preserves_existing_database_url(mock_run, tmp_path, monkeypatch):
147141
monkeypatch.setenv("DATABASE_URL", "postgresql://real-host/real-db")
148142
path = write_file(
149143
tmp_path,
@@ -155,10 +149,7 @@ def upgrade():
155149
pass
156150
""",
157151
)
158-
with patch(
159-
"subprocess.run",
160-
return_value=make_result(stdout="SQL;\n"),
161-
) as mock_run:
162-
generate_sql(path)
163-
env = mock_run.call_args[1]["env"]
164-
assert env["DATABASE_URL"] == "postgresql://real-host/real-db"
152+
mock_run.return_value = make_result(stdout="SQL;\n")
153+
generate_sql(path)
154+
env = mock_run.call_args[1]["env"]
155+
assert env["DATABASE_URL"] == "postgresql://real-host/real-db"

0 commit comments

Comments
 (0)