Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Source Facebook Marketing: fix DATA_RETENTION_PERIOD validation and schema data type failed_delivery_checks issues #15012

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py"
ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]


LABEL io.airbyte.version=0.2.56
LABEL io.airbyte.version=0.2.57
LABEL io.airbyte.name=airbyte/source-facebook-marketing
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,21 @@
"type": ["null", "number"]
},
"failed_delivery_checks": {
"type": ["null", "number"]
"type": ["null", "array"],
"items": {
"type": ["null", "object"],
"properties": {
"summary": {
"type": ["null", "string"]
},
"description": {
"type": ["null", "string"]
},
"check_name": {
"type": ["null", "string"]
}
}
}
},
"fb_entity": {
"type": ["null", "number"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
#

from datetime import datetime
import pendulum


class FutureDateException(Exception):
def __init__(self, field_name, *args, **kwargs):
self.field_name = field_name
self.message = f"{self.field_name} cannot be in the future. Please set today's date or later"
# Facebook store metrics maximum of 37 months old. Any time range that
# older that 37 months from current date would result in 400 Bad request
# HTTP response.
# https://developers.facebook.com/docs/marketing-api/reference/ad-account/insights/#overview
DATA_RETENTION_PERIOD = pendulum.duration(months=37)


class ValidationDateException(Exception):
def __init__(self, message, *args, **kwargs):
self.message = message
super().__init__(self.message, *args, **kwargs)

def __str__(self):
Expand All @@ -18,7 +25,12 @@ def __repr__(self):
return self.__str__()


def validate_date_field(field, date):
if date.timestamp() > datetime.now().timestamp():
raise FutureDateException(field)
def validate_date_field(field_name: str, date: datetime) -> datetime:
pendulum_date = pendulum.instance(date)
if pendulum_date.timestamp() > pendulum.now().timestamp():
message = f"{field_name} cannot be in the future. Please set today's date or later."
raise ValidationDateException(message)
elif pendulum_date.timestamp() < (pendulum.now() - DATA_RETENTION_PERIOD).timestamp():
message = f"{field_name} cannot be beyond {DATA_RETENTION_PERIOD.months} months from the current date."
raise ValidationDateException(message)
return date
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#

#
import pytest
lazebnyi marked this conversation as resolved.
Show resolved Hide resolved
from datetime import datetime
from source_facebook_marketing.utils import ValidationDateException, validate_date_field


def test_validate_date_field():
field_name = "test_field_name"
date = datetime(2008, 1, 1)
with pytest.raises(ValidationDateException):
assert validate_date_field(field_name, date)

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#

import pytest
import pendulum
from source_facebook_marketing.utils import ValidationDateException, validate_date_field, DATA_RETENTION_PERIOD


@pytest.mark.parametrize(
"date, expected_massage, raise_error",
[(pendulum.now(),
"", False),
(pendulum.now() - pendulum.duration(months=DATA_RETENTION_PERIOD.months + 1),
f" cannot be beyond {DATA_RETENTION_PERIOD.months} months from the current date.", True),
(pendulum.now() + pendulum.duration(months=1),
f" cannot be in the future. Please set today's date or later.", True)],
ids=["valid_date",
f"date in the past by {DATA_RETENTION_PERIOD.months} months",
"date in future"]
lazebnyi marked this conversation as resolved.
Show resolved Hide resolved
)
def test_validate_date_field(date, expected_massage, raise_error):
lazebnyi marked this conversation as resolved.
Show resolved Hide resolved
field_name = "test_field_name"

if raise_error:
with pytest.raises(ValidationDateException) as error:
assert validate_date_field(field_name, date)
assert str(error.value) == field_name + expected_massage
lazebnyi marked this conversation as resolved.
Show resolved Hide resolved
else:
assert validate_date_field(field_name, date)
1 change: 1 addition & 0 deletions docs/integrations/sources/facebook-marketing.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Please be informed that the connector uses the `lookback_window` parameter to pe

| Version | Date | Pull Request | Subject |
|:--------|:-----------|:---------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 0.2.57 | 2022-07-25 | [15012](https://github.com/airbytehq/airbyte/pull/15012) | Add `DATA_RETENTION_PERIOD`validation and fix `failed_delivery_checks` field schema type issue |
| 0.2.56 | 2022-07-19 | [14831](https://github.com/airbytehq/airbyte/pull/14831) | Add future `start_date` and `end_date` validation |
| 0.2.55 | 2022-07-18 | [14786](https://github.com/airbytehq/airbyte/pull/14786) | Check if the authorized user has the "MANAGE" task permission when getting the `funding_source_details` field in the ad\_account stream |
| 0.2.54 | 2022-06-29 | [14267](https://github.com/airbytehq/airbyte/pull/14267) | Make MAX_BATCH_SIZE available in config |
Expand Down