Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix load_from_json #137

Merged
merged 3 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def load_from_ini(self):
self.implementation = {}
for field in Implementation.schema()["properties"]:
value_config = config.get("IMPLEMENTATION", field, fallback=None)
value_default = self._DEFAULTS(f"implementation")[field]
value_default = self._DEFAULTS("implementation")[field]
if value_config is not None:
self.implementation[field] = value_config
elif value_default is not None:
Expand Down Expand Up @@ -191,28 +191,30 @@ def load_from_json(self):
setattr(
self,
f"{endpoint}_collection",
config.get(f"{endpoint}_collection"),
getattr(self._DEFAULTS(f"{endpoint}_collection")),
config.get(
f"{endpoint}_collection", self._DEFAULTS(f"{endpoint}_collection")
),
)

self.page_limit = int(config.get("page_limit", self._DEFAULTS("page_limit")))
self.version = config.get("api_version", self._DEFAULTS("api_version"))
self.default_db = config.get("default_db", self._DEFAULTS("default_db"))

# This is done in this way, since each field is OPTIONAL
self.implementation = config.get("implementation", {})
for field in Implementation.schema()["properties"]:
value_default = self._DEFAULTS(f"implementation.{field}")
value_default = self._DEFAULTS("implementation")[field]
if field in self.implementation:
# Keep the config value
pass
elif value_default is not None:
self.implementation[field] = value_default

self.provider = config.get("provider", self._DEFAULTS("provider"))
self.provider_fields = set(
config.get("provider_fields", self._DEFAULTS("provider_fields"))
)
self.provider_fields = {}
for endpoint in {"structures", "references"}:
self.provider_fields[endpoint] = set(
config.get("provider_fields", {}).get(endpoint, [])
)


CONFIG = ServerConfig()
25 changes: 25 additions & 0 deletions tests/server/config_test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[BACKEND]
USE_REAL_MONGO = no
MONGO_DATABASE = optimade
LINKS_COLLECTION = links
REFERENCES_COLLECTION = references
STRUCTURES_COLLECTION = structures

[SERVER]
PAGE_LIMIT = 500
DEFAULT_DB = test_server

[IMPLEMENTATION]
name = Example implementation
source_url = https://github.com/Materials-Consortia/optimade-python-tools

[PROVIDER]
prefix = _exmpl_
name = Example provider
description = Provider used for examples, not to be assigned to a real database
homepage = http://example.com
index_base_url = http://localhost:5001/index/optimade

[structures]
band_gap :
_mp_chemsys :
26 changes: 26 additions & 0 deletions tests/server/config_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"use_real_mongo": false,
"mongo_database": "optimade",
"links_collection": "links",
"references_collection": "references",
"structures_collection": "structures",
"page_limit": 500,
"default_db": "test_server",
"implementation": {
"name": "Example implementation",
"source_url": "https://github.com/Materials-Consortia/optimade-python-tools"
},
"provider": {
"prefix": "_exmpl_",
"name": "Example provider",
"description": "Provider used for examples, not to be assigned to a real database",
"homepage": "http://example.com",
"index_base_url": "http://localhost:5001/index/optimade"
},
"provider_fields": {
"structures": [
"band_gap",
"_mp_chemsys"
]
}
}
3 changes: 3 additions & 0 deletions tests/server/server_test_ini.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[optimadeconfig]
CONFIG = ./config_test.ini
# INDEX_LINKS = ./index_links.json
3 changes: 3 additions & 0 deletions tests/server/server_test_json.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[optimadeconfig]
CONFIG = ./config_test.json
# INDEX_LINKS = ./index_links.json
40 changes: 40 additions & 0 deletions tests/server/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# pylint: disable=import-outside-toplevel,protected-access,pointless-statement
import unittest

from pathlib import Path


class LoadFromIniTest(unittest.TestCase):
"""Test server.config.ServerConfig.load_from_ini"""

def test_config_ini(self):
"""Invoke CONFIG using config_test.ini"""
from optimade.server.config import ServerConfig

CONFIG = ServerConfig(
server_cfg=Path(__file__).parent.joinpath("server_test_ini.cfg").resolve()
)

CONFIG.default_db # Initiate CONFIG, running load_from_json()
# _path should now be updated with the correct path to the config json file:
self.assertEqual(
CONFIG._path, Path(__file__).parent.joinpath("config_test.ini").resolve()
)


class LoadFromJsonTest(unittest.TestCase):
"""Test server.config.ServerConfig.load_from_json"""

def test_config_json(self):
"""Invoke CONFIG using config_test.json"""
from optimade.server.config import ServerConfig

CONFIG = ServerConfig(
server_cfg=Path(__file__).parent.joinpath("server_test_json.cfg").resolve()
)

CONFIG.default_db # Initiate CONFIG, running load_from_json()
# _path should now be updated with the correct path to the config json file:
self.assertEqual(
CONFIG._path, Path(__file__).parent.joinpath("config_test.json").resolve()
)