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_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 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_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_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(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') # Test the chained version config.save(file_extension='.yaml', file_name='pytest').generate()
def test_yaml_invalid_extension(self, monkeypatch, tmp_path): """Test the YAML writer fails correctly when create path isn't set""" 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 with pytest.raises(TypeError): config.save( user_specified_path=f"{str(tmp_path)}/foo.bar/fizz.buzz/", file_extension=".foo", ).generate()
def test_yaml_invalid_extension(self, monkeypatch, tmp_path): """Test the YAML writer fails correctly when create path isn't set""" with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/yaml/test.yaml']) config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') # Test the chained version with pytest.raises(ValueError): config.save( user_specified_path=f'{str(tmp_path)}/foo.bar/fizz.buzz/', file_extension='.foo').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_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_file_writer(self, monkeypatch, tmp_path): """Test the YAML 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, file_extension=".yaml").generate() check_path = f"{str(tmp_path)}/*.yaml" fname = glob.glob(check_path)[0] with open(fname, "r") as fin: print(fin.read()) assert len(list(tmp_path.iterdir())) == 1
def test_toml_file_writer(self, monkeypatch, tmp_path): """Check the TOML writer works correctly""" with monkeypatch.context() as m: m.setattr(sys, 'argv', ['', '--config', './tests/conf/toml/test.toml']) config = ConfigArgBuilder(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') # Test the chained version config.save(user_specified_path=tmp_path, file_extension='.toml').generate() check_path = f'{str(tmp_path)}/*.toml' fname = glob.glob(check_path)[0] with open(fname, 'r') as fin: print(fin.read()) assert len(list(tmp_path.iterdir())) == 1
def test_yaml_s3_mock_file_writer(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, s3_config=S3Config(session=aws_session, s3_session=s3_client), 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}") # Test the chained version config.save( user_specified_path=f"s3://{mock_s3_bucket}/{mock_s3_object}", file_extension=".yaml", file_name=f"pytest.s3save.{curr_int_time}", ).generate() yaml_regex = re.compile( fr"{mock_s3_object}pytest.s3save.{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["Key"]) for val in s3_client.list_objects( Bucket=mock_s3_bucket)["Contents"] if re.fullmatch(yaml_regex, val["Key"]) is not None ] assert len(matches) == 1 print(f"Found file at s3://{mock_s3_bucket}/{matches[0].string}")
def test_raise_tuner_sample(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}") with pytest.raises(ValueError): config_values = config.save( file_extension=".yaml", file_name=f"pytest.{curr_int_time}", user_specified_path=tmp_path, add_tuner_sample=True)
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(TypeConfig, NestedStuff, NestedListStuff, TypeOptConfig, desc='Test Builder') # Test the chained version config_values = config.save(file_extension='.yaml', file_name='pytest').generate() check_path = f'{str(config_values.TypeConfig.save_path)}/pytest.spock.cfg.yaml' fname = glob.glob(check_path)[0] with open(fname, 'r') as fin: print(fin.read()) assert os.path.exists(check_path) # Clean up if assert is good if os.path.exists(check_path): os.remove(check_path)