Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions devel-common/src/tests_common/test_utils/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,20 @@ def _finalize(self) -> None:
self.PER_LOGGER_LEVELS[logger_name] = level

def __contains__(self, target):
import operator

if isinstance(target, str):

def predicate(e):
return e["event"] == target
elif isinstance(target, dict):
# An empty target would vacuously match every entry, silently passing the assertion
if not target:
raise ValueError("Can't search logs using an empty dict")
# Partial comparison -- only check keys passed in
get = operator.itemgetter(*target.keys())
want = tuple(target.values())

def predicate(e):
try:
got = get(e)
got = tuple(e[key] for key in target)
return all(
expected.match(val) if isinstance(expected, re.Pattern) else val == expected
for (val, expected) in zip(got, want)
Expand Down
62 changes: 62 additions & 0 deletions devel-common/tests/unit/tests_common/test_utils/test_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import re

import pytest

from tests_common.test_utils.logs import StructlogCapture


@pytest.fixture
def capture():
cap = StructlogCapture()
cap.entries.append({"event": "some event", "field1": False, "field2": [1, 2]})
return cap


class TestStructlogCaptureContains:
"""The dict branch of ``__contains__`` used to collapse a single-key target and never match."""

@pytest.mark.parametrize(
"target",
[
pytest.param({"event": "some event"}, id="single-key-event"),
pytest.param({"field2": [1, 2]}, id="single-key-other-field"),
pytest.param({"event": re.compile("some")}, id="single-key-pattern"),
pytest.param({"event": "some event", "field1": False}, id="two-keys"),
],
)
def test_match(self, capture, target):
assert target in capture

@pytest.mark.parametrize(
"target",
[
pytest.param({"event": "other event"}, id="single-key-wrong-value"),
pytest.param({"missing": "x"}, id="single-key-missing"),
pytest.param({"event": "some event", "field1": True}, id="two-keys-one-wrong"),
],
)
def test_no_match(self, capture, target):
assert target not in capture

def test_empty_target_is_rejected(self, capture):
with pytest.raises(ValueError, match="empty dict"):
assert {} in capture
Loading