|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test script for CTE go-to-definition functionality.""" |
| 3 | + |
| 4 | +import re |
| 5 | +from sqlmesh.core.context import Context |
| 6 | +from sqlmesh.lsp.context import LSPContext, ModelTarget |
| 7 | +from sqlmesh.lsp.reference import get_references |
| 8 | +from sqlmesh.lsp.uri import URI |
| 9 | +from lsprotocol.types import Range, Position |
| 10 | + |
| 11 | +def test_cte_parsing(): |
| 12 | + context = Context(paths=["examples/sushi"]) |
| 13 | + lsp_context = LSPContext(context) |
| 14 | + |
| 15 | + # Find model URIs |
| 16 | + sushi_customers_path = next( |
| 17 | + path |
| 18 | + for path, info in lsp_context.map.items() |
| 19 | + if isinstance(info, ModelTarget) and "sushi.customers" in info.names |
| 20 | + ) |
| 21 | + |
| 22 | + with open(sushi_customers_path, "r", encoding="utf-8") as file: |
| 23 | + read_file = file.readlines() |
| 24 | + |
| 25 | + references = get_references(lsp_context, URI.from_path(sushi_customers_path), Position(line=0, character=0)) |
| 26 | + |
| 27 | + assert len(references) == 1 |
| 28 | + assert references[0].uri == URI.from_path(sushi_customers_path) |
| 29 | + |
| 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\)") |
| 31 | + assert len(ranges) == 1 |
| 32 | + assert ranges[0].start == Position(line=1, character=0) |
| 33 | + assert ranges[0].end == Position(line=1, character=1) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +def find_ranges_from_regex(read_file: t.List[str], regex: str) -> t.List[Range]: |
| 40 | + """Find all ranges in the read file that match the regex.""" |
| 41 | + return [ |
| 42 | + Range( |
| 43 | + start=Position(line=line_number, character=match.start()), |
| 44 | + end=Position(line=line_number, character=match.end()) |
| 45 | + ) |
| 46 | + for line_number, line in enumerate(read_file) |
| 47 | + for match in [m for m in [re.search(regex, line)] if m] |
| 48 | + ] |
| 49 | + |
| 50 | + |
0 commit comments