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

unit test sendgrid messages stream #15331

Merged
merged 4 commits into from
Aug 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#

import unittest
from unittest.mock import MagicMock

import pendulum
import pytest
import requests
from airbyte_cdk.logger import AirbyteLogger
from source_sendgrid.source import SourceSendgrid
from source_sendgrid.streams import SendgridStream
from source_sendgrid.streams import Messages, SendgridStream

FAKE_NOW = pendulum.DateTime(2022, 1, 1, tzinfo=pendulum.timezone("utc"))


@pytest.fixture(name="sendgrid_stream")
Expand All @@ -19,6 +23,13 @@ def sendgrid_stream_fixture(mocker) -> SendgridStream:
return SendgridStream() # type: ignore


@pytest.fixture()
def mock_pendulum_now(monkeypatch):
pendulum_mock = unittest.mock.MagicMock(wraps=pendulum.now)
pendulum_mock.return_value = FAKE_NOW
monkeypatch.setattr(pendulum, "now", pendulum_mock)
Comment on lines +26 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to use FreezeGun for this kind of mock :D https://github.com/spulec/freezegun



def test_parse_response_gracefully_handles_nulls(mocker, sendgrid_stream: SendgridStream):
response = requests.Response()
mocker.patch.object(response, "json", return_value=None)
Expand All @@ -30,3 +41,14 @@ def test_source_wrong_credentials():
source = SourceSendgrid()
status, error = source.check_connection(logger=AirbyteLogger(), config={"apikey": "wrong.api.key123"})
assert not status


def test_messages_stream_request_params(mock_pendulum_now):
start_time = 1558359837
stream = Messages(start_time)
state = {"last_event_time": 1558359000}
request_params = stream.request_params(state)
assert (
request_params
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alafanechere do you know why this is returning a string instead of a Mapping[str, Any]?
If there's no obvious reason, I'd like to change it to a mapping so it's consistent with the interface

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't see obvious reason, the URL formatting could be a bit tricky due to the API query format, but I don't see any good reason to not use a mapping.

== "query=last_event_time%20BETWEEN%20TIMESTAMP%20%222019-05-20T06%3A30%3A00Z%22%20AND%20TIMESTAMP%20%222021-12-31T16%3A00%3A00Z%22&limit=1000"
)