| 1 |
import time |
| 2 |
from multiprocessing import Process |
| 3 |
|
| 4 |
import httpx |
| 5 |
import pytest |
| 6 |
import uvicorn |
| 7 |
from example.main import custom_concat |
| 8 |
from rdflib import Graph |
| 9 |
|
| 10 |
# from testcontainers.core.container import DockerContainer |
| 11 |
# from testcontainers.core.waiting_utils import wait_for_logs |
| 12 |
from rdflib_endpoint import SparqlEndpoint |
| 13 |
|
| 14 |
# https://github.com/biopragmatics/curies/blob/main/tests/test_federated_sparql.py |
| 15 |
|
| 16 |
graph = Graph() |
| 17 |
|
| 18 |
|
| 19 |
def _get_app(): |
| 20 |
return SparqlEndpoint( |
| 21 |
graph=graph, |
| 22 |
functions={ |
| 23 |
"https://w3id.org/sparql-functions/custom_concat": custom_concat, |
| 24 |
}, |
| 25 |
enable_update=True, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture(scope="module") |
| 30 |
def service_url(): |
| 31 |
host = "localhost" |
| 32 |
port = 8000 |
| 33 |
service_process = Process( |
| 34 |
target=uvicorn.run, |
| 35 |
args=(_get_app,), |
| 36 |
kwargs={"host": host, "port": port, "log_level": "info"}, |
| 37 |
daemon=True, |
| 38 |
) |
| 39 |
service_process.start() |
| 40 |
time.sleep(2) |
| 41 |
endpoint_url = f"http://{host}:{port}" |
| 42 |
yield endpoint_url |
| 43 |
service_process.kill() |
| 44 |
service_process.join() |
| 45 |
|
| 46 |
|
| 47 |
# Stop and delete all testcontainers: docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) |
| 48 |
# NOTE: in case issue in rootless docker: https://github.com/testcontainers/testcontainers-python/issues/537 |
| 49 |
# TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/run/user/$(id -u)/docker.sock uv run pytest tests/test_federation.py -s |
| 50 |
|
| 51 |
# import os |
| 52 |
|
| 53 |
# env = os.environ.copy() |
| 54 |
# env["GRAPHDB_USERNAME"] = "admin" |
| 55 |
# env["GRAPHDB_PASSWORD"] = "root" |
| 56 |
|
| 57 |
# @pytest.fixture(scope="module") |
| 58 |
# def graphdb(): |
| 59 |
# """Start GraphDB container as a fixture.""" |
| 60 |
# container = DockerContainer("ontotext/graphdb:10.8.4") |
| 61 |
# container.with_exposed_ports(7200).with_bind_ports(7200, 7200) |
| 62 |
# container.with_env("JAVA_OPTS", "-Xms1g -Xmx4g") |
| 63 |
# container.start() |
| 64 |
# delay = wait_for_logs(container, "Started GraphDB") |
| 65 |
# base_url = f"http://{container.get_container_host_ip()}:{container.get_exposed_port(7200)}" |
| 66 |
|
| 67 |
# print(f"GraphDB started in {delay:.0f}s at {base_url}") |
| 68 |
# # print(container.get_logs()) |
| 69 |
# yield base_url |
| 70 |
|
| 71 |
|
| 72 |
# def test_graphdb_custom_concat(service_url, graphdb): |
| 73 |
# print(concat_select.format(rdflib_endpoint_url=service_url)) |
| 74 |
# response = httpx.get(graphdb, params={"query": concat_select.format(rdflib_endpoint_url=service_url)}, headers={"accept": "application/json"}) |
| 75 |
# print(response.text) |
| 76 |
# assert response.status_code == 200 |
| 77 |
# assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast" |
| 78 |
|
| 79 |
|
| 80 |
# @pytest.fixture(scope="module") |
| 81 |
# def blazegraph(): |
| 82 |
# """Start blazegraph container as a fixture.""" |
| 83 |
# container = DockerContainer("lyrasis/blazegraph:2.1.4") |
| 84 |
# container.with_exposed_ports(8080).with_bind_ports(8080, 8080) |
| 85 |
# # container.with_env("JAVA_OPTS", "-Xms1g -Xmx4g") |
| 86 |
# container.start() |
| 87 |
# delay = wait_for_logs(container, "Started @") |
| 88 |
# base_url = f"http://{container.get_container_host_ip()}:{container.get_exposed_port(8080)}/bigdata/namespace/kb/sparql" |
| 89 |
|
| 90 |
# print(f"Blazegraph started in {delay:.0f}s at {base_url}") |
| 91 |
# # print(container.get_logs()) |
| 92 |
# yield base_url |
| 93 |
|
| 94 |
|
| 95 |
# def test_blazegraph_custom_concat(service_url, blazegraph): |
| 96 |
# print(concat_select.format(rdflib_endpoint_url=service_url)) |
| 97 |
# response = httpx.get(blazegraph, params={"query": concat_select.format(rdflib_endpoint_url=service_url)}, headers={"accept": "application/json"}) |
| 98 |
# print(response.text) |
| 99 |
# assert response.status_code == 200 |
| 100 |
# assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast" |
| 101 |
|
| 102 |
|
| 103 |
def test_direct_custom_concat(service_url): |
| 104 |
direct_concat_select = """PREFIX myfunctions: <https://w3id.org/sparql-functions/> |
| 105 |
SELECT ?concat WHERE { |
| 106 |
BIND(myfunctions:custom_concat("First", "last") AS ?concat) |
| 107 |
}""" |
| 108 |
response = httpx.get(service_url, params={"query": direct_concat_select}, headers={"accept": "application/json"}) |
| 109 |
print(response.text) |
| 110 |
assert response.status_code == 200 |
| 111 |
assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast" |
| 112 |
|
| 113 |
|
| 114 |
concat_select = """PREFIX myfunctions: <https://w3id.org/sparql-functions/> |
| 115 |
SELECT ?concat WHERE {{ |
| 116 |
SERVICE <{rdflib_endpoint_url}> {{ |
| 117 |
BIND(myfunctions:custom_concat("First", "last") AS ?concat) |
| 118 |
}} |
| 119 |
}}""" |