def test_array_of_strings_overridden(self): parser = JobConfigSchemaAsArgsParser.from_description(self.job_config_schema, 'testjob') out = parser.parse(['--divisions', 'a']) expected = { 'divisions': ['a'], 'boolwithdefaultfalse': False, 'boolwithdefaulttrue': True } self.assertEqual(expected, out)
def test_array_of_strings_default(self) -> None: parser = JobConfigSchemaAsArgsParser.from_description(self.job_config_schema, 'testjob') out = parser.parse([]) expected = { 'divisions': ['candidates', 'counties', 'townships', 'precincts'], 'boolwithdefaultfalse': False, 'boolwithdefaulttrue': True } self.assertEqual(expected, out)
def test_enum_cannot_be_invalid(self) -> None: parser = JobConfigSchemaAsArgsParser.from_description(self.job_config_schema, 'testjob') with self.assertRaises(SystemExit) as r: parser.parse(['--enumwithnulloption', 'x']) self.assertEqual(str(r.exception.__context__), "argument --enumwithnulloption: invalid choice: 'x' " "(choose from 'a', 'b')")
def test_enum_can_be_set(self) -> None: parser = JobConfigSchemaAsArgsParser.from_description(self.job_config_schema, 'testjob') out = parser.parse(['--enumwithnulloption', 'a']) expected = { 'divisions': ['candidates', 'counties', 'townships', 'precincts'], 'boolwithdefaultfalse': False, 'boolwithdefaulttrue': True, 'enumwithnulloption': 'a' } self.assertEqual(expected, out)
def test_boolean_no_default_false(self) -> None: parser = JobConfigSchemaAsArgsParser.from_description(self.job_config_schema, 'testjob') out = parser.parse(['--no_boolwithnodefault']) expected = { 'divisions': ['candidates', 'counties', 'townships', 'precincts'], 'boolwithdefaultfalse': False, 'boolwithdefaulttrue': True, 'boolwithnodefault': False, } self.assertEqual(expected, out)
def test_dict_of_dict(self): parser = JobConfigSchemaAsArgsParser.from_description(self.job_config_schema, 'testjob') out = parser.parse(['--nested_dict.object_key', 'a']) expected = { 'nested_dict': { 'object_key': 'a' }, 'divisions': ['candidates', 'counties', 'townships', 'precincts'], 'boolwithdefaultfalse': False, 'boolwithdefaulttrue': True } self.assertEqual(expected, out)
def test_bad_syntax_4(self): bad_job_config_schema = { "type": "object", "properties": 123 } parser = JobConfigSchemaAsArgsParser.from_description(bad_job_config_schema, 'testjob') with self.assertRaises(TypeError) as r: parser.parse([]) self.assertEqual(str(r.exception), "Did not understand 123 in " "{'type': 'object', 'properties': 123}")
def test_bad_syntax_1(self): bad_job_config_schema = { "type": "object", "properties": odict[ 'divisions': 123 # should be another json schema, not a number... ], } parser = JobConfigSchemaAsArgsParser.from_description(bad_job_config_schema, 'testjob') with self.assertRaises(TypeError) as r: parser.parse([]) self.assertEqual(str(r.exception), 'Did not understand [123] in [odict[divisions: 123]]')
def test_bad_syntax_2(self): bad_job_config_schema = { "type": "object", "properties": odict[ 'divisions': { 'type': 'array', 'items': 123, }, ], } parser = JobConfigSchemaAsArgsParser.from_description(bad_job_config_schema, 'testjob') with self.assertRaises(TypeError) as r: parser.parse([]) self.assertEqual(str(r.exception), "Did not understand [123] in " "[odict[divisions: {'type': 'array', 'items': 123}]]")