def test_transform_list():
    schema = schemas.Schema(JSON_SCHEMA)
    data = [True, 1, 'test']
    data = schema.transform(data)
    assert isinstance(data, np.ndarray)
    assert data.dtype == np.object
    assert data.shape == (3, )
def test_validate_dict():
    schema = schemas.Schema(JSON_SCHEMA)
    data = {
        "contact_has_telephone": True,
        "credit_amount": 1,
        "credit_purpose": "test"
    }
    schema.validate(data)
def test_validate_dict_wrong_types():
    schema = schemas.Schema(JSON_SCHEMA)
    data = {
        "contact_has_telephone": "test",
        "credit_amount": 1,
        "credit_purpose": True
    }
    with pytest.raises(ValidationError):
        schema.validate(data)
def test_validate_dict_extra_param():
    schema = schemas.Schema(JSON_SCHEMA)
    data = {
        "contact_has_telephone": "test",
        "credit_amount": 1,
        "credit_purpose": True,
        "extra_param": 123.456
    }
    with pytest.raises(ValidationError):
        schema.validate(data)
def test_transform_dict():
    schema = schemas.Schema(JSON_SCHEMA)
    data = {
        "credit_purpose": "test",
        "credit_amount": 1,
        "contact_has_telephone": True
    }
    data = schema.transform(data)
    assert isinstance(data, np.ndarray)
    assert data.dtype == np.object
    assert data.shape == (3, )
    assert data[0] == True  # noqa
    assert data[1] == 1
    assert data[2] == "test"
def transform_schema(preprocessor, data_schema):
    num_idx, num_features = preprocess_numerical_schema(preprocessor, data_schema)  # noqa
    cat_idx, cat_features = preprocess_categorical_schema(preprocessor, data_schema)  # noqa
    assert num_idx < cat_idx, "Ordering should be numerical, then categorical."
    features = num_features + cat_features

    array_schema = {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "array",
        "minItems": len(features),
        "maxItems": len(features),
        "items": features,
        "title": data_schema.title,
        "description": data_schema.description.replace(
            "items", "features"
        ),
    }
    return schemas.Schema(array_schema)
def get_table_schema(database_name, table_name):
    glue_client = boto3.client("glue", region_name=config.AWS_REGION
                               )  # use client, since no resource for glue.
    glue_table = glue_client.get_table(DatabaseName=database_name,
                                       Name=table_name)
    glue_table_schema = glue_table["Table"]["StorageDescriptor"]["Columns"]
    items = []
    for glue_column in glue_table_schema:
        item = {
            "title": glue_column["Name"],
            "type": GLUE_TO_JSON_TYPES[glue_column["Type"]],
        }
        items.append(item)
    array_schema = {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "array",
        "minItems": len(items),
        "maxItems": len(items),
        "items": items,
    }
    return schemas.Schema(array_schema)
def test_validate_list_too_short():
    schema = schemas.Schema(JSON_SCHEMA)
    data = [True, 1]
    with pytest.raises(ValidationError):
        schema.validate(data)
def test_validate_list_too_long():
    schema = schemas.Schema(JSON_SCHEMA)
    data = [True, 1, 'test', 123.456]
    with pytest.raises(ValidationError):
        schema.validate(data)
def test_validate_list_wrong_types():
    schema = schemas.Schema(JSON_SCHEMA)
    data = ['test', 1, True]
    with pytest.raises(ValidationError):
        schema.validate(data)
def test_validate_list():
    schema = schemas.Schema(JSON_SCHEMA)
    data = [True, 1, 'test']
    schema.validate(data)
def test_create_schema():
    schema = schemas.Schema(JSON_SCHEMA)
    assert isinstance(schema, schemas.Schema)
def test_validate_dict_missing_param():
    schema = schemas.Schema(JSON_SCHEMA)
    data = {"contact_has_telephone": True, "credit_amount": 1}
    with pytest.raises(ValidationError):
        schema.validate(data)