|
6 | 6 |
|
7 | 7 | import pendulum
|
8 | 8 | import pytest
|
| 9 | +import source_facebook_marketing.streams.base_insight_streams |
9 | 10 | from airbyte_cdk.models import SyncMode
|
| 11 | +from helpers import FakeInsightAsyncJob, FakeInsightAsyncJobManager, read_full_refresh, read_incremental |
10 | 12 | from pendulum import duration
|
11 | 13 | from source_facebook_marketing.streams import AdsInsights
|
12 | 14 | from source_facebook_marketing.streams.async_job import AsyncJob, InsightAsyncJob
|
@@ -186,7 +188,7 @@ def test_stream_slices_no_state_close_to_now(self, api, async_manager_mock, rece
|
186 | 188 | async_manager_mock.assert_called_once()
|
187 | 189 | args, kwargs = async_manager_mock.call_args
|
188 | 190 | generated_jobs = list(kwargs["jobs"])
|
189 |
| - assert len(generated_jobs) == (end_date - start_date).days + 1 |
| 191 | + assert len(generated_jobs) == (end_date - start_date).days |
190 | 192 | assert generated_jobs[0].interval.start == start_date.date()
|
191 | 193 | assert generated_jobs[1].interval.start == start_date.date() + duration(days=1)
|
192 | 194 |
|
@@ -223,7 +225,7 @@ def test_stream_slices_with_state_close_to_now(self, api, async_manager_mock, re
|
223 | 225 | async_manager_mock.assert_called_once()
|
224 | 226 | args, kwargs = async_manager_mock.call_args
|
225 | 227 | generated_jobs = list(kwargs["jobs"])
|
226 |
| - assert len(generated_jobs) == (end_date - start_date).days + 1 |
| 228 | + assert len(generated_jobs) == (end_date - start_date).days |
227 | 229 | assert generated_jobs[0].interval.start == start_date.date()
|
228 | 230 | assert generated_jobs[1].interval.start == start_date.date() + duration(days=1)
|
229 | 231 |
|
@@ -290,3 +292,72 @@ def test_fields_custom(self, api):
|
290 | 292 | )
|
291 | 293 |
|
292 | 294 | assert stream.fields == ["account_id", "account_currency"]
|
| 295 | + |
| 296 | + def test_completed_slices_in_lookback_period(self, api, monkeypatch, set_today): |
| 297 | + start_date = pendulum.parse("2020-03-01") |
| 298 | + end_date = pendulum.parse("2020-05-01") |
| 299 | + set_today("2020-04-01") |
| 300 | + monkeypatch.setattr(AdsInsights, "INSIGHTS_LOOKBACK_PERIOD", pendulum.duration(days=10)) |
| 301 | + monkeypatch.setattr(source_facebook_marketing.streams.base_insight_streams, "InsightAsyncJob", FakeInsightAsyncJob) |
| 302 | + monkeypatch.setattr(source_facebook_marketing.streams.base_insight_streams, "InsightAsyncJobManager", FakeInsightAsyncJobManager) |
| 303 | + |
| 304 | + state = { |
| 305 | + AdsInsights.cursor_field: "2020-03-19", |
| 306 | + "slices": [ |
| 307 | + "2020-03-21", |
| 308 | + "2020-03-22", |
| 309 | + "2020-03-23", |
| 310 | + ], |
| 311 | + "time_increment": 1, |
| 312 | + } |
| 313 | + |
| 314 | + stream = AdsInsights(api=api, start_date=start_date, end_date=end_date) |
| 315 | + stream.state = state |
| 316 | + assert stream._completed_slices == {pendulum.Date(2020, 3, 21), pendulum.Date(2020, 3, 22), pendulum.Date(2020, 3, 23)} |
| 317 | + |
| 318 | + slices = stream.stream_slices(stream_state=state, sync_mode=SyncMode.incremental) |
| 319 | + slices = [x["insight_job"].interval.start for x in slices] |
| 320 | + |
| 321 | + assert pendulum.parse("2020-03-21").date() not in slices |
| 322 | + assert pendulum.parse("2020-03-22").date() in slices |
| 323 | + assert pendulum.parse("2020-03-23").date() in slices |
| 324 | + assert stream._completed_slices == {pendulum.Date(2020, 3, 21)} |
| 325 | + |
| 326 | + def test_incremental_lookback_period_updated(self, api, monkeypatch, set_today): |
| 327 | + start_date = pendulum.parse("2020-03-01") |
| 328 | + end_date = pendulum.parse("2020-05-01") |
| 329 | + yesterday, _ = set_today("2020-04-01") |
| 330 | + monkeypatch.setattr(AdsInsights, "INSIGHTS_LOOKBACK_PERIOD", pendulum.duration(days=20)) |
| 331 | + monkeypatch.setattr(source_facebook_marketing.streams.base_insight_streams, "InsightAsyncJob", FakeInsightAsyncJob) |
| 332 | + monkeypatch.setattr(source_facebook_marketing.streams.base_insight_streams, "InsightAsyncJobManager", FakeInsightAsyncJobManager) |
| 333 | + |
| 334 | + stream = AdsInsights(api=api, start_date=start_date, end_date=end_date) |
| 335 | + |
| 336 | + records = read_full_refresh(stream) |
| 337 | + assert len(records) == (yesterday - start_date).days + 1 |
| 338 | + assert records[0]["date_start"] == str(start_date.date()) |
| 339 | + assert records[-1]["date_start"] == str(yesterday.date()) |
| 340 | + |
| 341 | + state = {AdsInsights.cursor_field: "2020-03-20", "time_increment": 1} |
| 342 | + records = read_incremental(stream, state) |
| 343 | + assert len(records) == (yesterday - pendulum.parse("2020-03-20")).days |
| 344 | + assert records[0]["date_start"] == "2020-03-21" |
| 345 | + assert records[-1]["date_start"] == str(yesterday.date()) |
| 346 | + assert state == {"date_start": str(yesterday.date()), "slices": [], "time_increment": 1} |
| 347 | + |
| 348 | + yesterday, _ = set_today("2020-04-02") |
| 349 | + records = read_incremental(stream, state) |
| 350 | + assert records == [{"date_start": str(yesterday.date()), "updated_time": str(yesterday.date())}] |
| 351 | + assert state == {"date_start": str(yesterday.date()), "slices": [], "time_increment": 1} |
| 352 | + |
| 353 | + yesterday, _ = set_today("2020-04-03") |
| 354 | + FakeInsightAsyncJob.update_insight("2020-03-26", "2020-04-01") |
| 355 | + FakeInsightAsyncJob.update_insight("2020-03-27", "2020-04-02") |
| 356 | + FakeInsightAsyncJob.update_insight("2020-03-28", "2020-04-03") |
| 357 | + |
| 358 | + records = read_incremental(stream, state) |
| 359 | + assert records == [ |
| 360 | + {"date_start": "2020-03-27", "updated_time": "2020-04-02"}, |
| 361 | + {"date_start": "2020-04-02", "updated_time": "2020-04-02"}, |
| 362 | + ] |
| 363 | + assert state == {"date_start": str(yesterday.date()), "slices": [], "time_increment": 1} |
0 commit comments