-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_diff.py
More file actions
152 lines (127 loc) · 4.22 KB
/
Copy pathtest_diff.py
File metadata and controls
152 lines (127 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from __future__ import annotations
from pathlib import Path
import pytest
from pgpkg.api import generate_incremental
pytestmark = pytest.mark.integration
def test_makemigration_produces_diff(staged_project: Path, pg_url: str):
# Add a new column to sql/ so 0.2.0 -> unreleased produces a meaningful diff.
(staged_project / "sql" / "030_newtable.sql").write_text(
"CREATE TABLE IF NOT EXISTS sampleext.extra (id int PRIMARY KEY);\n"
)
from pgpkg.api import stage_version
stage_version(staged_project, "unreleased")
path = generate_incremental(
staged_project,
from_version="0.2.0",
to_version="unreleased",
base_url=pg_url,
)
body = path.read_text().lower()
assert "extra" in body
assert "create table" in body
def test_makemigration_empty_when_identical(staged_project: Path, pg_url: str):
# 0.2.0 is what sql/ renders to right now (before modifications) — stage unreleased
# from the same sql/ state, then diff: result should be empty/trivial.
from pgpkg.api import stage_version
stage_version(staged_project, "unreleased")
path = generate_incremental(
staged_project,
from_version="0.2.0",
to_version="unreleased",
base_url=pg_url,
)
# Body contains header + (possibly empty) diff body. Strip comments.
body = path.read_text()
non_comment = "\n".join(
line for line in body.splitlines() if line.strip() and not line.strip().startswith("--")
).strip()
assert non_comment == ""
def test_makemigration_wraps_diff_with_files_and_sql(
staged_project: Path,
pg_url: str,
tmp_path: Path,
):
from pgpkg.api import stage_version
(staged_project / "sql" / "030_newtable.sql").write_text(
"CREATE TABLE IF NOT EXISTS sampleext.extra (id int PRIMARY KEY);\n"
)
stage_version(staged_project, "unreleased")
prepend = tmp_path / "000_pre.sql"
prepend.write_text("SET search_path TO sampleext, public;\n")
append = tmp_path / "999_post.sql"
append.write_text("SELECT 42;\n")
path = generate_incremental(
staged_project,
from_version="0.2.0",
to_version="unreleased",
base_url=pg_url,
prepend_files=[prepend],
append_files=[append],
append_sql=["SELECT 'done';"],
)
body = path.read_text()
assert "SET search_path TO sampleext, public;" in body
assert '"sampleext"."extra"' in body
assert "SELECT 42;" in body
assert "SELECT 'done';" in body
def test_makemigration_includes_body_only_function_changes(
staged_project: Path,
pg_url: str,
):
from pgpkg.api import stage_version
# Change only function body/signature internals without adding/removing objects.
(staged_project / "sql" / "020_functions.sql").write_text(
"""CREATE OR REPLACE FUNCTION sampleext.item_count()
RETURNS bigint
LANGUAGE sql
AS $$
SELECT count(*) + 1 FROM sampleext.items;
$$;
"""
)
stage_version(staged_project, "unreleased")
path = generate_incremental(
staged_project,
from_version="0.2.0",
to_version="unreleased",
base_url=pg_url,
)
body = path.read_text().lower()
assert "create or replace function sampleext.item_count()" in body
assert "count(*) + 1" in body
def test_makemigration_includes_body_only_procedure_changes(
staged_project: Path,
pg_url: str,
):
from pgpkg.api import stage_version
(staged_project / "sql" / "020_functions.sql").write_text(
"""CREATE OR REPLACE PROCEDURE sampleext.refresh_items()
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM count(*) FROM sampleext.items;
END;
$$;
"""
)
stage_version(staged_project, "0.2.0")
(staged_project / "sql" / "020_functions.sql").write_text(
"""CREATE OR REPLACE PROCEDURE sampleext.refresh_items()
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM count(*) + 1 FROM sampleext.items;
END;
$$;
"""
)
stage_version(staged_project, "unreleased")
path = generate_incremental(
staged_project,
from_version="0.2.0",
to_version="unreleased",
base_url=pg_url,
)
body = path.read_text().lower()
assert "create or replace procedure sampleext.refresh_items()" in body
assert "count(*) + 1" in body