Coverage for tests / test_argparsing.py: 25%
72 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:43 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:43 +0000
1# This file is part of utils.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import argparse
23import logging
24import unittest
26from lsst.utils.argparsing import AppendDict
28log = logging.getLogger("test_argparsing")
31class AppendDictTestSuite(unittest.TestCase):
32 """Test suite for AppendDict action."""
34 def setUp(self):
35 super().setUp()
37 self.testbed = argparse.ArgumentParser()
39 def test_default_none_positional(self):
40 self.testbed.add_argument("test", action=AppendDict, nargs="*")
42 namespace = self.testbed.parse_args([])
43 self.assertEqual(namespace.test, {})
45 namespace = self.testbed.parse_args("baz=bak".split())
46 self.assertEqual(namespace.test, {"baz": "bak"})
48 def test_default_none_keyword(self):
49 self.testbed.add_argument("--test", action=AppendDict)
51 namespace = self.testbed.parse_args([])
52 self.assertEqual(namespace.test, {})
54 namespace = self.testbed.parse_args("--test baz=bak".split())
55 self.assertEqual(namespace.test, {"baz": "bak"})
57 def test_default_empty_positional(self):
58 self.testbed.add_argument("test", action=AppendDict, default={}, nargs="*")
60 namespace = self.testbed.parse_args([])
61 self.assertEqual(namespace.test, {})
63 namespace = self.testbed.parse_args("baz=bak".split())
64 self.assertEqual(namespace.test, {"baz": "bak"})
66 def test_default_empty_keyword(self):
67 self.testbed.add_argument("--test", action=AppendDict, default={})
69 namespace = self.testbed.parse_args([])
70 self.assertEqual(namespace.test, {})
72 namespace = self.testbed.parse_args("--test baz=bak".split())
73 self.assertEqual(namespace.test, {"baz": "bak"})
75 def test_default_non_empty_positional(self):
76 self.testbed.add_argument("test", action=AppendDict, default={"foo": "bar"}, nargs="*")
78 namespace = self.testbed.parse_args([])
79 self.assertEqual(namespace.test, {"foo": "bar"})
81 namespace = self.testbed.parse_args("baz=bak".split())
82 self.assertEqual(namespace.test, {"foo": "bar", "baz": "bak"})
84 namespace = self.testbed.parse_args("foo=fum".split())
85 self.assertEqual(namespace.test, {"foo": "fum"})
87 def test_default_non_empty_keyword(self):
88 self.testbed.add_argument("--test", action=AppendDict, default={"foo": "bar"})
90 namespace = self.testbed.parse_args([])
91 self.assertEqual(namespace.test, {"foo": "bar"})
93 namespace = self.testbed.parse_args("--test baz=bak".split())
94 self.assertEqual(namespace.test, {"foo": "bar", "baz": "bak"})
96 namespace = self.testbed.parse_args("--test foo=fum".split())
97 self.assertEqual(namespace.test, {"foo": "fum"})
99 def test_default_invalid(self):
100 with self.assertRaises(TypeError):
101 self.testbed.add_argument("test", action=AppendDict, default="bovine")
102 with self.assertRaises(TypeError):
103 self.testbed.add_argument("test", action=AppendDict, default=[])
105 def test_multi_append(self):
106 self.testbed.add_argument("--test", action=AppendDict)
108 namespace = self.testbed.parse_args("--test foo=bar --test baz=bak".split())
109 self.assertEqual(namespace.test, {"foo": "bar", "baz": "bak"})
111 def test_multi_nargs_append(self):
112 self.testbed.add_argument("--test", action=AppendDict, nargs="*")
114 namespace = self.testbed.parse_args("--test foo=bar fee=fum --test baz=bak --test".split())
115 self.assertEqual(namespace.test, {"foo": "bar", "fee": "fum", "baz": "bak"})
117 def test_emptyvalue(self):
118 self.testbed.add_argument("test", action=AppendDict)
120 namespace = self.testbed.parse_args("foo=".split())
121 self.assertEqual(namespace.test, {"foo": ""})
123 def test_nopair(self):
124 self.testbed.add_argument("test", action=AppendDict)
126 with self.assertRaises(ValueError):
127 self.testbed.parse_args("foo".split())
129 with self.assertRaises(ValueError):
130 self.testbed.parse_args("assertion=beauty=truth".split())