mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Crud & tests for custom commands
This commit is contained in:
parent
53f58eb743
commit
fd57b5a79b
11 changed files with 281 additions and 0 deletions
0
tests/test_database/test_crud/__init__.py
Normal file
0
tests/test_database/test_crud/__init__.py
Normal file
98
tests/test_database/test_crud/test_custom_commands.py
Normal file
98
tests/test_database/test_crud/test_custom_commands.py
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import custom_commands as crud
|
||||
from database.exceptions.constraints import DuplicateInsertException
|
||||
from database.models import CustomCommand, CustomCommandAlias
|
||||
|
||||
|
||||
async def test_create_command_non_existing(database_session: AsyncSession):
|
||||
"""Test creating a new command when it doesn't exist yet"""
|
||||
await crud.create_command(database_session, "name", "response")
|
||||
|
||||
commands = (await database_session.execute(select(CustomCommand))).scalars().all()
|
||||
assert len(commands) == 1
|
||||
assert commands[0].name == "name"
|
||||
|
||||
|
||||
async def test_create_command_duplicate_name(database_session: AsyncSession):
|
||||
"""Test creating a command when the name already exists"""
|
||||
await crud.create_command(database_session, "name", "response")
|
||||
|
||||
with pytest.raises(DuplicateInsertException):
|
||||
await crud.create_command(database_session, "name", "other response")
|
||||
|
||||
|
||||
async def test_create_command_name_is_alias(database_session: AsyncSession):
|
||||
"""Test creating a command when the name is taken by an alias"""
|
||||
await crud.create_command(database_session, "name", "response")
|
||||
await crud.create_alias(database_session, "name", "n")
|
||||
|
||||
with pytest.raises(DuplicateInsertException):
|
||||
await crud.create_command(database_session, "n", "other response")
|
||||
|
||||
|
||||
async def test_create_alias_non_existing(database_session: AsyncSession):
|
||||
"""Test creating an alias when the name is still free"""
|
||||
command = await crud.create_command(database_session, "name", "response")
|
||||
await crud.create_alias(database_session, command.name, "n")
|
||||
|
||||
await database_session.refresh(command)
|
||||
assert len(command.aliases) == 1
|
||||
assert command.aliases[0].alias == "n"
|
||||
|
||||
|
||||
async def test_create_alias_duplicate(database_session: AsyncSession):
|
||||
"""Test creating an alias when another alias already has this name"""
|
||||
command = await crud.create_command(database_session, "name", "response")
|
||||
await crud.create_alias(database_session, command.name, "n")
|
||||
|
||||
with pytest.raises(DuplicateInsertException):
|
||||
await crud.create_alias(database_session, command.name, "n")
|
||||
|
||||
|
||||
async def test_create_alias_is_command(database_session: AsyncSession):
|
||||
"""Test creating an alias when the name is taken by a command"""
|
||||
await crud.create_command(database_session, "n", "response")
|
||||
command = await crud.create_command(database_session, "name", "response")
|
||||
|
||||
with pytest.raises(DuplicateInsertException):
|
||||
await crud.create_alias(database_session, command.name, "n")
|
||||
|
||||
|
||||
async def test_create_alias_match_by_alias(database_session: AsyncSession):
|
||||
"""Test creating an alias for a command when matching the name to another alias"""
|
||||
command = await crud.create_command(database_session, "name", "response")
|
||||
await crud.create_alias(database_session, command.name, "a1")
|
||||
alias = await crud.create_alias(database_session, "a1", "a2")
|
||||
assert alias.command == command
|
||||
|
||||
|
||||
async def test_get_command_by_name_exists(database_session: AsyncSession):
|
||||
"""Test getting a command by name"""
|
||||
await crud.create_command(database_session, "name", "response")
|
||||
command = await crud.get_command(database_session, "name")
|
||||
assert command is not None
|
||||
|
||||
|
||||
async def test_get_command_by_cleaned_name(database_session: AsyncSession):
|
||||
"""Test getting a command by the cleaned version of the name"""
|
||||
command = await crud.create_command(database_session, "CAPITALIZED NAME WITH SPACES", "response")
|
||||
found = await crud.get_command(database_session, "capitalizednamewithspaces")
|
||||
assert command == found
|
||||
|
||||
|
||||
async def test_get_command_by_alias(database_session: AsyncSession):
|
||||
"""Test getting a command by an alias"""
|
||||
command = await crud.create_command(database_session, "name", "response")
|
||||
await crud.create_alias(database_session, command.name, "a1")
|
||||
await crud.create_alias(database_session, command.name, "a2")
|
||||
|
||||
found = await crud.get_command(database_session, "a1")
|
||||
assert command == found
|
||||
|
||||
|
||||
async def test_get_command_non_existing(database_session: AsyncSession):
|
||||
"""Test getting a command when it doesn't exist"""
|
||||
assert await crud.get_command(database_session, "name") is None
|
||||
Loading…
Add table
Add a link
Reference in a new issue