From e1933d04042b984fb33598ed4d1db0ca31df63fc Mon Sep 17 00:00:00 2001 From: Caroline6312 Date: Wed, 10 Jul 2024 11:27:50 -0700 Subject: [PATCH 1/2] Add unit tests to test request of getting identity buckets --- examples/sample_get_identity_buckets.py | 2 +- tests/test_identity_map_client_unit_tests.py | 27 +++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/examples/sample_get_identity_buckets.py b/examples/sample_get_identity_buckets.py index 1d42820..b0f29f6 100644 --- a/examples/sample_get_identity_buckets.py +++ b/examples/sample_get_identity_buckets.py @@ -6,7 +6,7 @@ # this sample client takes timestamp string as input and generates an IdentityBucketsResponse object which contains # a list of buckets, the timestamp string in the format YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]], -# for example: UTC: 2024-07-02, 2024-07-02T14:30:15.123456+00:00 and EST: 2024-07-02T14:30:15.123456-05:00 +# 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 def _usage(): print('Usage: python3 sample_get_identity_buckets.py ' diff --git a/tests/test_identity_map_client_unit_tests.py b/tests/test_identity_map_client_unit_tests.py index 7a928e3..6ce3083 100644 --- a/tests/test_identity_map_client_unit_tests.py +++ b/tests/test_identity_map_client_unit_tests.py @@ -1,11 +1,14 @@ +import json import unittest import datetime as dt +from unittest.mock import patch, MagicMock from uid2_client import IdentityMapClient, get_datetime_utc_iso_format class IdentityMapUnitTests(unittest.TestCase): - identity_map_client = IdentityMapClient("UID2_BASE_URL", "UID2_API_KEY", "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=") + UID2_SECRET_KEY = "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=" + identity_map_client = IdentityMapClient("UID2_BASE_URL", "UID2_API_KEY", UID2_SECRET_KEY) def test_identity_buckets_invalid_timestamp(self): test_cases = ["1234567890", @@ -30,3 +33,25 @@ def test_get_datetime_utc_iso_format_timestamp(self): iso_format_timestamp = get_datetime_utc_iso_format(timestamp) self.assertEqual(expected_timestamp, iso_format_timestamp) + @patch('uid2_client.identity_map_client.make_v2_request') + @patch('uid2_client.identity_map_client.post') + @patch('uid2_client.identity_map_client.parse_v2_response') + def test_identity_buckets_request(self, mock_parse_v2_response, mock_post, mock_make_v2_request): + expected_timestamp = "2024-07-02T14:30:15.123456+00:00" + expected_req = json.dumps({"since_timestamp": get_datetime_utc_iso_format(dt.datetime.fromisoformat(expected_timestamp))}).encode() + test_cases = ["2024-07-02T14:30:15.123456+00:00", "2024-07-02 09:30:15.123456-05:00", + "2024-07-02T08:30:15.123456-06:00", "2024-07-02T10:30:15.123456-04:00", + "2024-07-02T06:30:15.123456-08:00", "2024-07-02T23:30:15.123456+09:00", + "2024-07-03T00:30:15.123456+10:00", "2024-07-02T20:00:15.123456+05:30"] + mock_req = b'mocked_request_data' + mock_nonce = 'mocked_nonce' + mock_make_v2_request.return_value = (mock_req, mock_nonce) + mock_response = MagicMock() + mock_response.read.return_value = b'{"mocked": "response"}' + mock_post.return_value = mock_response + mock_parse_v2_response.return_value = b'{"body":[],"status":"success"}' + print(expected_req) + for timestamp in test_cases: + self.identity_map_client.get_identity_buckets(dt.datetime.fromisoformat(timestamp)) + called_args, called_kwargs = mock_make_v2_request.call_args + self.assertEqual(expected_req, called_args[2]) From d0c5db7e524dfcfda3bad90a289efc368a656d22 Mon Sep 17 00:00:00 2001 From: Caroline6312 Date: Thu, 11 Jul 2024 18:19:53 -0700 Subject: [PATCH 2/2] Address the comments --- tests/test_identity_map_client_unit_tests.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_identity_map_client_unit_tests.py b/tests/test_identity_map_client_unit_tests.py index 6ce3083..7820969 100644 --- a/tests/test_identity_map_client_unit_tests.py +++ b/tests/test_identity_map_client_unit_tests.py @@ -1,3 +1,4 @@ +import base64 import json import unittest import datetime as dt @@ -7,7 +8,7 @@ class IdentityMapUnitTests(unittest.TestCase): - UID2_SECRET_KEY = "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=" + UID2_SECRET_KEY = base64.b64encode(b"UID2_CLIENT_SECRET").decode() identity_map_client = IdentityMapClient("UID2_BASE_URL", "UID2_API_KEY", UID2_SECRET_KEY) def test_identity_buckets_invalid_timestamp(self): @@ -37,8 +38,7 @@ def test_get_datetime_utc_iso_format_timestamp(self): @patch('uid2_client.identity_map_client.post') @patch('uid2_client.identity_map_client.parse_v2_response') def test_identity_buckets_request(self, mock_parse_v2_response, mock_post, mock_make_v2_request): - expected_timestamp = "2024-07-02T14:30:15.123456+00:00" - expected_req = json.dumps({"since_timestamp": get_datetime_utc_iso_format(dt.datetime.fromisoformat(expected_timestamp))}).encode() + expected_req = b'{"since_timestamp": "2024-07-02T14:30:15.123456"}' test_cases = ["2024-07-02T14:30:15.123456+00:00", "2024-07-02 09:30:15.123456-05:00", "2024-07-02T08:30:15.123456-06:00", "2024-07-02T10:30:15.123456-04:00", "2024-07-02T06:30:15.123456-08:00", "2024-07-02T23:30:15.123456+09:00", @@ -50,7 +50,6 @@ def test_identity_buckets_request(self, mock_parse_v2_response, mock_post, mock_ mock_response.read.return_value = b'{"mocked": "response"}' mock_post.return_value = mock_response mock_parse_v2_response.return_value = b'{"body":[],"status":"success"}' - print(expected_req) for timestamp in test_cases: self.identity_map_client.get_identity_buckets(dt.datetime.fromisoformat(timestamp)) called_args, called_kwargs = mock_make_v2_request.call_args