1010from sqlmesh .core .model import (
1111 IncrementalByTimeRangeKind ,
1212 Model ,
13+ ModelKind ,
14+ ModelKindName ,
1315 SeedKind ,
1416 SqlModel ,
1517 create_seed_model ,
@@ -51,6 +53,20 @@ def model():
5153 )
5254
5355
56+ @pytest .fixture
57+ def full_refresh_model ():
58+ return SqlModel (
59+ name = "fr_model" ,
60+ kind = ModelKind (name = ModelKindName .FULL ),
61+ owner = "owner" ,
62+ dialect = "spark" ,
63+ cron = "1 0 * * *" ,
64+ batch_size = 30 ,
65+ start = "2020-01-01" ,
66+ query = parse_one ("SELECT @EACH([1, 2], x -> x), ds FROM parent.tbl" ),
67+ )
68+
69+
5470@pytest .fixture
5571def snapshot (
5672 model : Model ,
@@ -70,6 +86,25 @@ def snapshot(
7086 return snapshot
7187
7288
89+ @pytest .fixture
90+ def full_refresh_snapshot (
91+ full_refresh_model : Model ,
92+ parent_model : Model ,
93+ monkeypatch : MonkeyPatch ,
94+ mocker : MockerFixture ,
95+ make_snapshot ,
96+ ):
97+ mock = mocker .Mock ()
98+ mock .return_value = to_datetime ("2022-09-23T00:12:53+00:00" )
99+ monkeypatch .setattr ("sqlmesh.utils.date.now" , mock )
100+ full_refresh_snapshot = make_snapshot (
101+ full_refresh_model ,
102+ models = {parent_model .name : parent_model , full_refresh_model .name : full_refresh_model },
103+ )
104+ full_refresh_snapshot .version = full_refresh_snapshot .fingerprint
105+ return full_refresh_snapshot
106+
107+
73108def test_json (snapshot : Snapshot ):
74109 assert json .loads (snapshot .json ()) == {
75110 "created_ts" : 1663891973000 ,
@@ -151,11 +186,11 @@ def test_add_interval(snapshot: Snapshot, make_snapshot):
151186 ]
152187 snapshot .add_interval ("2019-01-01 00:00:00" , "2020-01-31 00:00:01" )
153188 assert snapshot .intervals == [
154- (to_timestamp ("2019-01-01" ), to_timestamp ("2020-02-01 " )),
189+ (to_timestamp ("2019-01-01" ), to_timestamp ("2020-01-31 " )),
155190 ]
156- snapshot .add_interval ("2018-12-31 23:59:59" , "2020-01-31 00 :00:01" )
191+ snapshot .add_interval ("2018-12-31 23:59:59" , "2020-01-31 12 :00:01" )
157192 assert snapshot .intervals == [
158- (to_timestamp ("2018-12-31" ), to_timestamp ("2020-02-01 " )),
193+ (to_timestamp ("2018-12-31" ), to_timestamp ("2020-01-31 " )),
159194 ]
160195
161196 new_snapshot = make_snapshot (snapshot .model )
@@ -207,6 +242,34 @@ def test_missing_intervals(snapshot: Snapshot):
207242 ]
208243
209244
245+ def test_missing_interval_latest (
246+ full_refresh_snapshot : Snapshot , monkeypatch : MonkeyPatch , mocker : MockerFixture
247+ ):
248+ mock = mocker .Mock ()
249+ mock .return_value = to_datetime ("2020-01-05T00:12:53+00:00" )
250+ monkeypatch .setattr ("sqlmesh.core.snapshot.definition.now" , mock )
251+ full_refresh_snapshot .add_interval ("2020-01-01" , "2020-01-01" )
252+ assert full_refresh_snapshot .missing_intervals ("2020-01-01" , "2020-01-01" , "2020-01-01" ) == []
253+ assert full_refresh_snapshot .missing_intervals ("2020-01-02" , "2020-01-02" , "2020-01-02" ) == [
254+ (to_timestamp ("2020-01-02" ), to_timestamp ("2020-01-03" ))
255+ ]
256+ assert full_refresh_snapshot .missing_intervals ("2020-01-02" , "2020-01-03" , "2020-01-03" ) == [
257+ (to_timestamp ("2020-01-03" ), to_timestamp ("2020-01-04" ))
258+ ]
259+ assert full_refresh_snapshot .missing_intervals ("2020-01-02" , "2020-01-03" , "2020-01-04" ) == [
260+ (to_timestamp ("2020-01-04" ), to_timestamp ("2020-01-05" ))
261+ ]
262+ assert full_refresh_snapshot .missing_intervals (
263+ "2020-01-02" , "2020-01-03" , "2020-01-03 01:00:00"
264+ ) == [(to_timestamp ("2020-01-02" ), to_timestamp ("2020-01-03" ))]
265+ assert full_refresh_snapshot .missing_intervals (
266+ "2020-01-02" , "2020-01-03" , "2020-01-04 23:59:59"
267+ ) == [(to_timestamp ("2020-01-03" ), to_timestamp ("2020-01-04" ))]
268+ assert full_refresh_snapshot .missing_intervals ("2020-01-02" , "2020-01-03" ) == [
269+ (to_timestamp ("2020-01-04" ), to_timestamp ("2020-01-05" ))
270+ ]
271+
272+
210273def test_remove_intervals (snapshot : Snapshot ):
211274 snapshot .add_interval ("2020-01-01" , "2020-01-01" )
212275 snapshot .remove_interval ("2020-01-01" , "2020-01-01" )
0 commit comments