Exemplo n.º 1
0
def test_resolve_error(schema, schemas, exception):
    """
    GIVEN schema and schemas that are not valid
    WHEN resolve is called with the schema
    THEN the expected exception is raised.
    """
    with pytest.raises(exception):
        ref_helper.resolve(name="name 1", schema=schema, schemas=schemas)
Exemplo n.º 2
0
def test_resolve_valid(schema, schemas, expected_name):
    """
    GIVEN schema, schemas and expected name
    WHEN resolve is called with the schema, schemas and name
    THEN the schema and expected name are returned.
    """
    name = "Schema"

    (return_name, return_schema) = ref_helper.resolve(name=name,
                                                      schema=schema,
                                                      schemas=schemas)

    assert return_name == expected_name
    assert return_schema == {"type": "integer"}
Exemplo n.º 3
0
def test_resolve_valid_skip(schema, schemas, expected_name, expected_schema):
    """
    GIVEN schema, schemas and schema name to skip
    WHEN resolve is called with the schema, schemas, name and schema to skip
    THEN an empty schema is returned.
    """
    name = "Schema"
    skip_name = "RefSchema"

    (return_name, return_schema) = ref_helper.resolve(name=name,
                                                      schema=schema,
                                                      schemas=schemas,
                                                      skip_name=skip_name)

    assert return_name == expected_name
    assert return_schema == expected_schema
Exemplo n.º 4
0
def test_resolve_remote_url(mocked_urlopen, _clean_remote_schemas_store):
    """
    GIVEN remote $ref and urlopen with the remote schemas
    WHEN resolve is called with the $ref
    THEN the remote schema is returned.
    """
    # Defining returned data
    response_cm = mock.MagicMock()
    response_cm.getcode.return_value = 200
    response_cm.read.return_value = '{"Schema1": {"key": "value"}}'
    response_cm.__enter__.return_value = response_cm
    mocked_urlopen.return_value = response_cm
    # Set up remote schemas store
    ref_helper.set_context(path="path1")
    # Calculate $ref
    schema = {"$ref": "http://host.com/remote.json#/Schema1"}

    returned_name, returned_schema = ref_helper.resolve(name="name 1",
                                                        schema=schema,
                                                        schemas={})

    assert returned_schema == {"key": "value"}
    assert returned_name == "Schema1"
Exemplo n.º 5
0
def test_resolve_remote(tmp_path, _clean_remote_schemas_store):
    """
    GIVEN remote $ref and file with the remote schemas
    WHEN resolve is called with the $ref
    THEN the remote schema is returned.
    """
    # Create file
    directory = tmp_path / "base"
    directory.mkdir()
    schemas_file = directory / "original.json"
    remote_schemas_file = directory / "remote.json"
    remote_schemas_file.write_text('{"Schema1": {"key": "value"}}')
    # Set up remote schemas store
    ref_helper.set_context(path=str(schemas_file))
    # Calculate $ref
    schema = {"$ref": "remote.json#/Schema1"}

    returned_name, returned_schema = ref_helper.resolve(name="name 1",
                                                        schema=schema,
                                                        schemas={})

    assert returned_schema == {"key": "value"}
    assert returned_name == "Schema1"