Exemplo n.º 1
0
    def test_meta_response(self):
        """General test for `meta` property in response"""
        assert "meta" in self.json_response
        meta_required_keys = ResponseMeta.schema()["required"]
        meta_optional_keys = list(
            set(ResponseMeta.schema()["properties"].keys()) -
            set(meta_required_keys))
        implemented_optional_keys = ["data_available", "implementation"]

        self.check_keys(meta_required_keys, self.json_response["meta"])
        self.check_keys(implemented_optional_keys, meta_optional_keys)
        self.check_keys(implemented_optional_keys, self.json_response["meta"])
Exemplo n.º 2
0
def meta_values(
    url: str,
    data_returned: int,
    data_available: int,
    more_data_available: bool,
    **kwargs,
) -> ResponseMeta:
    """Helper to initialize the meta values"""
    from optimade.models import ResponseMetaQuery

    parse_result = urllib.parse.urlparse(url)

    # To catch all (valid) variations of the version part of the URL, a regex is used
    if re.match(r"/v[0-9]+(\.[0-9]+){,2}/.*", parse_result.path) is not None:
        url_path = re.sub(r"/v[0-9]+(\.[0-9]+){,2}/", "/", parse_result.path)
    else:
        url_path = parse_result.path

    return ResponseMeta(
        query=ResponseMetaQuery(representation=f"{url_path}?{parse_result.query}"),
        api_version=f"v{__api_version__}",
        time_stamp=datetime.utcnow(),
        data_returned=data_returned,
        more_data_available=more_data_available,
        provider=CONFIG.provider,
        data_available=data_available,
        implementation=CONFIG.implementation,
        **kwargs,
    )
Exemplo n.º 3
0
def meta_values(
    url: str,
    data_returned: int,
    data_available: int,
    more_data_available: bool,
    **kwargs,
) -> ResponseMeta:
    """Helper to initialize the meta values"""
    from optimade import __api_version__
    from optimade.models import ResponseMetaQuery, Provider, Implementation

    parse_result = urllib.parse.urlparse(url)
    provider = CONFIG.provider.copy()
    provider["prefix"] = provider["prefix"][1:-1]  # Remove surrounding `_`
    return ResponseMeta(
        query=ResponseMetaQuery(
            representation=f"{parse_result.path}?{parse_result.query}"
        ),
        api_version=f"v{__api_version__}",
        time_stamp=datetime.utcnow(),
        data_returned=data_returned,
        more_data_available=more_data_available,
        provider=Provider(**provider),
        data_available=data_available,
        implementation=Implementation(**CONFIG.implementation),
        **kwargs,
    )
Exemplo n.º 4
0
    def test_meta_response(self):
        """General test for `meta` property in response"""
        assert "meta" in self.json_response
        meta_required_keys = ResponseMeta.schema()["required"]
        meta_optional_keys = list(
            set(ResponseMeta.schema()["properties"].keys()) -
            set(meta_required_keys))
        implemented_optional_keys = [
            "time_stamp",
            "data_returned",
            "provider",
            "data_available",
            "implementation",
            # TODO: These keys are not implemented in the example server implementations
            # Add them in when they are.
            # "schema",
            # "last_id",
            # "response_message",
            # "warnings",
        ]

        self.check_keys(meta_required_keys, self.json_response["meta"])
        self.check_keys(implemented_optional_keys, meta_optional_keys)
        self.check_keys(implemented_optional_keys, self.json_response["meta"])
Exemplo n.º 5
0
def meta_values(url,
                data_returned,
                data_available,
                more_data_available=False,
                **kwargs):
    """Helper to initialize the meta values"""
    parse_result = urllib.parse.urlparse(url)
    provider = CONFIG.provider.copy()
    provider["prefix"] = provider["prefix"][1:-1]  # Remove surrounding `_`
    return ResponseMeta(
        query=ResponseMetaQuery(
            representation=f"{parse_result.path}?{parse_result.query}"),
        api_version=CONFIG.version,
        time_stamp=datetime.utcnow(),
        data_returned=data_returned,
        more_data_available=more_data_available,
        provider=Provider(**provider),
        data_available=data_available,
        **kwargs,
    )
Exemplo n.º 6
0
 def test_meta_response(self):
     assert "meta" in self.json_response
     meta_required_keys = ResponseMeta.schema().get("required")
     self.check_keys(meta_required_keys, self.json_response["meta"])