def get_entry_info(request: Request, entry: str) -> EntryInfoResponse:
    from optimade.models import EntryInfoResource

    valid_entry_info_endpoints = ENTRY_INFO_SCHEMAS.keys()
    if entry not in valid_entry_info_endpoints:
        raise StarletteHTTPException(
            status_code=404,
            detail=f"Entry info not found for {entry}, valid entry info endpoints are: {', '.join(valid_entry_info_endpoints)}",
        )

    schema = ENTRY_INFO_SCHEMAS[entry]()
    queryable_properties = {"id", "type", "attributes"}
    properties = retrieve_queryable_properties(
        schema, queryable_properties, entry_type=entry
    )

    output_fields_by_format = {"json": list(properties.keys())}

    return EntryInfoResponse(
        meta=meta_values(request.url, 1, 1, more_data_available=False),
        data=EntryInfoResource(
            formats=list(output_fields_by_format.keys()),
            description=schema.get("description", "Entry Resources"),
            properties=properties,
            output_fields_by_format=output_fields_by_format,
        ),
    )
def test_schemas():
    """Test that the default `ENTRY_INFO_SCHEMAS` contain
    all the required information about the OPTIMADE properties
    after dereferencing.

    """
    for entry in ("Structures", "References"):
        schema = ENTRY_INFO_SCHEMAS[entry.lower()]()

        top_level_props = ("id", "type", "attributes")
        properties = retrieve_queryable_properties(schema, top_level_props)

        fields = list(
            schema["definitions"][f"{entry[:-1]}ResourceAttributes"][
                "properties"
            ].keys()
        )
        fields += ["id", "type"]

        # Check all fields are present
        assert all(field in properties for field in fields)

        # Check that there are no references to definitions remaining
        assert "$ref" not in properties
        assert not any("$ref" in properties[field] for field in properties)

        # Check that all expected keys are present for OPTIMADE fields
        for key in ("type", "sortable", "queryable", "description"):
            assert all(key in properties[field] for field in properties)
def test_provider_field_schemas():
    """Tests that the default configured provider fields that have descriptions
    are dereferenced appropriately.

    """
    entry = "structures"
    test_field = "chemsys"
    schema = ENTRY_INFO_SCHEMAS[entry]()
    top_level_props = ("id", "type", "attributes")
    properties = retrieve_queryable_properties(schema, top_level_props, entry)
    name = f"_exmpl_{test_field}"

    assert name in properties
    assert properties[name] == {
        "type": "string",
        "description": "A string representing the chemical system in an ordered fashion",
        "sortable": True,
    }
Exemplo n.º 4
0
async def aretrieve_queryable_properties(
    schema: "Dict[str, Any]", queryable_properties: "Iterable"
) -> dict:
    """Asynchronous implementation of `retrieve_queryable_properties()` from `optimade`

    Reference to the function in the `optimade` API documentation:
    [`retrieve_queryable_properties()`](https://www.optimade.org/optimade-python-tools/api_reference/server/schemas/#optimade.server.schemas.retrieve_queryable_properties).

    Recursively loops through the schema of a pydantic model and resolves all references,
    returning a dictionary of all the OPTIMADE-queryable properties of that model.

    Parameters:
        schema: The schema of the pydantic model.
        queryable_properties: The list of properties to find in the schema.

    Returns:
        A flat dictionary with properties as keys, containing the field description, unit,
        sortability, support level, queryability and type, where provided.

    """
    return retrieve_queryable_properties(
        schema=schema,
        queryable_properties=queryable_properties,
    )
Exemplo n.º 5
0
    def ENTRY_RESOURCE_ATTRIBUTES(cls) -> Dict[str, Any]:
        """Returns the dictionary of attributes defined by the underlying entry resource class."""
        from optimade.server.schemas import retrieve_queryable_properties

        return retrieve_queryable_properties(cls.ENTRY_RESOURCE_CLASS.schema())
Exemplo n.º 6
0
    ValidatorLinksResponse,
    ValidatorReferenceResponseOne,
    ValidatorReferenceResponseMany,
    ValidatorStructureResponseOne,
    ValidatorStructureResponseMany,
)
from optimade.server.mappers import BaseResourceMapper
from optimade.server.schemas import (
    ENTRY_INFO_SCHEMAS,
    retrieve_queryable_properties,
)

__all__ = ("ValidatorConfig", "VALIDATOR_CONFIG")

_ENTRY_SCHEMAS = {
    endp: retrieve_queryable_properties(ENTRY_INFO_SCHEMAS[endp](),
                                        ("id", "type", "attributes"))
    for endp in ENTRY_INFO_SCHEMAS
}

_ENTRY_ENDPOINTS = ("structures", "references", "calculations")

_NON_ENTRY_ENDPOINTS = ("info", "links", "versions")

_RESPONSE_CLASSES = {
    "references": ValidatorReferenceResponseMany,
    "references/": ValidatorReferenceResponseOne,
    "structures": ValidatorStructureResponseMany,
    "structures/": ValidatorStructureResponseOne,
    "info": InfoResponse,
    "links": ValidatorLinksResponse,
}