didier/tests/test_database/test_crud/test_custom_commands.py

120 lines
4.7 KiB
Python
Raw Normal View History

2022-06-21 23:58:21 +02:00
import pytest
from sqlalchemy import select
2022-07-27 21:10:43 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
2022-06-21 23:58:21 +02:00
from database.crud import custom_commands as crud
from database.exceptions.constraints import DuplicateInsertException
from database.exceptions.not_found import NoResultFoundException
2022-08-29 20:24:42 +02:00
from database.schemas import CustomCommand
2022-06-21 23:58:21 +02:00
2022-07-27 21:10:43 +02:00
async def test_create_command_non_existing(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test creating a new command when it doesn't exist yet"""
2022-07-25 19:12:27 +02:00
await crud.create_command(postgres, "name", "response")
2022-06-21 23:58:21 +02:00
2022-07-25 19:12:27 +02:00
commands = (await postgres.execute(select(CustomCommand))).scalars().all()
2022-06-21 23:58:21 +02:00
assert len(commands) == 1
assert commands[0].name == "name"
2022-07-27 21:10:43 +02:00
async def test_create_command_duplicate_name(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test creating a command when the name already exists"""
2022-07-25 19:12:27 +02:00
await crud.create_command(postgres, "name", "response")
2022-06-21 23:58:21 +02:00
with pytest.raises(DuplicateInsertException):
2022-07-25 19:12:27 +02:00
await crud.create_command(postgres, "name", "other response")
2022-06-21 23:58:21 +02:00
2022-07-27 21:10:43 +02:00
async def test_create_command_name_is_alias(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test creating a command when the name is taken by an alias"""
2022-07-25 19:12:27 +02:00
await crud.create_command(postgres, "name", "response")
await crud.create_alias(postgres, "name", "n")
2022-06-21 23:58:21 +02:00
with pytest.raises(DuplicateInsertException):
2022-07-25 19:12:27 +02:00
await crud.create_command(postgres, "n", "other response")
2022-06-21 23:58:21 +02:00
2022-07-27 21:10:43 +02:00
async def test_create_alias(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test creating an alias when the name is still free"""
2022-07-25 19:12:27 +02:00
command = await crud.create_command(postgres, "name", "response")
await crud.create_alias(postgres, command.name, "n")
2022-06-21 23:58:21 +02:00
2022-07-25 19:12:27 +02:00
await postgres.refresh(command)
2022-06-21 23:58:21 +02:00
assert len(command.aliases) == 1
assert command.aliases[0].alias == "n"
2022-07-27 21:10:43 +02:00
async def test_create_alias_non_existing(postgres: AsyncSession):
"""Test creating an alias when the command doesn't exist"""
with pytest.raises(NoResultFoundException):
2022-07-25 19:12:27 +02:00
await crud.create_alias(postgres, "name", "alias")
2022-07-27 21:10:43 +02:00
async def test_create_alias_duplicate(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test creating an alias when another alias already has this name"""
2022-07-25 19:12:27 +02:00
command = await crud.create_command(postgres, "name", "response")
await crud.create_alias(postgres, command.name, "n")
2022-06-21 23:58:21 +02:00
with pytest.raises(DuplicateInsertException):
2022-07-25 19:12:27 +02:00
await crud.create_alias(postgres, command.name, "n")
2022-06-21 23:58:21 +02:00
2022-07-27 21:10:43 +02:00
async def test_create_alias_is_command(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test creating an alias when the name is taken by a command"""
2022-07-25 19:12:27 +02:00
await crud.create_command(postgres, "n", "response")
command = await crud.create_command(postgres, "name", "response")
2022-06-21 23:58:21 +02:00
with pytest.raises(DuplicateInsertException):
2022-07-25 19:12:27 +02:00
await crud.create_alias(postgres, command.name, "n")
2022-06-21 23:58:21 +02:00
2022-07-27 21:10:43 +02:00
async def test_create_alias_match_by_alias(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test creating an alias for a command when matching the name to another alias"""
2022-07-25 19:12:27 +02:00
command = await crud.create_command(postgres, "name", "response")
await crud.create_alias(postgres, command.name, "a1")
alias = await crud.create_alias(postgres, "a1", "a2")
2022-06-21 23:58:21 +02:00
assert alias.command == command
2022-07-27 21:10:43 +02:00
async def test_get_command_by_name_exists(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test getting a command by name"""
2022-07-25 19:12:27 +02:00
await crud.create_command(postgres, "name", "response")
command = await crud.get_command(postgres, "name")
2022-06-21 23:58:21 +02:00
assert command is not None
2022-07-27 21:10:43 +02:00
async def test_get_command_by_cleaned_name(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test getting a command by the cleaned version of the name"""
2022-07-25 19:12:27 +02:00
command = await crud.create_command(postgres, "CAPITALIZED NAME WITH SPACES", "response")
found = await crud.get_command(postgres, "capitalizednamewithspaces")
2022-06-21 23:58:21 +02:00
assert command == found
2022-07-27 21:10:43 +02:00
async def test_get_command_by_alias(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test getting a command by an alias"""
2022-07-25 19:12:27 +02:00
command = await crud.create_command(postgres, "name", "response")
await crud.create_alias(postgres, command.name, "a1")
await crud.create_alias(postgres, command.name, "a2")
2022-06-21 23:58:21 +02:00
2022-07-25 19:12:27 +02:00
found = await crud.get_command(postgres, "a1")
2022-06-21 23:58:21 +02:00
assert command == found
2022-07-27 21:10:43 +02:00
async def test_get_command_non_existing(postgres: AsyncSession):
2022-06-21 23:58:21 +02:00
"""Test getting a command when it doesn't exist"""
2022-07-25 19:12:27 +02:00
assert await crud.get_command(postgres, "name") is None
2022-07-27 21:10:43 +02:00
async def test_edit_command(postgres: AsyncSession):
"""Test editing an existing command"""
2022-07-25 19:12:27 +02:00
command = await crud.create_command(postgres, "name", "response")
await crud.edit_command(postgres, command.name, "new name", "new response")
assert command.name == "new name"
assert command.response == "new response"
2022-07-27 21:10:43 +02:00
async def test_edit_command_non_existing(postgres: AsyncSession):
"""Test editing a command that doesn't exist"""
with pytest.raises(NoResultFoundException):
2022-07-25 19:12:27 +02:00
await crud.edit_command(postgres, "name", "n", "r")