forked from airbytehq/airbyte
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨Source Facebook Marketing: add integration tests (airbytehq#35061)
- Loading branch information
1 parent
301d597
commit 41e849e
Showing
23 changed files
with
1,485 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 25 additions & 25 deletions
50
...ntegrations/connectors/source-facebook-marketing/integration_tests/expected_records.jsonl
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
airbyte-integrations/connectors/source-facebook-marketing/poetry.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
55 changes: 55 additions & 0 deletions
55
airbyte-integrations/connectors/source-facebook-marketing/unit_tests/integration/config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
from typing import Any, List, MutableMapping | ||
|
||
import pendulum | ||
|
||
ACCESS_TOKEN = "test_access_token" | ||
ACCOUNT_ID = "111111111111111" | ||
CLIENT_ID = "test_client_id" | ||
CLIENT_SECRET = "test_client_secret" | ||
DATE_FORMAT = "%Y-%m-%d" | ||
DATE_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" | ||
END_DATE = "2023-01-01T23:59:59Z" | ||
NOW = pendulum.now(tz="utc") | ||
START_DATE = "2023-01-01T00:00:00Z" | ||
|
||
|
||
class ConfigBuilder: | ||
def __init__(self) -> None: | ||
self._config: MutableMapping[str, Any] = { | ||
"account_ids": [ACCOUNT_ID], | ||
"access_token": ACCESS_TOKEN, | ||
"start_date": START_DATE, | ||
"end_date": END_DATE, | ||
"include_deleted": True, | ||
"fetch_thumbnail_images": True, | ||
"custom_insights": [], | ||
"page_size": 100, | ||
"insights_lookback_window": 28, | ||
"insights_job_timeout": 60, | ||
"action_breakdowns_allow_empty": True, | ||
"client_id": CLIENT_ID, | ||
"client_secret": CLIENT_SECRET, | ||
} | ||
|
||
def with_account_ids(self, account_ids: List[str]) -> ConfigBuilder: | ||
self._config["account_ids"] = account_ids | ||
return self | ||
|
||
def with_start_date(self, start_date: datetime) -> ConfigBuilder: | ||
self._config["start_date"] = start_date.strftime(DATE_TIME_FORMAT) | ||
return self | ||
|
||
def with_end_date(self, end_date: datetime) -> ConfigBuilder: | ||
self._config["end_date"] = end_date.strftime(DATE_TIME_FORMAT) | ||
return self | ||
|
||
def build(self) -> MutableMapping[str, Any]: | ||
return self._config |
24 changes: 24 additions & 0 deletions
24
...te-integrations/connectors/source-facebook-marketing/unit_tests/integration/pagination.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
from typing import Any, Dict | ||
from urllib.parse import urlunparse | ||
|
||
from airbyte_cdk.test.mock_http.request import HttpRequest | ||
from airbyte_cdk.test.mock_http.response_builder import PaginationStrategy | ||
|
||
NEXT_PAGE_TOKEN = "QVFIUlhOX3Rnbm5Y" | ||
|
||
|
||
class FacebookMarketingPaginationStrategy(PaginationStrategy): | ||
def __init__(self, request: HttpRequest, next_page_token: str) -> None: | ||
self._next_page_token = next_page_token | ||
self._next_page_url = f"{urlunparse(request._parsed_url)}&after={self._next_page_token}" | ||
|
||
def update(self, response: Dict[str, Any]) -> None: | ||
# set a constant value for paging.cursors.after so we know how the 'next' link is built | ||
# https://developers.facebook.com/docs/graph-api/results | ||
response["paging"]["cursors"]["after"] = self._next_page_token | ||
response["paging"]["next"] = self._next_page_url |
83 changes: 83 additions & 0 deletions
83
...tegrations/connectors/source-facebook-marketing/unit_tests/integration/request_builder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
from __future__ import annotations | ||
|
||
from typing import Any, List, Mapping, Optional, Union | ||
|
||
from airbyte_cdk.test.mock_http.request import HttpRequest | ||
|
||
from .config import ACCESS_TOKEN, ACCOUNT_ID | ||
|
||
|
||
def get_account_request(account_id: Optional[str] = ACCOUNT_ID) -> RequestBuilder: | ||
return RequestBuilder.get_account_endpoint(access_token=ACCESS_TOKEN, account_id=account_id) | ||
|
||
|
||
class RequestBuilder: | ||
|
||
@classmethod | ||
def get_account_endpoint(cls, access_token: str, account_id: str) -> RequestBuilder: | ||
return cls(access_token=access_token).with_account_id(account_id) | ||
|
||
@classmethod | ||
def get_videos_endpoint(cls, access_token: str, account_id: str) -> RequestBuilder: | ||
return cls(access_token=access_token, resource="advideos").with_account_id(account_id) | ||
|
||
@classmethod | ||
def get_insights_endpoint(cls, access_token: str, account_id: str) -> RequestBuilder: | ||
return cls(access_token=access_token, resource="insights").with_account_id(account_id) | ||
|
||
@classmethod | ||
def get_execute_batch_endpoint(cls, access_token: str) -> RequestBuilder: | ||
return cls(access_token=access_token) | ||
|
||
@classmethod | ||
def get_insights_download_endpoint(cls, access_token: str, job_id: str) -> RequestBuilder: | ||
return cls(access_token=access_token, resource=f"{job_id}/insights") | ||
|
||
def __init__(self, access_token: str, resource: Optional[str] = "") -> None: | ||
self._account_id = None | ||
self._resource = resource | ||
self._query_params = {"access_token": access_token} | ||
self._body = None | ||
|
||
def with_account_id(self, account_id: str) -> RequestBuilder: | ||
self._account_id = account_id | ||
return self | ||
|
||
def with_limit(self, limit: int) -> RequestBuilder: | ||
self._query_params["limit"] = limit | ||
return self | ||
|
||
def with_summary(self) -> RequestBuilder: | ||
self._query_params["summary"] = "true" | ||
return self | ||
|
||
def with_fields(self, fields: List[str]) -> RequestBuilder: | ||
self._query_params["fields"] = self._get_formatted_fields(fields) | ||
return self | ||
|
||
def with_next_page_token(self, next_page_token: str) -> RequestBuilder: | ||
self._query_params["after"] = next_page_token | ||
return self | ||
|
||
def with_body(self, body: Union[str, bytes, Mapping[str, Any]]) -> RequestBuilder: | ||
self._body = body | ||
return self | ||
|
||
def build(self) -> HttpRequest: | ||
return HttpRequest( | ||
url=f"https://graph.facebook.com/v17.0/{self._account_sub_path()}{self._resource}", | ||
query_params=self._query_params, | ||
body=self._body, | ||
) | ||
|
||
def _account_sub_path(self) -> str: | ||
return f"act_{self._account_id}/" if self._account_id else "" | ||
|
||
@staticmethod | ||
def _get_formatted_fields(fields: List[str]) -> str: | ||
return ",".join(fields) |
33 changes: 33 additions & 0 deletions
33
...egrations/connectors/source-facebook-marketing/unit_tests/integration/response_builder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
import json | ||
from http import HTTPStatus | ||
from typing import Any, List, Mapping, Optional, Union | ||
|
||
from airbyte_cdk.test.mock_http import HttpResponse | ||
|
||
from .config import ACCOUNT_ID | ||
|
||
|
||
def build_response( | ||
body: Union[Mapping[str, Any], List[Mapping[str, Any]]], | ||
status_code: HTTPStatus, | ||
headers: Optional[Mapping[str, str]] = None, | ||
) -> HttpResponse: | ||
headers = headers or {} | ||
return HttpResponse(body=json.dumps(body), status_code=status_code.value, headers=headers) | ||
|
||
|
||
def get_account_response(account_id: Optional[str] = ACCOUNT_ID) -> HttpResponse: | ||
response = {"account_id": account_id, "id": f"act_{account_id}"} | ||
return build_response(body=response, status_code=HTTPStatus.OK) | ||
|
||
|
||
def error_reduce_amount_of_data_response() -> HttpResponse: | ||
response = { | ||
"error": {"code": 1, "message": "Please reduce the amount of data you're asking for, then retry your request"}, | ||
} | ||
return build_response(body=response, status_code=HTTPStatus.INTERNAL_SERVER_ERROR) |
Oops, something went wrong.