From d23a8287fd537cd0cd4a08ce183bd66c86360c80 Mon Sep 17 00:00:00 2001 From: Tiara Rodney Date: Sat, 6 Jun 2026 14:35:02 +0200 Subject: [PATCH] test: add KeyValueAction unit tests --- .../byteb4rb1e/utils/argparse/test_actions.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/unit/byteb4rb1e/utils/argparse/test_actions.py diff --git a/tests/unit/byteb4rb1e/utils/argparse/test_actions.py b/tests/unit/byteb4rb1e/utils/argparse/test_actions.py new file mode 100644 index 0000000..3d0c150 --- /dev/null +++ b/tests/unit/byteb4rb1e/utils/argparse/test_actions.py @@ -0,0 +1,52 @@ +"""Tests for custom argparse actions.""" + +from argparse import ArgumentParser + +import pytest + +from byteb4rb1e.utils.argparse.actions import KeyValueAction + + +def _parse(*args): + parser = ArgumentParser() + parser.add_argument("--config", action=KeyValueAction, default={}, metavar="KEY=VALUE") + return parser.parse_args(list(args)) + + +class TestKeyValueAction: + + def test_single_pair(self): + args = _parse("--config", "key=value") + assert args.config == {"key": "value"} + + def test_multiple_pairs(self): + args = _parse("--config", "a=1", "--config", "b=2") + assert args.config == {"a": "1", "b": "2"} + + def test_dotted_key(self): + args = _parse("--config", "provider.base_url=http://localhost") + assert args.config == {"provider.base_url": "http://localhost"} + + def test_value_with_equals(self): + args = _parse("--config", "url=http://host?a=1&b=2") + assert args.config == {"url": "http://host?a=1&b=2"} + + def test_empty_value(self): + args = _parse("--config", "key=") + assert args.config == {"key": ""} + + def test_strips_whitespace(self): + args = _parse("--config", " key = value ") + assert args.config == {"key": "value"} + + def test_overwrites_duplicate_key(self): + args = _parse("--config", "key=first", "--config", "key=second") + assert args.config == {"key": "second"} + + def test_default_empty_dict(self): + args = _parse() + assert args.config == {} + + def test_no_equals_raises(self): + with pytest.raises(SystemExit): + _parse("--config", "no_equals_here")