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

Non Local Mongo #150

Merged
merged 5 commits into from
Jan 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions optimade/server/config.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[BACKEND]
USE_REAL_MONGO = no
MONGO_DATABASE = optimade
MONGO_URI = localhost:27017
LINKS_COLLECTION = links
REFERENCES_COLLECTION = references
STRUCTURES_COLLECTION = structures
Expand Down
5 changes: 5 additions & 0 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def _DEFAULTS(field: str) -> Any:
res = {
"use_real_mongo": False,
"mongo_database": "optimade",
"mongo_uri": "localhost:27017",
"links_collection": "links",
"references_collection": "references",
"structures_collection": "structures",
Expand Down Expand Up @@ -136,6 +137,9 @@ def load_from_ini(self):
self.mongo_database = config.get(
"BACKEND", "MONGO_DATABASE", fallback=self._DEFAULTS("mongo_database")
)
self.mongo_uri = config.get(
"BACKEND", "MONGO_URI", fallback=self._DEFAULTS("mongo_uri")
)

self.page_limit = config.getint(
"SERVER", "PAGE_LIMIT", fallback=self._DEFAULTS("page_limit")
Expand Down Expand Up @@ -195,6 +199,7 @@ def load_from_json(self):
self.mongo_database = config.get(
"mongo_database", self._DEFAULTS("mongo_database")
)
self.mongo_uri = config.get("mongo_uri", self._DEFAULTS("mongo_uri"))
for endpoint in {"links", "references", "structures"}:
setattr(
self,
Expand Down
4 changes: 3 additions & 1 deletion optimade/server/entry_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

if CONFIG.use_real_mongo:
from pymongo import MongoClient

client = MongoClient(CONFIG.mongo_uri)
else:
from mongomock import MongoClient

client = MongoClient()
client = MongoClient()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not a possibility in mongomock? Or maybe it just doesn't make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually works fine as far as code goes, but it's probably not guaranteed behavior, so it doesn't hurt to separate the two unless we're getting issues elsewhere like linting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was merely for the elegance of the code as it was - only having one line for the instantiation of the client - but never mind.



class EntryCollection(Collection): # pylint: disable=inherit-non-class
Expand Down
1 change: 1 addition & 0 deletions tests/server/config_test.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[BACKEND]
USE_REAL_MONGO = no
MONGO_DATABASE = optimade
MONGO_URI = localhost:27017
LINKS_COLLECTION = links
REFERENCES_COLLECTION = references
STRUCTURES_COLLECTION = structures
Expand Down
1 change: 1 addition & 0 deletions tests/server/config_test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"use_real_mongo": false,
"mongo_database": "optimade",
"mongo_uri": "localhost:27017",
"links_collection": "links",
"references_collection": "references",
"structures_collection": "structures",
Expand Down