|
1 | 1 | import os |
2 | 2 |
|
3 | 3 | from sqlmesh import Context |
| 4 | +from sqlmesh.core.linter.rule import Position, Range |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def test_no_missing_external_models(tmp_path, copy_to_temp_path) -> None: |
@@ -44,8 +45,124 @@ def test_no_missing_external_models(tmp_path, copy_to_temp_path) -> None: |
44 | 45 | # Lint the models |
45 | 46 | lints = context.lint_models(raise_on_error=False) |
46 | 47 | assert len(lints) == 1 |
47 | | - assert lints[0].violation_range is not None |
| 48 | + lint = lints[0] |
| 49 | + assert lint.violation_range is not None |
48 | 50 | assert ( |
49 | | - lints[0].violation_msg |
| 51 | + lint.violation_msg |
50 | 52 | == """Model '"memory"."sushi"."customers"' depends on unregistered external model '"memory"."raw"."demographics"'. Please register it in the external models file. This can be done by running 'sqlmesh create_external_models'.""" |
51 | 53 | ) |
| 54 | + assert len(lint.fixes) == 0 |
| 55 | + |
| 56 | + |
| 57 | +def test_no_missing_external_models_with_existing_file_ending_in_newline( |
| 58 | + tmp_path, copy_to_temp_path |
| 59 | +) -> None: |
| 60 | + sushi_paths = copy_to_temp_path("examples/sushi") |
| 61 | + sushi_path = sushi_paths[0] |
| 62 | + |
| 63 | + # Overwrite the external_models.yaml file to end with a random file and a newline |
| 64 | + os.remove(sushi_path / "external_models.yaml") |
| 65 | + with open(sushi_path / "external_models.yaml", "w") as f: |
| 66 | + f.write("- name: memory.raw.test\n") |
| 67 | + |
| 68 | + # Override the config.py to turn on lint |
| 69 | + with open(sushi_path / "config.py", "r") as f: |
| 70 | + read_file = f.read() |
| 71 | + |
| 72 | + before = """ linter=LinterConfig( |
| 73 | + enabled=False, |
| 74 | + rules=[ |
| 75 | + "ambiguousorinvalidcolumn", |
| 76 | + "invalidselectstarexpansion", |
| 77 | + "noselectstar", |
| 78 | + "nomissingaudits", |
| 79 | + "nomissingowner", |
| 80 | + "nomissingexternalmodels", |
| 81 | + ], |
| 82 | + ),""" |
| 83 | + after = """linter=LinterConfig(enabled=True, rules=["nomissingexternalmodels"]),""" |
| 84 | + read_file = read_file.replace(before, after) |
| 85 | + assert after in read_file |
| 86 | + with open(sushi_path / "config.py", "w") as f: |
| 87 | + f.writelines(read_file) |
| 88 | + |
| 89 | + # Load the context with the temporary sushi path |
| 90 | + context = Context(paths=[sushi_path]) |
| 91 | + |
| 92 | + # Lint the models |
| 93 | + lints = context.lint_models(raise_on_error=False) |
| 94 | + assert len(lints) == 1 |
| 95 | + lint = lints[0] |
| 96 | + assert lint.violation_range is not None |
| 97 | + assert ( |
| 98 | + lint.violation_msg |
| 99 | + == """Model '"memory"."sushi"."customers"' depends on unregistered external model '"memory"."raw"."demographics"'. Please register it in the external models file. This can be done by running 'sqlmesh create_external_models'.""" |
| 100 | + ) |
| 101 | + assert len(lint.fixes) == 1 |
| 102 | + fix = lint.fixes[0] |
| 103 | + assert len(fix.edits) == 1 |
| 104 | + edit = fix.edits[0] |
| 105 | + assert edit.new_text == """- name: '"memory"."raw"."demographics"'\n""" |
| 106 | + assert edit.range == Range( |
| 107 | + start=Position(line=1, character=0), |
| 108 | + end=Position(line=1, character=0), |
| 109 | + ) |
| 110 | + fix_path = sushi_path / "external_models.yaml" |
| 111 | + assert edit.path == fix_path |
| 112 | + |
| 113 | + |
| 114 | +def test_no_missing_external_models_with_existing_file_not_ending_in_newline( |
| 115 | + tmp_path, copy_to_temp_path |
| 116 | +) -> None: |
| 117 | + sushi_paths = copy_to_temp_path("examples/sushi") |
| 118 | + sushi_path = sushi_paths[0] |
| 119 | + |
| 120 | + # Overwrite the external_models.yaml file to end with a random file and a newline |
| 121 | + os.remove(sushi_path / "external_models.yaml") |
| 122 | + with open(sushi_path / "external_models.yaml", "w") as f: |
| 123 | + f.write("- name: memory.raw.test") |
| 124 | + |
| 125 | + # Override the config.py to turn on lint |
| 126 | + with open(sushi_path / "config.py", "r") as f: |
| 127 | + read_file = f.read() |
| 128 | + |
| 129 | + before = """ linter=LinterConfig( |
| 130 | + enabled=False, |
| 131 | + rules=[ |
| 132 | + "ambiguousorinvalidcolumn", |
| 133 | + "invalidselectstarexpansion", |
| 134 | + "noselectstar", |
| 135 | + "nomissingaudits", |
| 136 | + "nomissingowner", |
| 137 | + "nomissingexternalmodels", |
| 138 | + ], |
| 139 | + ),""" |
| 140 | + after = """linter=LinterConfig(enabled=True, rules=["nomissingexternalmodels"]),""" |
| 141 | + read_file = read_file.replace(before, after) |
| 142 | + assert after in read_file |
| 143 | + with open(sushi_path / "config.py", "w") as f: |
| 144 | + f.writelines(read_file) |
| 145 | + |
| 146 | + # Load the context with the temporary sushi path |
| 147 | + context = Context(paths=[sushi_path]) |
| 148 | + |
| 149 | + # Lint the models |
| 150 | + lints = context.lint_models(raise_on_error=False) |
| 151 | + assert len(lints) == 1 |
| 152 | + lint = lints[0] |
| 153 | + assert lint.violation_range is not None |
| 154 | + assert ( |
| 155 | + lint.violation_msg |
| 156 | + == """Model '"memory"."sushi"."customers"' depends on unregistered external model '"memory"."raw"."demographics"'. Please register it in the external models file. This can be done by running 'sqlmesh create_external_models'.""" |
| 157 | + ) |
| 158 | + assert len(lint.fixes) == 1 |
| 159 | + fix = lint.fixes[0] |
| 160 | + assert len(fix.edits) == 1 |
| 161 | + edit = fix.edits[0] |
| 162 | + assert edit.new_text == """\n- name: '"memory"."raw"."demographics"'\n""" |
| 163 | + assert edit.range == Range( |
| 164 | + start=Position(line=0, character=23), |
| 165 | + end=Position(line=0, character=23), |
| 166 | + ) |
| 167 | + fix_path = sushi_path / "external_models.yaml" |
| 168 | + assert edit.path == fix_path |
0 commit comments