-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from meaningfy-ws/feature/MWB-271
Upgrade state manager component
- Loading branch information
Showing
6 changed files
with
76 additions
and
28 deletions.
There are no files selected for viewing
47 changes: 41 additions & 6 deletions
47
mapping_workbench/backend/database/adapters/gridfs_storage.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,76 @@ | ||
from io import BytesIO | ||
import gzip | ||
from typing import Optional | ||
|
||
from motor.motor_asyncio import AsyncIOMotorGridFSBucket, AsyncIOMotorDatabase | ||
|
||
|
||
class AsyncGridFSStorage: | ||
""" | ||
This class is a wrapper for the AsyncIOMotorGridFSBucket class. | ||
""" | ||
_mongo_database: AsyncIOMotorDatabase = None | ||
|
||
@classmethod | ||
def set_mongo_database(cls, mongo_database: AsyncIOMotorDatabase): | ||
""" | ||
Sets the mongo database to use for the gridfs storage. | ||
:param mongo_database: The mongo database to use for the gridfs storage. | ||
:return: None | ||
""" | ||
cls._mongo_database = mongo_database | ||
|
||
@classmethod | ||
def get_mongo_database(cls) -> AsyncIOMotorDatabase: | ||
""" | ||
Gets the mongo database to use for the gridfs storage. | ||
:return: The mongo database to use for the gridfs storage. | ||
""" | ||
if cls._mongo_database is None: | ||
from mapping_workbench.backend.database.adapters.mongodb import DB | ||
cls._mongo_database = DB.get_database() | ||
return cls._mongo_database | ||
|
||
@classmethod | ||
async def upload_file(cls, file_id: str, file_content: str): | ||
async def upload_file(cls, file_name: str, file_content: str) -> str: | ||
""" | ||
Uploads a file to the gridfs storage. | ||
:param file_name: The name of the file to upload. | ||
:param file_content: The content of the file to upload. | ||
:return: The id of the uploaded file. | ||
""" | ||
mongo_db = cls.get_mongo_database() | ||
grid_fs = AsyncIOMotorGridFSBucket(mongo_db) | ||
compressed_data = gzip.compress(file_content.encode("utf-8")) | ||
await grid_fs.upload_from_stream(file_id, compressed_data) | ||
file_id = await grid_fs.upload_from_stream(file_name, compressed_data) | ||
return file_id | ||
|
||
@classmethod | ||
async def download_file(cls, file_id: str) -> str: | ||
async def download_file(cls, file_id: str) -> Optional[str]: | ||
""" | ||
Downloads a file from the gridfs storage. | ||
:param file_id: The id of the file to download. | ||
:return: The content of the downloaded file. | ||
""" | ||
mongo_db = cls.get_mongo_database() | ||
grid_fs = AsyncIOMotorGridFSBucket(mongo_db) | ||
tmp_stream = BytesIO() | ||
await grid_fs.download_to_stream_by_name(file_id, tmp_stream) | ||
compressed_data = tmp_stream.read() | ||
return gzip.decompress(compressed_data).decode("utf-8") | ||
try: | ||
await grid_fs.download_to_stream(file_id, tmp_stream) | ||
compressed_data = tmp_stream.getvalue() | ||
result_data = gzip.decompress(compressed_data).decode("utf-8") | ||
except Exception: | ||
result_data = None | ||
tmp_stream.close() | ||
return result_data | ||
|
||
@classmethod | ||
async def delete_file(cls, file_id: str): | ||
""" | ||
Deletes a file from the gridfs storage. | ||
:param file_id: The id of the file to delete. | ||
:return: None | ||
""" | ||
mongo_db = cls.get_mongo_database() | ||
grid_fs = AsyncIOMotorGridFSBucket(mongo_db) | ||
await grid_fs.delete(file_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pytest | ||
from gridfs import NoFile | ||
from mongomock_motor import enabled_gridfs_integration | ||
|
||
from mapping_workbench.backend.state_manager.services.object_state_manager import save_object_state, load_object_state, \ | ||
delete_object_state | ||
from tests.fakes.fake_state_object import FakeObjectState | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_object_state_manager(): | ||
with enabled_gridfs_integration(): | ||
fake_object_state = FakeObjectState(name="Test1", object_data="Test2") | ||
fake_object_state_id = await save_object_state(fake_object_state) | ||
new_fake_object_state = await load_object_state(fake_object_state_id, FakeObjectState) | ||
assert new_fake_object_state.name == fake_object_state.name | ||
assert new_fake_object_state.object_data == fake_object_state.object_data | ||
await delete_object_state(fake_object_state_id) | ||
with pytest.raises(NoFile): | ||
await delete_object_state(fake_object_state_id) | ||
new_fake_object_state = await load_object_state(fake_object_state_id, FakeObjectState) | ||
assert new_fake_object_state is None |