1
- #!/usr/bin/env python3
2
- """Test script for CTE go-to-definition functionality."""
3
-
4
1
import re
5
2
from sqlmesh .core .context import Context
6
3
from sqlmesh .lsp .context import LSPContext , ModelTarget
7
4
from sqlmesh .lsp .reference import get_references
8
5
from sqlmesh .lsp .uri import URI
9
6
from lsprotocol .types import Range , Position
7
+ import typing as t
8
+
10
9
11
10
def test_cte_parsing ():
12
11
context = Context (paths = ["examples/sushi" ])
@@ -22,29 +21,41 @@ def test_cte_parsing():
22
21
with open (sushi_customers_path , "r" , encoding = "utf-8" ) as file :
23
22
read_file = file .readlines ()
24
23
25
- references = get_references (lsp_context , URI .from_path (sushi_customers_path ), Position (line = 0 , character = 0 ))
24
+ # Find position of the cte reference
25
+ ranges = find_ranges_from_regex (
26
+ read_file ,
27
+ r"current_marketing"
28
+ )
29
+ assert len (ranges ) == 2
30
+ # Middle of the second range
31
+ position = Position (line = ranges [1 ].start .line , character = ranges [1 ].start .character + 4 )
32
+
33
+ references = get_references (
34
+ lsp_context , URI .from_path (sushi_customers_path ), position
35
+ )
26
36
27
37
assert len (references ) == 1
28
38
assert references [0 ].uri == URI .from_path (sushi_customers_path )
39
+ assert references [0 ].description is None
29
40
30
- ranges = find_ranges_from_regex (read_file , r"WITH\s+current_marketing\s+AS\s+\(SELECT\s+customer_id,\s+status\s+FROM\s+sushi\.marketing\s+WHERE\s+valid_to\s+is\s+null\)" )
41
+ ranges = find_ranges_from_regex (
42
+ read_file ,
43
+ r"WITH\s+current_marketing\s+AS\s+\(SELECT\s+customer_id,\s+status\s+FROM\s+sushi\.marketing\s+WHERE\s+valid_to\s+is\s+null\)" ,
44
+ )
31
45
assert len (ranges ) == 1
32
46
assert ranges [0 ].start == Position (line = 1 , character = 0 )
33
47
assert ranges [0 ].end == Position (line = 1 , character = 1 )
34
48
35
-
36
-
49
+ assert references [0 ].range == ranges [0 ]
37
50
38
51
39
52
def find_ranges_from_regex (read_file : t .List [str ], regex : str ) -> t .List [Range ]:
40
53
"""Find all ranges in the read file that match the regex."""
41
54
return [
42
55
Range (
43
56
start = Position (line = line_number , character = match .start ()),
44
- end = Position (line = line_number , character = match .end ())
57
+ end = Position (line = line_number , character = match .end ()),
45
58
)
46
59
for line_number , line in enumerate (read_file )
47
60
for match in [m for m in [re .search (regex , line )] if m ]
48
61
]
49
-
50
-
0 commit comments