|
| 1 | +from lsprotocol.types import Position |
| 2 | +from sqlmesh.core.context import Context |
| 3 | +from sqlmesh.lsp.context import LSPContext, ModelTarget |
| 4 | +from sqlmesh.lsp.reference import get_cte_references |
| 5 | +from sqlmesh.lsp.uri import URI |
| 6 | +from tests.lsp.test_reference_cte import find_ranges_from_regex |
| 7 | + |
| 8 | + |
| 9 | +def test_cte_find_all_references(): |
| 10 | + context = Context(paths=["examples/sushi"]) |
| 11 | + lsp_context = LSPContext(context) |
| 12 | + |
| 13 | + sushi_customers_path = next( |
| 14 | + path |
| 15 | + for path, info in lsp_context.map.items() |
| 16 | + if isinstance(info, ModelTarget) and "sushi.customers" in info.names |
| 17 | + ) |
| 18 | + |
| 19 | + with open(sushi_customers_path, "r", encoding="utf-8") as file: |
| 20 | + read_file = file.readlines() |
| 21 | + |
| 22 | + # Test finding all references of "current_marketing" |
| 23 | + ranges = find_ranges_from_regex(read_file, r"current_marketing(?!_outer)") |
| 24 | + assert len(ranges) == 2 |
| 25 | + |
| 26 | + # Click on the CTE definition |
| 27 | + position = Position(line=ranges[0].start.line, character=ranges[0].start.character + 4) |
| 28 | + references = get_cte_references(lsp_context, URI.from_path(sushi_customers_path), position) |
| 29 | + |
| 30 | + # Should find both the definition and the usage |
| 31 | + assert len(references) == 2 |
| 32 | + assert all(ref.uri == URI.from_path(sushi_customers_path).value for ref in references) |
| 33 | + |
| 34 | + reference_ranges = [ref.range for ref in references] |
| 35 | + for expected_range in ranges: |
| 36 | + assert any( |
| 37 | + ref_range.start.line == expected_range.start.line |
| 38 | + and ref_range.start.character == expected_range.start.character |
| 39 | + for ref_range in reference_ranges |
| 40 | + ), ( |
| 41 | + f"Expected to find reference at line {expected_range.start.line}, char {expected_range.start.character}" |
| 42 | + ) |
| 43 | + |
| 44 | + # Click on the CTE usage |
| 45 | + position = Position(line=ranges[1].start.line, character=ranges[1].start.character + 4) |
| 46 | + references = get_cte_references(lsp_context, URI.from_path(sushi_customers_path), position) |
| 47 | + |
| 48 | + # Should find the same references |
| 49 | + assert len(references) == 2 |
| 50 | + assert all(ref.uri == URI.from_path(sushi_customers_path).value for ref in references) |
| 51 | + |
| 52 | + reference_ranges = [ref.range for ref in references] |
| 53 | + for expected_range in ranges: |
| 54 | + assert any( |
| 55 | + ref_range.start.line == expected_range.start.line |
| 56 | + and ref_range.start.character == expected_range.start.character |
| 57 | + for ref_range in reference_ranges |
| 58 | + ), ( |
| 59 | + f"Expected to find reference at line {expected_range.start.line}, char {expected_range.start.character}" |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def test_cte_find_all_references_outer(): |
| 64 | + context = Context(paths=["examples/sushi"]) |
| 65 | + lsp_context = LSPContext(context) |
| 66 | + |
| 67 | + sushi_customers_path = next( |
| 68 | + path |
| 69 | + for path, info in lsp_context.map.items() |
| 70 | + if isinstance(info, ModelTarget) and "sushi.customers" in info.names |
| 71 | + ) |
| 72 | + |
| 73 | + with open(sushi_customers_path, "r", encoding="utf-8") as file: |
| 74 | + read_file = file.readlines() |
| 75 | + |
| 76 | + # Test finding all references of "current_marketing_outer" |
| 77 | + ranges = find_ranges_from_regex(read_file, r"current_marketing_outer") |
| 78 | + assert len(ranges) == 2 |
| 79 | + |
| 80 | + # Click on the CTE definition |
| 81 | + position = Position(line=ranges[0].start.line, character=ranges[0].start.character + 4) |
| 82 | + references = get_cte_references(lsp_context, URI.from_path(sushi_customers_path), position) |
| 83 | + |
| 84 | + # Should find both the definition and the usage |
| 85 | + assert len(references) == 2 |
| 86 | + assert all(ref.uri == URI.from_path(sushi_customers_path).value for ref in references) |
| 87 | + |
| 88 | + # Verify that we found both occurrences |
| 89 | + reference_ranges = [ref.range for ref in references] |
| 90 | + for expected_range in ranges: |
| 91 | + assert any( |
| 92 | + ref_range.start.line == expected_range.start.line |
| 93 | + and ref_range.start.character == expected_range.start.character |
| 94 | + for ref_range in reference_ranges |
| 95 | + ), ( |
| 96 | + f"Expected to find reference at line {expected_range.start.line}, char {expected_range.start.character}" |
| 97 | + ) |
| 98 | + |
| 99 | + # Click on the CTE usage |
| 100 | + position = Position(line=ranges[1].start.line, character=ranges[1].start.character + 4) |
| 101 | + references = get_cte_references(lsp_context, URI.from_path(sushi_customers_path), position) |
| 102 | + |
| 103 | + # Should find the same references |
| 104 | + assert len(references) == 2 |
| 105 | + assert all(ref.uri == URI.from_path(sushi_customers_path).value for ref in references) |
| 106 | + |
| 107 | + reference_ranges = [ref.range for ref in references] |
| 108 | + for expected_range in ranges: |
| 109 | + assert any( |
| 110 | + ref_range.start.line == expected_range.start.line |
| 111 | + and ref_range.start.character == expected_range.start.character |
| 112 | + for ref_range in reference_ranges |
| 113 | + ), ( |
| 114 | + f"Expected to find reference at line {expected_range.start.line}, char {expected_range.start.character}" |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def test_cte_no_references_on_non_cte(): |
| 119 | + # Test that clicking on non-CTE elements returns nothing, once this is supported adapt this test accordingly |
| 120 | + context = Context(paths=["examples/sushi"]) |
| 121 | + lsp_context = LSPContext(context) |
| 122 | + |
| 123 | + sushi_customers_path = next( |
| 124 | + path |
| 125 | + for path, info in lsp_context.map.items() |
| 126 | + if isinstance(info, ModelTarget) and "sushi.customers" in info.names |
| 127 | + ) |
| 128 | + |
| 129 | + with open(sushi_customers_path, "r", encoding="utf-8") as file: |
| 130 | + read_file = file.readlines() |
| 131 | + |
| 132 | + # Click on a regular table reference |
| 133 | + ranges = find_ranges_from_regex(read_file, r"sushi\.orders") |
| 134 | + assert len(ranges) >= 1 |
| 135 | + |
| 136 | + position = Position(line=ranges[0].start.line, character=ranges[0].start.character + 4) |
| 137 | + references = get_cte_references(lsp_context, URI.from_path(sushi_customers_path), position) |
| 138 | + |
| 139 | + # Should find no references since this is not a CTE |
| 140 | + assert len(references) == 0 |
0 commit comments