-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description ### Context In order to improve the system we'll go back to using a database that can be accessed easily from different places. The previous Sqlite3 database will be used just as "cache" on the bot, while MongoDB will be set up to be used widely. ### This diff * Add handler to interact with the database * Add Guild Model again on MongoDB # Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update # How Has This Been Tested? Locally tested and added unit tests # Checklist: - [x] I have commented my code, particularly in hard-to-understand areas - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works
- Loading branch information
Showing
10 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
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
Empty file.
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,29 @@ | ||
from mongoengine import DoesNotExist | ||
|
||
from otter_welcome_buddy.database.models.external.guild_model import GuildModel | ||
|
||
|
||
class DbGuildHandler: | ||
"""Class to interact with the table guild via static methods""" | ||
|
||
@staticmethod | ||
def get_guild(guild_id: int) -> GuildModel | None: | ||
"""Static method to get a guild by its id""" | ||
try: | ||
guild_model: GuildModel = GuildModel.objects(guild_id=guild_id).get() | ||
return guild_model | ||
except DoesNotExist: | ||
return None | ||
|
||
@staticmethod | ||
def insert_guild(guild_model: GuildModel) -> GuildModel: | ||
"""Static method to insert a guild record""" | ||
guild_model = guild_model.save() | ||
return guild_model | ||
|
||
@staticmethod | ||
def delete_guild(guild_id: int) -> None: | ||
"""Static method to delete an interview match record by a guild_id""" | ||
guild_model: GuildModel | None = GuildModel.objects(guild_id=guild_id).first() | ||
if guild_model: | ||
guild_model.delete() |
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions
14
otter_welcome_buddy/database/models/external/guild_model.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from mongoengine import Document | ||
from mongoengine import IntField | ||
|
||
|
||
class GuildModel(Document): | ||
""" | ||
A model that represents a guild (server) in the database. | ||
Attributes: | ||
guild_id (int): The identifier for the guild, is taken from discord records and is | ||
the primary key of the object | ||
""" | ||
|
||
guild_id = IntField(primary_key=True, required=True) |
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 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,80 @@ | ||
import pytest | ||
from mongoengine import DoesNotExist | ||
from mongoengine import ValidationError | ||
from mongomock import MongoClient | ||
|
||
from otter_welcome_buddy.database.handlers.db_guild_handler import DbGuildHandler | ||
from otter_welcome_buddy.database.models.external.guild_model import GuildModel | ||
|
||
|
||
def test_get_guild_succeed(temporary_mongo_connection: MongoClient) -> None: | ||
# Arrange | ||
mocked_guild_id: int = 123 | ||
mocked_guild_model: GuildModel = GuildModel( | ||
guild_id=mocked_guild_id, | ||
) | ||
mocked_guild_model.save() | ||
|
||
# Act | ||
result = DbGuildHandler.get_guild(guild_id=mocked_guild_id) | ||
|
||
# Assert | ||
assert result is not None | ||
assert result.id == mocked_guild_id | ||
|
||
|
||
def test_get_guild_not_found(temporary_mongo_connection: MongoClient) -> None: | ||
# Act | ||
result = DbGuildHandler.get_guild(guild_id=123) | ||
|
||
# Assert | ||
assert result is None | ||
|
||
|
||
def test_insert_guild_succeed(temporary_mongo_connection: MongoClient) -> None: | ||
# Arrange | ||
mocked_guild_id: int = 123 | ||
mocked_guild_model: GuildModel = GuildModel( | ||
guild_id=mocked_guild_id, | ||
) | ||
|
||
# Act | ||
result = DbGuildHandler.insert_guild(guild_model=mocked_guild_model) | ||
|
||
# Assert | ||
assert result is not None | ||
assert result.id == mocked_guild_id | ||
|
||
|
||
def test_insert_guild_failed(temporary_mongo_connection: MongoClient) -> None: | ||
# Arrange | ||
mocked_guild_model: GuildModel = GuildModel() | ||
|
||
# Act / Assert | ||
with pytest.raises(ValidationError): | ||
DbGuildHandler.insert_guild(guild_model=mocked_guild_model) | ||
|
||
|
||
def test_delete_guild_valid_id(temporary_mongo_connection: MongoClient) -> None: | ||
# Arrange | ||
mocked_guild_id: int = 123 | ||
mocked_guild_model: GuildModel = GuildModel( | ||
guild_id=mocked_guild_id, | ||
) | ||
mocked_guild_model.save() | ||
|
||
# Act | ||
DbGuildHandler.delete_guild(guild_id=mocked_guild_id) | ||
|
||
# Assert | ||
with pytest.raises(DoesNotExist): | ||
GuildModel.objects(guild_id=mocked_guild_id).get() | ||
|
||
|
||
def test_delete_guild_invalid_id(temporary_mongo_connection: MongoClient) -> None: | ||
# Act | ||
DbGuildHandler.delete_guild(guild_id=123) | ||
|
||
# Assert | ||
with pytest.raises(DoesNotExist): | ||
GuildModel.objects(guild_id=123).get() |