yeomko22 / TIL

Today I learned
1 stars 0 forks source link

DS School - Config Test #125

Open yeomko22 opened 2 years ago

yeomko22 commented 2 years ago

test config

def test_fetch_config_structure(tmpdir):
    # Given
    # We make use of the pytest built-in tmpdir fixture
    configs_dir = Path(tmpdir)
    config_1 = configs_dir / "sample_config.yml"
    config_1.write_text(TEST_CONFIG_TEXT)
    parsed_config = fetch_config_from_yaml(cfg_path=config_1)

    # When
    config = create_and_validate_config(parsed_config=parsed_config)

    # Then
    assert config.model_config
    assert config.app_config

def test_config_validation_raises_error_for_invalid_config(tmpdir):
    # Given
    # We make use of the pytest built-in tmpdir fixture
    configs_dir = Path(tmpdir)
    config_1 = configs_dir / "sample_config.yml"

    # invalid config attempts to set a prohibited loss
    # function which we validate against an allowed set of
    # loss function parameters.
    config_1.write_text(INVALID_TEST_CONFIG_TEXT)
    parsed_config = fetch_config_from_yaml(cfg_path=config_1)

    # When
    with pytest.raises(ValidationError) as excinfo:
        create_and_validate_config(parsed_config=parsed_config)

    # Then
    assert "not in the allowed set" in str(excinfo.value)
yeomko22 commented 2 years ago

test input data

def test_validate_inputs_identifies_errors(sample_input_data):

Given

test_inputs = sample_input_data.copy()

# introduce errors
test_inputs.at[1, "BldgType"] = 50  # we expect a string

# When
validated_inputs, errors = validate_inputs(input_data=test_inputs)

# Then
assert errors
assert errors[1] == {"BldgType": ["Not a valid string."]}