Add tests for users crud

pull/118/head
stijndcl 2022-06-30 21:33:37 +02:00
parent 032b636b02
commit bec893bd20
3 changed files with 32 additions and 2 deletions

View File

@ -1,10 +1,9 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from didier import Didier
from database.crud import currency as crud from database.crud import currency as crud
from database.exceptions.currency import DoubleNightly from database.exceptions.currency import DoubleNightly
from didier import Didier
from didier.utils.discord.checks import is_owner from didier.utils.discord.checks import is_owner
from didier.utils.types.string import pluralize from didier.utils.types.string import pluralize
@ -21,8 +20,13 @@ class Currency(commands.Cog):
@commands.command(name="Award") @commands.command(name="Award")
@commands.check(is_owner) @commands.check(is_owner)
async def award(self, ctx: commands.Context, user: discord.User, amount: int): 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: async with self.client.db_session as session:
await crud.add_dinks(session, user.id, amount) 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) await self.client.confirm_message(ctx.message)
@commands.hybrid_command(name="bank") @commands.hybrid_command(name="bank")
@ -51,4 +55,5 @@ class Currency(commands.Cog):
async def setup(client: Didier): async def setup(client: Didier):
"""Load the cog"""
await client.add_cog(Currency(client)) await client.add_cog(Currency(client))

View File

@ -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