From bec893bd2064c72fe005deb1ec60139bf9a236e8 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Thu, 30 Jun 2022 21:33:37 +0200 Subject: [PATCH] Add tests for users crud --- didier/cogs/currency.py | 9 +++++-- .../test_database/test_crud/test_currency.py | 0 tests/test_database/test_crud/test_users.py | 25 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/test_database/test_crud/test_currency.py create mode 100644 tests/test_database/test_crud/test_users.py diff --git a/didier/cogs/currency.py b/didier/cogs/currency.py index 193a9ef..3283ed4 100644 --- a/didier/cogs/currency.py +++ b/didier/cogs/currency.py @@ -1,10 +1,9 @@ import discord from discord.ext import commands -from didier import Didier - from database.crud import currency as crud from database.exceptions.currency import DoubleNightly +from didier import Didier from didier.utils.discord.checks import is_owner from didier.utils.types.string import pluralize @@ -21,8 +20,13 @@ class Currency(commands.Cog): @commands.command(name="Award") @commands.check(is_owner) async def award(self, ctx: commands.Context, user: discord.User, amount: int): + """Award a user a given amount of Didier Dinks""" async with self.client.db_session as session: await crud.add_dinks(session, user.id, amount) + plural = pluralize("Didier Dink", amount) + await ctx.reply( + f"**{ctx.author.display_name}** heeft **{user.display_name}** **{amount}** {plural} geschonken." + ) await self.client.confirm_message(ctx.message) @commands.hybrid_command(name="bank") @@ -51,4 +55,5 @@ class Currency(commands.Cog): async def setup(client: Didier): + """Load the cog""" await client.add_cog(Currency(client)) diff --git a/tests/test_database/test_crud/test_currency.py b/tests/test_database/test_crud/test_currency.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_database/test_crud/test_users.py b/tests/test_database/test_crud/test_users.py new file mode 100644 index 0000000..08b4c81 --- /dev/null +++ b/tests/test_database/test_crud/test_users.py @@ -0,0 +1,25 @@ +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from database.crud import users as crud +from database.models import User + + +async def test_get_or_add_non_existing(database_session: AsyncSession): + """Test get_or_add for a user that doesn't exist""" + await crud.get_or_add(database_session, 1) + statement = select(User) + res = (await database_session.execute(statement)).scalars().all() + + assert len(res) == 1 + assert res[0].bank is not None + assert res[0].nightly_data is not None + + +async def test_get_or_add_existing(database_session: AsyncSession): + """Test get_or_add for a user that does exist""" + user = await crud.get_or_add(database_session, 1) + bank = user.bank + + assert await crud.get_or_add(database_session, 1) == user + assert (await crud.get_or_add(database_session, 1)).bank == bank