def test_apply_argparse_15(): """ Test of the apply_argparse function for Schema 15. Here we have a doubly nested override. """ schema_15 = Schema_15() config = Config(schema_15) parser = argparse.ArgumentParser("new-parser") argparse_options(parser, schema_15) args = parser.parse_args( [ "--nested-schema.time-duration-attr", "2y 2d 2h 2m 2s 2ms", "--nested-schema.float-attr", "9.87", "--nested-schema.schema-attr.int-attr", "90210", "--nested-schema.schema-attr.str-attr", "updated", ] ) apply_argparse(args, config) # Config should be overwritten by arguments assert config["nested_schema"]["float_attr"] == 9.87 assert config["nested_schema"]["time_duration_attr"] == 63295322.002 assert config["nested_schema"]["schema_attr"]["int_attr"] == 90210 assert config["nested_schema"]["schema_attr"]["str_attr"] == "updated"
def test_apply_argparse_10_invalid(): """ Test of the apply_argparse function for Schema 10. Four nested schema options are passed to the config. """ schema_10 = Schema_10() with open( os.path.join(os.path.dirname(__file__), "data", "defaults", "in.01.json") ) as fh: config_data = json.load(fh) config = Config(schema_10, config_data) parser = argparse.ArgumentParser("new-parser") argparse_options(parser, schema_10) parser.add_argument("--schema-attr.new-attr-int", type=int) parser.add_argument("--schema-attr.new-attr-str", type=str) args = parser.parse_args( [ "--schema-attr.new-attr-int", "2222", "--schema-attr.new-attr-str", "hello world", ] ) apply_argparse(args, config) # New arguments should not get added to config assert not config["schema_attr"].get("new_attribute_int", False) assert not config["schema_attr"].get("new_attribute_str", False)
def test_apply_argparse_03_invalid(): """ Test of the apply_argparse function for Schema 03. Here we provide additional attributes outside of the schema. These should be marked as invalid. """ config = Config(Schema_03()) parser = argparse.ArgumentParser("new-parser") argparse_options(parser, Schema_03()) parser.add_argument("--new-attribute-int", type=int) parser.add_argument("--new-attribute-str", type=str) args = parser.parse_args( [ "--new-attribute-int", "123", "--new-attribute-str", "not good", ] ) config = apply_argparse(args, config) # New arguments should not get added to config assert not config.get("new_attribute_int", False) assert not config.get("new_attribute_str", False)
def test_apply_argparse_10(): """ Test of the apply_argparse function for Schema 10. Four nested schema options are passed to the config. """ schema_10 = Schema_10() with open( os.path.join(os.path.dirname(__file__), "data", "defaults", "in.01.json") ) as fh: config_data = json.load(fh) config = Config(schema_10, config_data) parser = argparse.ArgumentParser("new-parser") argparse_options(parser, schema_10) args = parser.parse_args( [ "--schema-attr.int-attr", "2222", "--schema-attr.str-attr", "hello world", "--schema-attr.str-attr-nd", "defaults empty string", "--schema-attr.str-attr-null", "not null", ] ) apply_argparse(args, config) # Config should be overwritten by arguments assert config["schema_attr"]["int_attr"] == 2222 assert config["schema_attr"]["str_attr"] == "hello world" assert config["schema_attr"]["str_attr_nd"] == "defaults empty string" assert config["schema_attr"]["str_attr_null"] == "not null"
def test_apply_argparse_03(): """ Test of the apply_argparse function for Schema 03. Two nested schema options are passed to the config. """ config = Config(Schema_03()) parser = argparse.ArgumentParser("new-parser") argparse_options(parser, Schema_03()) args = parser.parse_args( [ "--str-attr", "donkey", "--list-attr-int", "1,2,3", "--list-attr-str", "1,2,3", "--int-attr", "999", "--float-attr", "365.2", "--bool-attr", "--bool-attr-w-dflt", "--no-bool-attr-w-dflt-yes", "--nested.int-attr-choices", "2", "--nested.int-attr", "3", ] ) config = apply_argparse(args, config) # Config should be overwritten by arguments assert config["str_attr"] == "donkey" assert config["list_attr_int"] == [1, 2, 3] assert config["list_attr_str"] == ["1", "2", "3"] assert config["int_attr"] == 999 assert config["bool_attr"] is True assert config["bool_attr_w_dflt"] is True assert config["bool_attr_w_dflt_yes"] is False assert config["nested"]["int_attr_choices"] == 2 assert config["nested"]["int_attr"] == 3