From c4914fb4c91a79377ef44919ddc2878305a40d00 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Fri, 8 Dec 2023 09:21:36 +0100 Subject: [PATCH] testutils: Add assert_dict_has This is a convenient way for tests to assert that some nested dicts (like a parsed json) has a particular key/value somewhere in it. For example: assert_dict_has(config, "toplevel.subitem.key", True) --- osbuild/testutil/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/osbuild/testutil/__init__.py b/osbuild/testutil/__init__.py index cdaf849242..b429f3439f 100644 --- a/osbuild/testutil/__init__.py +++ b/osbuild/testutil/__init__.py @@ -6,3 +6,10 @@ def has_executable(executable: str) -> bool: return shutil.which(executable) is not None + + +def assert_dict_has(v, keys, expected_value): + for key in keys.split("."): + assert key in v + v = v[key] + assert v == expected_value