Skip to content

Commit e1933d0

Browse files
committed
Add unit tests to test request of getting identity buckets
1 parent 3d31c4d commit e1933d0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

examples/sample_get_identity_buckets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# this sample client takes timestamp string as input and generates an IdentityBucketsResponse object which contains
88
# a list of buckets, the timestamp string in the format YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]],
9-
# for example: UTC: 2024-07-02, 2024-07-02T14:30:15.123456+00:00 and EST: 2024-07-02T14:30:15.123456-05:00
9+
# for example: local timezone: 2024-07-02, UTC: 2024-07-02T14:30:15.123456+00:00, EST: 2024-07-02T14:30:15.123456-05:00
1010

1111
def _usage():
1212
print('Usage: python3 sample_get_identity_buckets.py <base_url> <api_key> <client_secret> <timestamp>'

tests/test_identity_map_client_unit_tests.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import json
12
import unittest
23
import datetime as dt
4+
from unittest.mock import patch, MagicMock
35

46
from uid2_client import IdentityMapClient, get_datetime_utc_iso_format
57

68

79
class IdentityMapUnitTests(unittest.TestCase):
8-
identity_map_client = IdentityMapClient("UID2_BASE_URL", "UID2_API_KEY", "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=")
10+
UID2_SECRET_KEY = "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg="
11+
identity_map_client = IdentityMapClient("UID2_BASE_URL", "UID2_API_KEY", UID2_SECRET_KEY)
912

1013
def test_identity_buckets_invalid_timestamp(self):
1114
test_cases = ["1234567890",
@@ -30,3 +33,25 @@ def test_get_datetime_utc_iso_format_timestamp(self):
3033
iso_format_timestamp = get_datetime_utc_iso_format(timestamp)
3134
self.assertEqual(expected_timestamp, iso_format_timestamp)
3235

36+
@patch('uid2_client.identity_map_client.make_v2_request')
37+
@patch('uid2_client.identity_map_client.post')
38+
@patch('uid2_client.identity_map_client.parse_v2_response')
39+
def test_identity_buckets_request(self, mock_parse_v2_response, mock_post, mock_make_v2_request):
40+
expected_timestamp = "2024-07-02T14:30:15.123456+00:00"
41+
expected_req = json.dumps({"since_timestamp": get_datetime_utc_iso_format(dt.datetime.fromisoformat(expected_timestamp))}).encode()
42+
test_cases = ["2024-07-02T14:30:15.123456+00:00", "2024-07-02 09:30:15.123456-05:00",
43+
"2024-07-02T08:30:15.123456-06:00", "2024-07-02T10:30:15.123456-04:00",
44+
"2024-07-02T06:30:15.123456-08:00", "2024-07-02T23:30:15.123456+09:00",
45+
"2024-07-03T00:30:15.123456+10:00", "2024-07-02T20:00:15.123456+05:30"]
46+
mock_req = b'mocked_request_data'
47+
mock_nonce = 'mocked_nonce'
48+
mock_make_v2_request.return_value = (mock_req, mock_nonce)
49+
mock_response = MagicMock()
50+
mock_response.read.return_value = b'{"mocked": "response"}'
51+
mock_post.return_value = mock_response
52+
mock_parse_v2_response.return_value = b'{"body":[],"status":"success"}'
53+
print(expected_req)
54+
for timestamp in test_cases:
55+
self.identity_map_client.get_identity_buckets(dt.datetime.fromisoformat(timestamp))
56+
called_args, called_kwargs = mock_make_v2_request.call_args
57+
self.assertEqual(expected_req, called_args[2])

0 commit comments

Comments
 (0)