|
| 1 | +# test_subapp_mounts.py |
| 2 | +import pytest |
| 3 | +from fastapi import FastAPI |
| 4 | +from fastapi.testclient import TestClient |
| 5 | + |
| 6 | +from optimade.server.config import ServerConfig |
| 7 | +from optimade.server.create_app import create_app |
| 8 | + |
| 9 | +CONFIG = ServerConfig() |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mounted_client(): |
| 14 | + """Mount 3 Optimade APIs with different Mongo databases.""" |
| 15 | + parent_app = FastAPI() |
| 16 | + |
| 17 | + base_url = "http://testserver" |
| 18 | + |
| 19 | + conf1 = ServerConfig() |
| 20 | + conf1.base_url = f"{base_url}/app1" |
| 21 | + conf1.mongo_database = "optimade_1" |
| 22 | + conf1.provider = { |
| 23 | + "name": "Example 1", |
| 24 | + "description": "Example 1", |
| 25 | + "prefix": "example1", |
| 26 | + } |
| 27 | + app1 = create_app(conf1) |
| 28 | + parent_app.mount("/app1", app1) |
| 29 | + |
| 30 | + conf2 = ServerConfig() |
| 31 | + conf2.base_url = f"{base_url}/app2" |
| 32 | + conf2.mongo_database = "optimade_2" |
| 33 | + conf2.provider = { |
| 34 | + "name": "Example 2", |
| 35 | + "description": "Example 2", |
| 36 | + "prefix": "example2", |
| 37 | + } |
| 38 | + app2 = create_app(conf2) |
| 39 | + parent_app.mount("/app2", app2) |
| 40 | + |
| 41 | + conf3 = ServerConfig() |
| 42 | + conf3.base_url = f"{base_url}/idx" |
| 43 | + conf3.mongo_database = "optimade_idx" |
| 44 | + app3 = create_app(conf3, index=True) |
| 45 | + parent_app.mount("/idx", app3) |
| 46 | + |
| 47 | + return TestClient(parent_app) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.skipif( |
| 51 | + CONFIG.database_backend.value not in ("mongomock", "mongodb"), |
| 52 | + reason="Requires db-specific config, only MongoDB currently supported.", |
| 53 | +) |
| 54 | +def test_subapps(mounted_client): |
| 55 | + for app in ["app1", "app2"]: |
| 56 | + r = mounted_client.get(f"/{app}/structures") |
| 57 | + assert r.status_code == 200, f"API not reachable for /{app}" |
| 58 | + response = r.json() |
| 59 | + # Make sure we get at least 10 structures: |
| 60 | + assert len(response["data"]) > 10 |
| 61 | + |
| 62 | + # Make sure the custom providers were picked up: |
| 63 | + prefix = response["meta"]["provider"]["prefix"] |
| 64 | + if app == "app1": |
| 65 | + assert prefix == "example1" |
| 66 | + if app == "app2": |
| 67 | + assert prefix == "example2" |
| 68 | + |
| 69 | + # the index, make sure links are accessible |
| 70 | + r = mounted_client.get("/idx/links") |
| 71 | + assert r.status_code == 200, "API not reachable for /idx" |
| 72 | + response = r.json() |
| 73 | + assert len(response["data"]) > 5 |
0 commit comments