Skip to content

Commit

Permalink
Source OneSignal: correct get_update_state and unit test (#7617)
Browse files Browse the repository at this point in the history
* correct get_update_state and unit test

* bunmp version
  • Loading branch information
marcosmarxm committed Nov 11, 2021
1 parent d3ba217 commit 760f429
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 42 deletions.
Expand Up @@ -2,6 +2,6 @@
"sourceDefinitionId": "bb6afd81-87d5-47e3-97c4-e2c2901b1cf8",
"name": "OneSignal",
"dockerRepository": "airbyte/source-onesignal",
"dockerImageTag": "0.1.0",
"dockerImageTag": "0.1.1",
"documentationUrl": "https://docs.airbyte.io/integrations/sources/onesignal"
}
Expand Up @@ -373,7 +373,7 @@
- name: OneSignal
sourceDefinitionId: bb6afd81-87d5-47e3-97c4-e2c2901b1cf8
dockerRepository: airbyte/source-onesignal
dockerImageTag: 0.1.0
dockerImageTag: 0.1.1
documentationUrl: https://docs.airbyte.io/integrations/sources/lever-onesignal
sourceType: api
- name: Oracle DB
Expand Down
Expand Up @@ -4021,7 +4021,7 @@
supportsNormalization: false
supportsDBT: false
supported_destination_sync_modes: []
- dockerImage: "airbyte/source-onesignal:0.1.0"
- dockerImage: "airbyte/source-onesignal:0.1.1"
spec:
documentationUrl: "https://docs.airbyte.io/integrations/sources/onesignal"
connectionSpecification:
Expand Down
@@ -1,6 +1,7 @@
#
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
#

import json
from abc import ABC, abstractmethod
from dataclasses import dataclass
Expand Down
Expand Up @@ -34,5 +34,5 @@ COPY source_onesignal ./source_onesignal
ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py"
ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]

LABEL io.airbyte.version=0.1.0
LABEL io.airbyte.version=0.1.1
LABEL io.airbyte.name=airbyte/source-onesignal
Expand Up @@ -4,10 +4,9 @@

import time
from abc import ABC
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional, TypeVar
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional

import pendulum
import pydantic
import requests
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources.streams.http import HttpStream, HttpSubStream
Expand Down Expand Up @@ -76,28 +75,6 @@ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapp
yield from data


T = TypeVar("T")


class StateValueWrapper(pydantic.BaseModel):
stream: T
state_value: int
max_cursor_time = 0

def __repr__(self):
"""Overrides print view"""
return str(self.value)

@property
def value(self):
"""Return max cursor time after stream sync is finished."""
return self.max_cursor_time if self.stream.is_finished else self.state_value

def dict(self, **kwargs):
"""Overrides default logic to return current value only."""
return {pydantic.utils.ROOT_KEY: self.value}


class ChildStreamMixin(HttpSubStream):

is_finished = False
Expand Down Expand Up @@ -176,14 +153,11 @@ def get_updated_state(
current_stream_state: MutableMapping[str, Any],
latest_record: Mapping[str, Any],
) -> Mapping[str, Any]:
state_value = (current_stream_state or {}).get(self.cursor_field, 0)
if not isinstance(state_value, StateValueWrapper):
state_value = StateValueWrapper(stream=self, state_value=state_value)

record_time = latest_record.get(self.cursor_field, self.start_date)
state_value.max_cursor_time = max(state_value.max_cursor_time, record_time)
current_stream_state = current_stream_state or {}
current_stream_state_date = current_stream_state.get(self.cursor_field, self.start_date)
latest_record_date = latest_record.get(self.cursor_field, self.start_date)

return {self.cursor_field: state_value}
return {self.cursor_field: max(current_stream_state_date, latest_record_date)}


class Apps(OnesignalStream):
Expand Down
Expand Up @@ -41,21 +41,21 @@ def test_cursor_field(stream):

def test_get_updated_state(stream):
inputs = {"current_stream_state": {"updated_at": 42}, "latest_record": {"updated_at": 90}}
expected_state = 42
expected_state = 90
state = stream.get_updated_state(**inputs)
assert state["updated_at"].value == expected_state
assert state["updated_at"] == expected_state

inputs = {"current_stream_state": state, "latest_record": {"updated_at": 100}}
expected_state = 42
expected_state = 100
state = stream.get_updated_state(**inputs)
assert state["updated_at"].value == expected_state
assert state["updated_at"] == expected_state

# after stream sync is finished, state should output the max cursor time
stream.is_finished = True
inputs = {"current_stream_state": state, "latest_record": {"updated_at": 80}}
expected_state = 100
state = stream.get_updated_state(**inputs)
assert state["updated_at"].value == expected_state
assert state["updated_at"] == expected_state


def test_stream_slices(stream, requests_mock):
Expand Down Expand Up @@ -94,9 +94,9 @@ def test_end_of_stream_state(parent, args, requests_mock):
for record in stream.read_records(sync_mode, stream_slice=app_slice):
state = stream.get_updated_state(state, record)
if idx == 2: # the last slice
assert state["queued_at"].value == 90
assert state["queued_at"] == 90
else:
assert state["queued_at"].value == 50
assert state["queued_at"] == 90


def test_supports_incremental(patch_incremental_base_class, mocker, parent, args):
Expand Down
1 change: 1 addition & 0 deletions docs/integrations/sources/onesignal.md
Expand Up @@ -54,5 +54,6 @@ Please register on OneSignal and follow this [docs](https://documentation.onesig

| Version | Date | Pull Request | Subject |
| :--- | :--- | :--- | :--- |
| 0.1.1 | 2021-11-10 | [7617](https://github.com/airbytehq/airbyte/pull/7617) | Fix get_update state |
| 0.1.0 | 2021-10-13 | [6998](https://github.com/airbytehq/airbyte/pull/6998) | Initial Release |

0 comments on commit 760f429

Please sign in to comment.