Skip to content

Commit ada2040

Browse files
committed
fix: snapshot test due to metadata changes
1 parent c83c99b commit ada2040

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

tests/core/test_snapshot.py

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def test_json(snapshot: Snapshot):
165165
"enabled": True,
166166
"extract_dependencies_from_query": True,
167167
"virtual_environment_mode": "full",
168+
"grants_target_layer": "all",
168169
},
169170
"name": '"name"',
170171
"parents": [{"name": '"parent"."tbl"', "identifier": snapshot.parents[0].identifier}],
@@ -178,6 +179,36 @@ def test_json(snapshot: Snapshot):
178179
}
179180

180181

182+
def test_json_with_grants(make_snapshot: t.Callable):
183+
from sqlmesh.core.model.meta import GrantsTargetLayer
184+
185+
model = SqlModel(
186+
name="name",
187+
kind=dict(time_column="ds", batch_size=30, name=ModelKindName.INCREMENTAL_BY_TIME_RANGE),
188+
owner="owner",
189+
dialect="spark",
190+
cron="1 0 * * *",
191+
start="2020-01-01",
192+
query=parse_one("SELECT @EACH([1, 2], x -> x), ds FROM parent.tbl"),
193+
grants={"SELECT": ["role1", "role2"], "INSERT": ["role3"]},
194+
grants_target_layer=GrantsTargetLayer.VIRTUAL,
195+
)
196+
snapshot = make_snapshot(model)
197+
198+
json_str = snapshot.json()
199+
json_data = json.loads(json_str)
200+
assert (
201+
json_data["node"]["grants"]
202+
== "('SELECT' = ARRAY('role1', 'role2'), 'INSERT' = ARRAY('role3'))"
203+
)
204+
assert json_data["node"]["grants_target_layer"] == "virtual"
205+
206+
reparsed_snapshot = Snapshot.model_validate_json(json_str)
207+
assert isinstance(reparsed_snapshot.node, SqlModel)
208+
assert reparsed_snapshot.node.grants == {"SELECT": ["role1", "role2"], "INSERT": ["role3"]}
209+
assert reparsed_snapshot.node.grants_target_layer == GrantsTargetLayer.VIRTUAL
210+
211+
181212
def test_json_custom_materialization(make_snapshot: t.Callable):
182213
model = SqlModel(
183214
name="name",
@@ -914,7 +945,7 @@ def test_fingerprint(model: Model, parent_model: Model):
914945

915946
original_fingerprint = SnapshotFingerprint(
916947
data_hash="3301649319",
917-
metadata_hash="3575333731",
948+
metadata_hash="4020678062",
918949
)
919950

920951
assert fingerprint == original_fingerprint
@@ -975,7 +1006,7 @@ def test_fingerprint_seed_model():
9751006

9761007
expected_fingerprint = SnapshotFingerprint(
9771008
data_hash="1586624913",
978-
metadata_hash="2315134974",
1009+
metadata_hash="1817881990",
9791010
)
9801011

9811012
model = load_sql_based_model(expressions, path=Path("./examples/sushi/models/test_model.sql"))
@@ -1014,7 +1045,7 @@ def test_fingerprint_jinja_macros(model: Model):
10141045
)
10151046
original_fingerprint = SnapshotFingerprint(
10161047
data_hash="2908339239",
1017-
metadata_hash="3575333731",
1048+
metadata_hash="4020678062",
10181049
)
10191050

10201051
fingerprint = fingerprint_from_node(model, nodes={})
@@ -1089,6 +1120,40 @@ def test_fingerprint_virtual_properties(model: Model, parent_model: Model):
10891120
assert updated_fingerprint.data_hash == fingerprint.data_hash
10901121

10911122

1123+
def test_fingerprint_grants(model: Model, parent_model: Model):
1124+
from sqlmesh.core.model.meta import GrantsTargetLayer
1125+
1126+
original_model = deepcopy(model)
1127+
fingerprint = fingerprint_from_node(model, nodes={})
1128+
1129+
updated_model = SqlModel(
1130+
**original_model.dict(),
1131+
grants={"SELECT": ["role1", "role2"]},
1132+
)
1133+
updated_fingerprint = fingerprint_from_node(updated_model, nodes={})
1134+
1135+
assert updated_fingerprint != fingerprint
1136+
assert updated_fingerprint.metadata_hash != fingerprint.metadata_hash
1137+
assert updated_fingerprint.data_hash == fingerprint.data_hash
1138+
1139+
different_grants_model = SqlModel(
1140+
**original_model.dict(),
1141+
grants={"SELECT": ["role3"], "INSERT": ["role4"]},
1142+
)
1143+
different_grants_fingerprint = fingerprint_from_node(different_grants_model, nodes={})
1144+
1145+
assert different_grants_fingerprint.metadata_hash != updated_fingerprint.metadata_hash
1146+
assert different_grants_fingerprint.metadata_hash != fingerprint.metadata_hash
1147+
1148+
target_layer_model = SqlModel(
1149+
**{**original_model.dict(), "grants_target_layer": GrantsTargetLayer.PHYSICAL},
1150+
grants={"SELECT": ["role1", "role2"]},
1151+
)
1152+
target_layer_fingerprint = fingerprint_from_node(target_layer_model, nodes={})
1153+
1154+
assert target_layer_fingerprint.metadata_hash != updated_fingerprint.metadata_hash
1155+
1156+
10921157
def test_tableinfo_equality():
10931158
snapshot_a = SnapshotTableInfo(
10941159
name="test_schema.a",

0 commit comments

Comments
 (0)