|
| 1 | +import httpx |
1 | 2 | import pytest |
2 | 3 |
|
| 4 | +from diffbot import Diffbot, DiffbotAsync |
| 5 | + |
| 6 | + |
| 7 | +""" |
| 8 | +Endpoint configuration |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +def test_dql_defaults_to_public_endpoint(): |
| 13 | + def handler(request: httpx.Request) -> httpx.Response: |
| 14 | + assert str(request.url).startswith("https://kg.diffbot.com/kg/v3/dql") |
| 15 | + return httpx.Response(200, json={"data": []}) |
| 16 | + |
| 17 | + db = Diffbot(token="test-token", transport=httpx.MockTransport(handler)) |
| 18 | + db.dql("type:Organization") |
| 19 | + |
| 20 | + |
| 21 | +def test_dql_honors_custom_dql_url(): |
| 22 | + def handler(request: httpx.Request) -> httpx.Response: |
| 23 | + assert str(request.url).startswith("http://localhost:8080/kg/v3/dql") |
| 24 | + return httpx.Response(200, json={"data": []}) |
| 25 | + |
| 26 | + db = Diffbot( |
| 27 | + token="test-token", |
| 28 | + dql_url="http://localhost:8080/kg/v3/dql", |
| 29 | + transport=httpx.MockTransport(handler), |
| 30 | + ) |
| 31 | + db.dql("type:Organization") |
| 32 | + |
| 33 | + |
| 34 | +def test_dql_parallel_honors_custom_dql_url(): |
| 35 | + def handler(request: httpx.Request) -> httpx.Response: |
| 36 | + assert str(request.url).startswith("http://localhost:8080/kg/v3/dql") |
| 37 | + return httpx.Response(200, json={"hits": 1}) |
| 38 | + |
| 39 | + db = Diffbot( |
| 40 | + token="test-token", |
| 41 | + dql_url="http://localhost:8080/kg/v3/dql", |
| 42 | + transport=httpx.MockTransport(handler), |
| 43 | + ) |
| 44 | + results = db.dql_parallel([{"query": "type:Organization", "size": 0}] * 2) |
| 45 | + assert results == [{"hits": 1}, {"hits": 1}] |
| 46 | + |
| 47 | + |
| 48 | +def test_ontology_honors_custom_ontology_url(): |
| 49 | + def handler(request: httpx.Request) -> httpx.Response: |
| 50 | + assert str(request.url) == "http://localhost:8080/kg/ontology" |
| 51 | + return httpx.Response(200, json={"types": {"Organization": {"fields": {}}}}) |
| 52 | + |
| 53 | + db = Diffbot( |
| 54 | + token="test-token", |
| 55 | + ontology_url="http://localhost:8080/kg/ontology", |
| 56 | + transport=httpx.MockTransport(handler), |
| 57 | + ) |
| 58 | + assert db.dql_fetch_ontology().types() == ["Organization"] |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.anyio |
| 62 | +async def test_async_dql_honors_custom_dql_url(): |
| 63 | + def handler(request: httpx.Request) -> httpx.Response: |
| 64 | + assert str(request.url).startswith("http://localhost:8080/kg/v3/dql") |
| 65 | + return httpx.Response(200, json={"data": []}) |
| 66 | + |
| 67 | + db = DiffbotAsync( |
| 68 | + token="test-token", |
| 69 | + dql_url="http://localhost:8080/kg/v3/dql", |
| 70 | + transport=httpx.MockTransport(handler), |
| 71 | + ) |
| 72 | + await db.dql("type:Organization") |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.anyio |
| 76 | +async def test_async_ontology_honors_custom_ontology_url(): |
| 77 | + def handler(request: httpx.Request) -> httpx.Response: |
| 78 | + assert str(request.url) == "http://localhost:8080/kg/ontology" |
| 79 | + return httpx.Response(200, json={"types": {"Person": {"fields": {}}}}) |
| 80 | + |
| 81 | + db = DiffbotAsync( |
| 82 | + token="test-token", |
| 83 | + ontology_url="http://localhost:8080/kg/ontology", |
| 84 | + transport=httpx.MockTransport(handler), |
| 85 | + ) |
| 86 | + ont = await db.dql_fetch_ontology() |
| 87 | + assert ont.types() == ["Person"] |
| 88 | + |
3 | 89 |
|
4 | 90 | """ |
5 | 91 | Live |
|
0 commit comments