Skip to content

Commit d205df8

Browse files
eli-r-phclaude
andcommitted
test: cover the event name, warn-once and personal key helpers in test_utils
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a65ed52 commit d205df8

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎posthog/test/test_utils.py‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,75 @@ def clean_or_raise(value):
354354
):
355355
assert utils._clean_dict({"ok": 1, "bad": bad_value}) == {"ok": 1}
356356

357-
warning.assert_called_once()
358-
assert warning.call_args.args[1:] == ("bad", type(bad_value))
359-
assert bad_value not in warning.call_args.args
357+
warning.assert_called_once_with(
358+
'Dictionary value for key "%s" of type %s is not JSON serializable, dropping it.',
359+
"bad",
360+
type(bad_value),
361+
)
362+
363+
@parameterized.expand(
364+
[
365+
("missing", None, "missing"),
366+
("non_string", 123, "non-string"),
367+
("bytes", b"event", "non-string"),
368+
("empty", "", "empty"),
369+
("valid", "signed_up", None),
370+
("whitespace_is_a_name", " ", None),
371+
]
372+
)
373+
def test_invalid_event_name_reason(self, _, event, expected):
374+
assert utils._invalid_event_name_reason(event) == expected
375+
376+
def test_drop_invalid_event_name_logs_each_reason_once(self):
377+
logger = mock.Mock()
378+
with mock.patch.object(utils, "_warned_reasons", set()):
379+
dropped = [
380+
utils._drop_invalid_event_name(logger, msg)
381+
for msg in ({"event": None}, {}, {"event": ""}, {"event": "ok"})
382+
]
383+
384+
assert dropped == [True, True, True, False]
385+
message = (
386+
"Dropping an event with a %s name: event names must be non-empty strings. "
387+
"Later events dropped for the same reason are not logged."
388+
)
389+
assert logger.warning.call_args_list == [
390+
mock.call(message, "missing"),
391+
mock.call(message, "empty"),
392+
]
393+
394+
def test_warn_once_logs_each_reason_once(self):
395+
logger = mock.Mock()
396+
with mock.patch.object(utils, "_warned_reasons", set()):
397+
utils._warn_once(logger, "a", "first %s", 1)
398+
utils._warn_once(logger, "a", "again %s", 2)
399+
utils._warn_once(logger, "b", "second")
400+
401+
assert logger.warning.call_args_list == [
402+
mock.call("first %s", 1),
403+
mock.call("second"),
404+
]
405+
406+
@parameterized.expand(
407+
[
408+
("personal", "phx_abc", True),
409+
("project", "phc_abc", False),
410+
("prefix_elsewhere", "abc_phx_", False),
411+
("empty", "", False),
412+
]
413+
)
414+
def test_is_personal_api_key(self, _, api_key, expected):
415+
assert utils._is_personal_api_key(api_key) is expected
360416

361417
def test_clean_drops_only_keys_json_cannot_write(self):
362418
cleaned = utils.clean(
363419
{
420+
("t",): "tuple",
364421
"s": 1,
365422
2: "int",
366423
1.5: "float",
367424
True: "bool",
368425
None: "none",
369-
("t",): "tuple",
370426
}
371427
)
372428
assert cleaned == {"s": 1, 2: "int", 1.5: "float", True: "bool", None: "none"}

‎posthog/utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _drop_invalid_event_name(logger: logging.Logger, msg: dict) -> bool:
175175
return False
176176
_warn_once(
177177
logger,
178-
f"event_name_{reason}",
178+
f"event_name_{reason}", # pragma: no mutate
179179
"Dropping an event with a %s name: event names must be non-empty strings. "
180180
"Later events dropped for the same reason are not logged.",
181181
reason,

0 commit comments

Comments
 (0)