-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathtest_code_actions.py
More file actions
183 lines (158 loc) · 6.46 KB
/
test_code_actions.py
File metadata and controls
183 lines (158 loc) · 6.46 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import typing as t
import os
from lsprotocol import types
from sqlmesh.core.context import Context
from sqlmesh.lsp.context import LSPContext
from sqlmesh.lsp.uri import URI
def test_code_actions_with_linting(copy_to_temp_path: t.Callable):
"""Test that code actions are generated for linting violations."""
# Copy sushi example to a temporary directory
sushi_paths = copy_to_temp_path("examples/sushi")
sushi_path = sushi_paths[0]
# Override the config and turn the linter on
config_path = sushi_path / "config.py"
with config_path.open("r") as f:
lines = f.readlines()
lines = [
line.replace("enabled=False,", "enabled=True,") if "enabled=False," in line else line
for line in lines
]
with config_path.open("w") as f:
f.writelines(lines)
# Override the latest_order.sql file to introduce a linter violation
model_content = """MODEL (
name sushi.latest_order,
kind CUSTOM (
materialization 'custom_full_with_custom_kind',
materialization_properties (
custom_property = 'sushi!!!'
)
),
cron '@daily'
);
SELECT *
FROM sushi.orders
ORDER BY event_date DESC LIMIT 1
"""
latest_order_path = sushi_path / "models" / "latest_order.sql"
with latest_order_path.open("w") as f:
f.write(model_content)
# Create context with the mocked config
context = Context(paths=[str(sushi_path)])
# Create LSP context
lsp_context = LSPContext(context)
# Get diagnostics (linting violations)
violations = lsp_context.lint_model(URI.from_path(sushi_path / "models" / "latest_order.sql"))
uri = URI.from_path(sushi_path / "models" / "latest_order.sql")
# First, convert violations to LSP diagnostics
diagnostics = []
for violation in violations:
if violation.violation_range:
diagnostic = types.Diagnostic(
range=types.Range(
start=types.Position(
line=violation.violation_range.start.line,
character=violation.violation_range.start.character,
),
end=types.Position(
line=violation.violation_range.end.line,
character=violation.violation_range.end.character,
),
),
message=violation.violation_msg,
severity=types.DiagnosticSeverity.Warning,
)
diagnostics.append(diagnostic)
# Create code action params with diagnostics
params = types.CodeActionParams(
text_document=types.TextDocumentIdentifier(uri=uri.value),
range=types.Range(
start=types.Position(line=0, character=0),
end=types.Position(line=100, character=0),
),
context=types.CodeActionContext(diagnostics=diagnostics),
)
# Get code actions
code_actions = lsp_context.get_code_actions(
URI.from_path(sushi_path / "models" / "latest_order.sql"), params
)
# Verify we have code actions
assert code_actions is not None
assert len(code_actions) > 0
# Verify the code action properties
first_action = code_actions[0]
if not isinstance(first_action, types.CodeAction):
raise AssertionError("First action is not a CodeAction instance")
assert first_action.kind == types.CodeActionKind.QuickFix
assert first_action.edit is not None
assert first_action.edit.changes is not None
assert (
URI.from_path(sushi_path / "models" / "latest_order.sql").value in first_action.edit.changes
)
# The fix should replace SELECT * with specific columns
text_edits = first_action.edit.changes[
URI.from_path(sushi_path / "models" / "latest_order.sql").value
]
assert len(text_edits) > 0
def test_code_actions_create_file(copy_to_temp_path: t.Callable) -> None:
sushi_paths = copy_to_temp_path("examples/sushi")
sushi_path = sushi_paths[0]
# Remove external models file and enable linter
os.remove(sushi_path / "external_models.yaml")
config_path = sushi_path / "config.py"
with config_path.open("r") as f:
content = f.read()
before = """ linter=LinterConfig(
enabled=False,
rules=[
"ambiguousorinvalidcolumn",
"invalidselectstarexpansion",
"noselectstar",
"nomissingaudits",
"nomissingowner",
"nomissingexternalmodels",
"cronintervalalignment",
],
),"""
after = """linter=LinterConfig(enabled=True, rules=["nomissingexternalmodels"]),"""
content = content.replace(before, after)
with config_path.open("w") as f:
f.write(content)
context = Context(paths=[str(sushi_path)])
lsp_context = LSPContext(context)
uri = URI.from_path(sushi_path / "models" / "customers.sql")
violations = lsp_context.lint_model(uri)
diagnostics = []
for violation in violations:
if violation.violation_range:
diagnostics.append(
types.Diagnostic(
range=types.Range(
start=types.Position(
line=violation.violation_range.start.line,
character=violation.violation_range.start.character,
),
end=types.Position(
line=violation.violation_range.end.line,
character=violation.violation_range.end.character,
),
),
message=violation.violation_msg,
severity=types.DiagnosticSeverity.Warning,
)
)
params = types.CodeActionParams(
text_document=types.TextDocumentIdentifier(uri=uri.value),
range=types.Range(
start=types.Position(line=0, character=0), end=types.Position(line=1, character=0)
),
context=types.CodeActionContext(diagnostics=diagnostics),
)
actions = lsp_context.get_code_actions(uri, params)
assert actions is not None and len(actions) > 0
action = next(a for a in actions if isinstance(a, types.CodeAction))
assert action.edit is not None
assert action.edit.document_changes is not None
create_file = [c for c in action.edit.document_changes if isinstance(c, types.CreateFile)]
assert create_file, "Expected a CreateFile operation"
assert create_file[0].uri == URI.from_path(sushi_path / "external_models.yaml").value