Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
tests: adds tests for retiring many serialized users at once
Browse files Browse the repository at this point in the history
and alters the code to show coverage of this branch.
  • Loading branch information
pomegranited committed Dec 7, 2023
1 parent edc380f commit 972e37c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions event_sink_clickhouse/sinks/user_retire.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ def send_item(self, serialized_item, many=False):
Send delete queries to remove the serialized User from ClickHouse.
"""
users = serialized_item if many else [serialized_item]
if many:
users = serialized_item
else:
users = [serialized_item]
user_ids = {str(user["user_id"]) for user in users}
user_ids_str = ",".join(user_ids)
user_ids_str = ",".join(sorted(user_ids))
clickhouse_pii_tables = getattr(
settings, "EVENT_SINK_CLICKHOUSE_PII_MODELS", []
)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_user_retire.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for the user_retire sinks.
"""
import logging
from unittest.mock import patch

import responses
Expand All @@ -11,6 +12,8 @@
from event_sink_clickhouse.tasks import dump_data_to_clickhouse
from test_utils.helpers import FakeUser

log = logging.getLogger(__name__)


@responses.activate( # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
registry=OrderedRegistry
Expand Down Expand Up @@ -64,3 +67,39 @@ def test_retire_user(mock_user_model, mock_is_enabled, mock_serialize_item):
assert mock_serialize_item.call_count == 1
assert user_profile_delete.call_count == 1
assert external_id_delete.call_count == 1


@responses.activate( # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
registry=OrderedRegistry
)
@override_settings(EVENT_SINK_CLICKHOUSE_PII_MODELS=["user_profile"])
@patch("event_sink_clickhouse.sinks.user_retire.UserRetirementSink.serialize_item")
def test_retire_many_users(mock_serialize_item):
"""
Test of a successful "many users" retirement.
"""
# Create and serialize a few fake users
users = (FakeUser(246), FakeUser(22), FakeUser(91))
mock_serialize_item.return_value = [{"user_id": user.id} for user in users]

# Use the responses library to catch the POSTs to ClickHouse
# and match them against the expected values
user_profile_delete = responses.post(
"https://foo.bar/",
match=[
responses.matchers.query_param_matcher(
{
"query": "ALTER TABLE cool_data.user_profile DELETE WHERE user_id in (22,246,91)",
}
)
],
)

sink = UserRetirementSink(None, log)
sink.dump(
item_id=users[0].id,
many=True,
)

assert mock_serialize_item.call_count == 1
assert user_profile_delete.call_count == 1

0 comments on commit 972e37c

Please sign in to comment.