Exemplo n.º 1
0
def test_set_spec_context(_clean_remote_schemas_store):
    """
    GIVEN spec context
    WHEN set_spec_context is called
    THEN the RemoteSchemaStore has the context.
    """
    # pylint: disable=protected-access

    ref_helper.set_context(path="path1")

    assert ref_helper._remote_schema_store.spec_context == "path1"
Exemplo n.º 2
0
def test_remote_ref(tmp_path, _clean_remote_schemas_store):
    """
    GIVEN remote $ref that is not normalized and file with the remote schemas
    WHEN get_remote_ref is called with the $ref
    THEN the schemas are stored under the normalized path.
    """
    # Create file
    directory = tmp_path / "base"
    directory.mkdir()
    schemas_file = directory / "original.json"
    remote_schemas_file = directory / "remote.json"
    remote_schemas_file.write_text('{"Table": {"key": "value"}}')
    # Set up remote schemas store
    ref.set_context(path=str(schemas_file))
    schemas = {"RefTable": {"$ref": "remote.json#/Table"}}
    model_factory = mock.MagicMock()

    define_all.define_all(model_factory=model_factory, schemas=schemas)
Exemplo n.º 3
0
def test_constructable_remote(tmp_path, _clean_remote_schemas_store):
    """
    GIVEN schema with remote $ref with x-tablename
    WHEN constructable is called with the schema
    THEN True 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('{"Table": {"x-tablename": "table 1"}}')
    # Set up remote schemas store
    ref_helper.set_context(path=str(schemas_file))
    schema = {"$ref": "remote.json#/Table"}

    result = schema_helper.constructable(schema=schema, schemas={})

    assert result is True
Exemplo n.º 4
0
def test_get_remote_ref_ref(tmp_path, _clean_remote_schemas_store):
    """
    GIVEN remote $ref and file with the remote schemas
    WHEN get_remote_ref 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": {"$ref": "#/Schema2"}}')
    # Set up remote schemas store
    ref_helper.set_context(path=str(schemas_file))
    # Calculate $ref
    ref = "remote.json#/Schema1"

    name, schema = ref_helper.get_remote_ref(ref=ref)

    assert schema == {"$ref": "remote.json#/Schema2"}
    assert name == "Schema1"
Exemplo n.º 5
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.º 6
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"
Exemplo n.º 7
0
def test_get_remote_ref_norm(tmp_path, _clean_remote_schemas_store):
    """
    GIVEN remote $ref that is not normalized and file with the remote schemas
    WHEN get_remote_ref is called with the $ref
    THEN the schemas are stored under the normalized path.
    """
    # pylint: disable=protected-access
    # Create file
    directory = tmp_path / "base"
    directory.mkdir()
    sub_directory = directory / "subdir"
    sub_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
    ref = "subdir/../remote.json#/Schema1"

    ref_helper.get_remote_ref(ref=ref)

    assert "remote.json" in ref_helper._remote_schema_store._schemas