def test_yaml_file_writer_save_path(self, monkeypatch): """Test the YAML writer works correctly""" with monkeypatch.context() as m: m.setattr( sys, "argv", ["", "--config", "./tests/conf/yaml/test_save_path.yaml"]) config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) # Test the chained version now = datetime.datetime.now() curr_int_time = int( f"{now.year}{now.month}{now.day}{now.hour}{now.second}") config_values = config.save( file_extension=".yaml", file_name=f"pytest.{curr_int_time}").generate() yaml_regex = re.compile( fr"pytest.{curr_int_time}." fr"[a-fA-F0-9]{{8}}-[a-fA-F0-9]{{4}}-[a-fA-F0-9]{{4}}-" fr"[a-fA-F0-9]{{4}}-[a-fA-F0-9]{{12}}.spock.cfg.yaml") matches = [ re.fullmatch(yaml_regex, val) for val in os.listdir(str(config_values.TypeConfig.save_path)) if re.fullmatch(yaml_regex, val) is not None ] fname = f"{str(config_values.TypeConfig.save_path)}/{matches[0].string}" with open(fname, "r") as fin: print(fin.read()) assert os.path.exists(fname) # Clean up if assert is good if os.path.exists(fname): os.remove(fname)
def test_yaml_s3_mock_file_writer_missing_s3(self, monkeypatch, tmp_path, s3): """Test the YAML writer works correctly""" with monkeypatch.context() as m: aws_session, s3_client = s3 m.setattr(sys, "argv", ["", "--config", "./tests/conf/yaml/test.yaml"]) config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) # Mock a S3 bucket and object mock_s3_bucket = "spock-test" mock_s3_object = "fake/test/bucket/" s3_client.create_bucket(Bucket=mock_s3_bucket) # Test the chained version now = datetime.datetime.now() curr_int_time = int( f"{now.year}{now.month}{now.day}{now.hour}{now.second}") with pytest.raises(ValueError): # Test the chained version config.save( user_specified_path=f"s3://foo/{mock_s3_object}", file_extension=".yaml", file_name=f"pytest.save.{curr_int_time}", ).generate()
def test_serialization_deserialization(self, monkeypatch, tmp_path): """Test serialization/de-serialization""" with monkeypatch.context() as m: m.setattr(sys, "argv", ["", "--config", "./tests/conf/yaml/test.yaml"]) # Serialize config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) now = datetime.datetime.now() curr_int_time = int( f"{now.year}{now.month}{now.day}{now.hour}{now.second}") config_values = config.save( file_extension=".yaml", file_name=f"pytest.{curr_int_time}", user_specified_path=tmp_path).generate() yaml_regex = re.compile( fr"pytest.{curr_int_time}." fr"[a-fA-F0-9]{{8}}-[a-fA-F0-9]{{4}}-[a-fA-F0-9]{{4}}-" fr"[a-fA-F0-9]{{4}}-[a-fA-F0-9]{{12}}.spock.cfg.yaml") matches = [ re.fullmatch(yaml_regex, val) for val in os.listdir(str(tmp_path)) if re.fullmatch(yaml_regex, val) is not None ] fname = f"{str(tmp_path)}/{matches[0].string}" # Deserialize m.setattr(sys, "argv", ["", "--config", f"{fname}"]) de_serial_config = ConfigArgBuilder( *all_configs, desc="Test Builder", ).generate() assert config_values == de_serial_config
def test_raises_missing_class(self, monkeypatch): with monkeypatch.context() as m: m.setattr(sys, "argv", ["", "--config", "./tests/conf/yaml/test.yaml"]) with pytest.raises(ValueError): config = ConfigArgBuilder(*all_configs[:-1]) return config.generate()
def arg_builder(monkeypatch): with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/json/test.json']) config = ConfigArgBuilder(TypeConfig, TypeOptConfig, TypeDefaultConfig, TypeDefaultOptConfig, desc='Test Builder') return config.generate()
def arg_builder(monkeypatch): with monkeypatch.context() as m: m.setattr(sys, 'argv', [ '', '--config', './tests/conf/yaml/test_class.yaml', '--TypeConfig.bool_p', '--TypeConfig.int_p', '11', '--TypeConfig.float_p', '11.0', '--TypeConfig.string_p', 'Hooray', '--TypeConfig.list_p_float', '[11.0,21.0]', '--TypeConfig.list_p_int', '[11, 21]', '--TypeConfig.list_p_str', "['Hooray', 'Working']", '--TypeConfig.list_p_bool', '[False, True]', '--TypeConfig.tuple_p_float', '(11.0, 21.0)', '--TypeConfig.tuple_p_int', '(11, 21)', '--TypeConfig.tuple_p_str', "('Hooray', 'Working')", '--TypeConfig.tuple_p_bool', '(False, True)', '--TypeConfig.list_list_p_int', "[[11, 21], [11, 21]]", '--TypeConfig.choice_p_str', 'option_2', '--TypeConfig.choice_p_int', '20', '--TypeConfig.choice_p_float', '20.0', '--TypeConfig.list_choice_p_str', "['option_2']", '--TypeConfig.list_list_choice_p_str', "[['option_2'], ['option_2']]", '--TypeConfig.list_choice_p_int', '[20]', '--TypeConfig.list_choice_p_float', '[20.0]', '--NestedStuff.one', '12', '--NestedStuff.two', 'ancora', '--TypeConfig.nested_list.NestedListStuff.one', '[11, 21]', '--TypeConfig.nested_list.NestedListStuff.two', "['Hooray', 'Working']", ]) config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, desc='Test Builder') return config.generate()
def test_callable_module(self, monkeypatch): with monkeypatch.context() as m: m.setattr( sys, "argv", ["", "--config", "./tests/conf/yaml/test_fail_callable.yaml"]) with pytest.raises(_SpockValueError): config = ConfigArgBuilder(*all_configs) return config.generate()
def arg_builder(monkeypatch): with monkeypatch.context() as m: config = ConfigArgBuilder( *all_configs, no_cmd_line=True, configs=["./tests/conf/yaml/test.yaml"], ) return config.generate()
def test_default_file_writer(self, monkeypatch, tmp_path): """Test the default writer works correctly""" with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/yaml/test.yaml']) config = ConfigArgBuilder(TypeConfig, TypeOptConfig, desc='Test Builder') # Test the chained version config.save(user_specified_path=tmp_path).generate() assert len(list(tmp_path.iterdir())) == 1
def arg_builder(monkeypatch): with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/yaml/inherited.yaml']) config = ConfigArgBuilder(TypeInherited, NestedStuff, NestedListStuff, desc='Test Builder') return config.generate()
def arg_builder(monkeypatch): with monkeypatch.context() as m: config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, no_cmd_line=True, configs=['./tests/conf/yaml/test.yaml']) return config.generate()
def arg_builder(monkeypatch): with monkeypatch.context() as m: m.setattr(sys, "argv", [""]) config = ConfigArgBuilder( *all_configs, desc="Test Builder", configs=["./tests/conf/yaml/test.yaml"], ) return config.generate()
def test_cmd_line_kwarg_raise(self, monkeypatch): with monkeypatch.context() as m: with pytest.raises(TypeError): config = ConfigArgBuilder( *all_configs, no_cmd_line=True, configs="./tests/conf/yaml/test.yaml", ) return config.generate()
def arg_builder(monkeypatch): with monkeypatch.context() as m: m.setattr( sys, "argv", [ "", "--config", "./tests/conf/yaml/test_class.yaml", "--TypeConfig.bool_p", "--TypeConfig.int_p", "11", "--TypeConfig.float_p", "11.0", "--TypeConfig.string_p", "Hooray", "--TypeConfig.list_p_float", "[11.0,21.0]", "--TypeConfig.list_p_int", "[11, 21]", "--TypeConfig.list_p_str", "['Hooray', 'Working']", "--TypeConfig.list_p_bool", "[False, True]", "--TypeConfig.tuple_p_float", "(11.0, 21.0)", "--TypeConfig.tuple_p_int", "(11, 21)", "--TypeConfig.tuple_p_str", "('Hooray', 'Working')", "--TypeConfig.tuple_p_bool", "(False, True)", "--TypeConfig.tuple_p_mixed", "(5, 11.5)", "--TypeConfig.list_list_p_int", "[[11, 21], [11, 21]]", "--TypeConfig.choice_p_str", "option_2", "--TypeConfig.choice_p_int", "20", "--TypeConfig.choice_p_float", "20.0", "--TypeConfig.list_choice_p_str", "['option_2']", "--TypeConfig.list_list_choice_p_str", "[['option_2'], ['option_2']]", "--TypeConfig.list_choice_p_int", "[20]", "--TypeConfig.list_choice_p_float", "[20.0]", "--TypeConfig.class_enum", "NestedStuff", "--NestedStuff.one", "12", "--NestedStuff.two", "ancora", "--TypeConfig.nested_list.NestedListStuff.one", "[11, 21]", "--TypeConfig.nested_list.NestedListStuff.two", "['Hooray', 'Working']", "--TypeConfig.high_config", "SingleNestedConfig", "--SingleNestedConfig.double_nested_config", "SecondDoubleNestedConfig", "--SecondDoubleNestedConfig.morph_tolerance", "0.2", "--TypeConfig.call_me", 'tests.base.attr_configs_test.bar', "--TypeConfig.call_us", "['tests.base.attr_configs_test.bar', 'tests.base.attr_configs_test.bar']", "--TypeConfig.str_dict", "{'key_1': 2.5, 'key_2': 3.5}", "--TypeConfig.int_list_str_dict", "{'1': ['again', 'test'], '2': ['test', 'me']}", "--TypeConfig.float_tuple_callable_dict", '{"1.0": ("tests.base.attr_configs_test.bar", "tests.base.attr_configs_test.foo"), "2.0": ("tests.base.attr_configs_test.bar", "tests.base.attr_configs_test.foo")}', "--TypeConfig.hardest", '[{"key_1": ("tests.base.attr_configs_test.bar", "tests.base.attr_configs_test.foo"), "key_2": ("tests.base.attr_configs_test.bar", "tests.base.attr_configs_test.foo")}, {"key_3": ("tests.base.attr_configs_test.bar", "tests.base.attr_configs_test.foo"), "key_4": ("tests.base.attr_configs_test.bar", "tests.base.attr_configs_test.foo")}]' ], ) config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, SingleNestedConfig, FirstDoubleNestedConfig, SecondDoubleNestedConfig, desc="Test Builder") return config.generate()
def test_wrong_input_raise(self, monkeypatch): with monkeypatch.context() as m: m.setattr(sys, "argv", ["", "--config", "./tests/conf/yaml/test.foo"]) with pytest.raises(TypeError): config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) return config.generate()
def arg_builder(monkeypatch): with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/toml/test.toml']) config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') return config.generate()
def test_wrong_input_raise(self, monkeypatch): with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/yaml/test.foo']) with pytest.raises(ValueError): config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') return config.generate()
def test_repr(self, monkeypatch, capsys): with monkeypatch.context() as m: m.setattr(sys, "argv", ["", "--config", "./tests/conf/yaml/test.yaml"]) config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) print(config.generate()) out, _ = capsys.readouterr() assert ("NestedListStuff" in out) and "TypeConfig" in out
def test_help(self, monkeypatch): with monkeypatch.context() as m: m.setattr( sys, "argv", ["", "--config", "./tests/conf/yaml/test.yaml", "--help"]) with pytest.raises(SystemExit): config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) return config.generate()
def test_cmd_line_kwarg_raise(self, monkeypatch): with monkeypatch.context() as m: with pytest.raises(ValueError): config = ConfigArgBuilder( TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, no_cmd_line=True, configs='./tests/conf/yaml/test.yaml') return config.generate()
def test_repr(self, monkeypatch, capsys): with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/yaml/test.yaml']) config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') print(config.generate()) out, _ = capsys.readouterr() assert ('NestedListStuff' in out) and 'TypeConfig' in out
def test_cmd_line_no_key(self, monkeypatch): with monkeypatch.context() as m: m.setattr(sys, 'argv', [ '', '--config', './tests/conf/yaml/test.yaml', '--TypeConfig.foo_bar_stuff', '11' ]) with pytest.raises(SystemExit): config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, desc='Test Builder') return config.generate()
def test_repeated_defs_fail(self, monkeypatch): with monkeypatch.context() as m: m.setattr( sys, "argv", [""], ) with pytest.raises(_SpockInstantiationError): config = ConfigArgBuilder(RepeatedDefsFailConfig, NestedListStuff, desc="Test Builder") config.generate()
def test_cmd_line_list_len(self, monkeypatch): with monkeypatch.context() as m: m.setattr(sys, 'argv', [ '', '--config', './tests/conf/yaml/test.yaml', '--TypeConfig.nested_list.NestedListStuff.one', '[11]' ]) with pytest.raises(ValueError): config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, desc='Test Builder') return config.generate()
def test_help(self, monkeypatch): with monkeypatch.context() as m: m.setattr( sys, 'argv', ['', '--config', './tests/conf/yaml/test.yaml', '--help']) with pytest.raises(SystemExit): config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') return config.generate()
def test_default_file_writer(self, monkeypatch, tmp_path): """Test the default writer works correctly""" with monkeypatch.context() as m: m.setattr(sys, "argv", ["", "--config", "./tests/conf/yaml/test.yaml"]) config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) # Test the chained version config.save(user_specified_path=tmp_path).generate() assert len(list(tmp_path.iterdir())) == 1
def test_non_attrs_fail(self, monkeypatch): with monkeypatch.context() as m: with pytest.raises(TypeError): class AttrFail: failed_attr: int config = ConfigArgBuilder( *all_configs, AttrFail, configs=["./tests/conf/yaml/test.yaml"], ) return config.generate()
def test_yaml_file_writer_no_path(self, monkeypatch): """Test the YAML writer works correctly""" with monkeypatch.context() as m: m.setattr(sys, "argv", ["", "--config", "./tests/conf/yaml/test.yaml"]) with pytest.raises(ValueError): config = ConfigArgBuilder( *all_configs, desc="Test Builder", ) # Test the chained version config.save(file_extension=".yaml", file_name="pytest").generate()
def test_json_file_writer(self, monkeypatch, tmp_path): """Check JSON writer works correctly""" with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/json/test.json']) config = ConfigArgBuilder(TypeConfig, TypeOptConfig, desc='Test Builder') # Test the chained version config.save(user_specified_path=tmp_path, file_extension='.json').generate() check_path = str(tmp_path) + '/*.json' fname = glob.glob(check_path)[0] with open(fname, 'r') as fin: print(fin.read()) assert len(list(tmp_path.iterdir())) == 1
def test_dynamic_raise(self, monkeypatch): with monkeypatch.context() as m: m.setattr(sys, "argv", [""]) with pytest.raises(_SpockUndecoratedClass): @spock class TestConfigDefaultsFail(Foo, Bar): x: int = 235 y: str = 'yarghhh' z: List[int] = [10, 20] config = ConfigArgBuilder(TestConfigDefaultsFail) return config.generate()