def test_get_providers():
    """Make sure valid responses are handled as expected."""
    try:
        from optimade.server.data import providers
    except ImportError:
        pytest.fail("Cannot import providers from optimade.server.data, "
                    "please initialize the `providers` submodule!")

    from optimade.server.routers.utils import get_providers, mongo_id_for_database

    side_effects = [
        mocked_providers_list_response,
        ConnectionError,
    ]

    for side_effect in side_effects:
        with mock.patch("requests.get", side_effect=side_effect):
            providers_list = [
                _ for _ in providers.get("data", []) if _["id"] != "exmpl"
            ]
            for provider in providers_list:
                provider.update({
                    "_id": {
                        "$oid":
                        mongo_id_for_database(provider["id"], provider["type"])
                    }
                })
            assert get_providers() == providers_list
示例#2
0
if CONFIG.debug:  # pragma: no cover
    LOGGER.info("DEBUG MODE")

# Load AiiDA profile
PROFILE_NAME = os.getenv("AIIDA_PROFILE")
load_profile(PROFILE_NAME)
LOGGER.debug("AiiDA Profile: %s", PROFILE_NAME)

# Load links in mongomock
LINKS_DATA = Path(__file__).parent.joinpath("data/links.json").resolve()
with open(LINKS_DATA) as handle:
    data = json.load(handle)

    processed = []
    for link in data:
        link["_id"] = {"$oid": mongo_id_for_database(link["id"], link["type"])}
        processed.append(link)

    links.LINKS.collection.insert_many(
        bson.json_util.loads(bson.json_util.dumps(processed)))

DOCS_ENDPOINT_PREFIX = f"{get_custom_base_url_path()}{BASE_URL_PREFIXES['major']}"
APP = FastAPI(
    title="OPTIMADE API for AiiDA",
    description=
    ("The [Open Databases Integration for Materials Design (OPTIMADE) consortium]"
     "(http://www.optimade.org/) aims to make materials databases inter-operational "
     "by developing a common REST API.\n\n[Automated Interactive Infrastructure "
     "and Database for Computational Science (AiiDA)](http://www.aiida.net) aims to "
     "help researchers with managing complex workflows and making them fully "
     "reproducible."),
    openapi_url=f"{BASE_URL_PREFIXES['major']}/extensions/openapi.json",
)

if not CONFIG.use_real_mongo and CONFIG.index_links_path.exists():
    import bson.json_util
    from bson.objectid import ObjectId
    from optimade.server.routers.links import links_coll
    from optimade.server.routers.utils import mongo_id_for_database, get_providers

    LOGGER.debug("Loading index links...")
    with open(CONFIG.index_links_path) as f:
        data = json.load(f)

    processed = []
    for db in data:
        db["_id"] = {"$oid": mongo_id_for_database(db["id"], db["type"])}
        processed.append(db)

    LOGGER.debug("  Inserting index links into collection from %s...",
                 CONFIG.index_links_path)
    links_coll.collection.insert_many(
        bson.json_util.loads(bson.json_util.dumps(processed)))

    LOGGER.debug(
        "  Adding Materials-Consortia providers to links from optimade.org...")
    providers = get_providers()
    for doc in providers:
        links_coll.collection.replace_one(
            filter={"_id": ObjectId(doc["_id"]["$oid"])},
            replacement=bson.json_util.loads(bson.json_util.dumps(doc)),
            upsert=True,