didier/tests/test_database/test_crud/test_custom_commands.py

119 lines
4.4 KiB
Python
Raw Normal View History

2022-06-21 23:58:21 +02:00
import pytest
from sqlalchemy import select
from database.crud import custom_commands as crud
from database.exceptions.constraints import DuplicateInsertException
from database.exceptions.not_found import NoResultFoundException
2022-07-25 21:20:09 +02:00
from database.schemas.relational import CustomCommand
2022-06-21 23:58:21 +02:00
2022-07-25 19:12:27 +02:00
async def test_create_command_non_existing(postgres):
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-25 19:12:27 +02:00
async def test_create_command_duplicate_name(postgres):
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-25 19:12:27 +02:00
async def test_create_command_name_is_alias(postgres):
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-25 19:12:27 +02:00
async def test_create_alias(postgres):
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-25 19:12:27 +02:00
async def test_create_alias_non_existing(postgres):
"""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-25 19:12:27 +02:00
async def test_create_alias_duplicate(postgres):
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-25 19:12:27 +02:00
async def test_create_alias_is_command(postgres):
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-25 19:12:27 +02:00
async def test_create_alias_match_by_alias(postgres):
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-25 19:12:27 +02:00
async def test_get_command_by_name_exists(postgres):
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-25 19:12:27 +02:00
async def test_get_command_by_cleaned_name(postgres):
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-25 19:12:27 +02:00
async def test_get_command_by_alias(postgres):
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-25 19:12:27 +02:00
async def test_get_command_non_existing(postgres):
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-25 19:12:27 +02:00
async def test_edit_command(postgres):
"""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-25 19:12:27 +02:00
async def test_edit_command_non_existing(postgres):
"""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")