|
1 | 1 | from sqlmesh import Context |
2 | | -from sqlmesh.core.linter.helpers import read_range_from_file, get_range_of_model_block |
| 2 | +from sqlmesh.core.linter.helpers import ( |
| 3 | + read_range_from_file, |
| 4 | + get_range_of_model_block, |
| 5 | + get_range_of_a_key_in_model_block, |
| 6 | +) |
3 | 7 | from sqlmesh.core.model import SqlModel |
4 | 8 |
|
5 | 9 |
|
@@ -34,3 +38,41 @@ def test_get_position_of_model_block(): |
34 | 38 | read_range = read_range_from_file(path, range) |
35 | 39 | assert read_range.startswith("MODEL") |
36 | 40 | assert read_range.endswith(";") |
| 41 | + |
| 42 | + |
| 43 | +def test_get_range_of_a_key_in_model_block_testing_on_sushi(): |
| 44 | + context = Context(paths=["examples/sushi"]) |
| 45 | + |
| 46 | + sql_models = [ |
| 47 | + model |
| 48 | + for model in context.models.values() |
| 49 | + if isinstance(model, SqlModel) |
| 50 | + and model._path is not None |
| 51 | + and str(model._path).endswith(".sql") |
| 52 | + ] |
| 53 | + assert len(sql_models) > 0 |
| 54 | + |
| 55 | + for model in sql_models: |
| 56 | + possible_keys = ["name", "tags", "description", "columns", "owner", "cron", "dialect"] |
| 57 | + |
| 58 | + dialect = model.dialect |
| 59 | + assert dialect is not None |
| 60 | + |
| 61 | + path = model._path |
| 62 | + assert path is not None |
| 63 | + |
| 64 | + with open(path, "r", encoding="utf-8") as file: |
| 65 | + content = file.read() |
| 66 | + |
| 67 | + count_properties_checked = 0 |
| 68 | + |
| 69 | + for key in possible_keys: |
| 70 | + range = get_range_of_a_key_in_model_block(content, dialect, key) |
| 71 | + |
| 72 | + # Check that the range starts with the key and ends with ; |
| 73 | + if range: |
| 74 | + read_range = read_range_from_file(path, range) |
| 75 | + assert read_range.lower() == key.lower() |
| 76 | + count_properties_checked += 1 |
| 77 | + |
| 78 | + assert count_properties_checked > 0 |
0 commit comments