Skip to content

Commit

Permalink
Added get_restricted_start_date function to restrict start_date to no…
Browse files Browse the repository at this point in the history
… more than 90 days from current date. Added corresponding unittest. (singer-io#30)

Co-authored-by: pbegle <[email protected]>
  • Loading branch information
0xpetersatoshi and pbegle authored Jul 28, 2021
1 parent 014a281 commit 0dcfced
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions tap_appsflyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ def af_datetime_str_to_datetime(s):
return datetime.datetime.strptime(s.strip(), "%Y-%m-%d %H:%M:%S")


def get_restricted_start_date(date: str) -> datetime.datetime:
# https://support.appsflyer.com/hc/en-us/articles/207034366-API-Policy
restriction_date = datetime.datetime.now() - datetime.timedelta(days=90)
start_date = utils.strptime(date)

return max(start_date, restriction_date)


def get_start(key):
if key in STATE:
return utils.strptime(STATE[key])
return get_restricted_start_date(STATE[key])

if "start_date" in CONFIG:
return utils.strptime(CONFIG["start_date"])
return get_restricted_start_date(CONFIG["start_date"])

return datetime.datetime.now() - datetime.timedelta(days=30)

Expand Down
24 changes: 24 additions & 0 deletions tests/unittests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import datetime
from unittest.mock import patch

from tap_appsflyer import get_restricted_start_date

MOCKED_DATE = datetime.datetime(2021, 7, 1, 1, 1, 1, 369251)

class MockedDatetime(datetime.datetime):
def now():
return MOCKED_DATE

@patch('datetime.datetime', MockedDatetime)
def test_get_restricted_start_date():
test_cases = [
{'case': '2018-01-01T00:00:00Z', 'expected': datetime.datetime(2021, 4, 2, 1, 1, 1, 369251)},
{'case': '2021-07-27T00:00:00Z', 'expected': datetime.datetime(2021, 7, 27, 0, 0)},
{'case': '2021-04-02T00:00:00Z', 'expected': datetime.datetime(2021, 4, 2, 1, 1, 1, 369251)},
{'case': '2021-05-23T12:34:56Z', 'expected': datetime.datetime(2021, 5, 23, 12, 34, 56)},
]

for test_case in test_cases:
date = get_restricted_start_date(test_case['case'])

assert date == test_case['expected']

0 comments on commit 0dcfced

Please sign in to comment.