feat: added CRUD operations for timers, created decorator for error handling, added schema ; refactor: updated functions and ; test: moved tests from to , written tests for CRUD operations on timers and database.
This commit is contained in:
129
kawai_focus/tests/cruds_tests.py
Normal file
129
kawai_focus/tests/cruds_tests.py
Normal file
@@ -0,0 +1,129 @@
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from alembic.config import Config
|
||||
from alembic import command
|
||||
|
||||
from kawai_focus.schemas import TimerModel
|
||||
from kawai_focus.database.session import db
|
||||
from kawai_focus.database.models import Timer, Base
|
||||
from kawai_focus.database.cruds import list_timers, get_timer, new_timer, update_timer, del_timer
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def test_db() -> sessionmaker:
|
||||
"""Создание тестовой базы данных и сессии"""
|
||||
|
||||
engine = create_engine('sqlite:///:memory:') # Временная БД
|
||||
Base.metadata.create_all(engine)
|
||||
alembic_cfg = Config('./alembic.ini')
|
||||
alembic_cfg.set_main_option('script_location', './kawai_focus/database/alembic')
|
||||
command.upgrade(alembic_cfg, 'head')
|
||||
|
||||
session = sessionmaker(bind=engine)
|
||||
db._session_factory = session
|
||||
|
||||
return db._session_factory
|
||||
|
||||
|
||||
def test_new_timer(test_db: sessionmaker):
|
||||
"""Тест crud создания нового таймера"""
|
||||
|
||||
timer_data = TimerModel(title='test_timer_1', pomodoro_time=60, break_long_time=30, break_time=6,count_pomodoro=5)
|
||||
|
||||
with test_db() as session:
|
||||
timer = new_timer(data=timer_data)
|
||||
|
||||
assert timer.title == 'test_timer_1'
|
||||
assert timer.pomodoro_time == 60
|
||||
assert timer.break_long_time == 30
|
||||
assert timer.break_time == 6
|
||||
assert timer.count_pomodoro ==5
|
||||
|
||||
|
||||
def test_get_timer(test_db: sessionmaker):
|
||||
"""Тест crud получения таймера по id"""
|
||||
|
||||
timer_data = TimerModel(title='test_timer_1', pomodoro_time=60, break_long_time=30, break_time=6,count_pomodoro=5)
|
||||
|
||||
with test_db() as session:
|
||||
new_timer(data=timer_data)
|
||||
timer = get_timer(timer_id=1)
|
||||
|
||||
assert timer
|
||||
assert timer.title == 'test_timer_1'
|
||||
assert timer.pomodoro_time == 60
|
||||
assert timer.break_long_time == 30
|
||||
assert timer.break_time == 6
|
||||
assert timer.count_pomodoro ==5
|
||||
|
||||
|
||||
def test_update_timer(test_db: sessionmaker):
|
||||
"""Тест crud обновления таймера"""
|
||||
|
||||
timer_data = TimerModel(title='test_timer_1', pomodoro_time=60, break_long_time=30, break_time=6,count_pomodoro=5)
|
||||
|
||||
with test_db() as session:
|
||||
new_timer(data=timer_data)
|
||||
new_timer_data = TimerModel(
|
||||
id=1,
|
||||
title='test_timer_2',
|
||||
pomodoro_time=55,
|
||||
break_long_time=25,
|
||||
break_time=5,
|
||||
count_pomodoro=6
|
||||
)
|
||||
|
||||
timer = update_timer(data=new_timer_data)
|
||||
|
||||
assert timer
|
||||
assert timer.title == 'test_timer_2'
|
||||
assert timer.pomodoro_time == 55
|
||||
assert timer.break_long_time == 25
|
||||
assert timer.break_time == 5
|
||||
assert timer.count_pomodoro == 6
|
||||
|
||||
|
||||
def test_del_timer(test_db: sessionmaker):
|
||||
"""Тест crud удаления таймера"""
|
||||
|
||||
timer_data = TimerModel(title='test_timer_1', pomodoro_time=60, break_long_time=30, break_time=6,count_pomodoro=5)
|
||||
|
||||
with test_db() as session:
|
||||
new_timer(data=timer_data)
|
||||
|
||||
del_timer(timer_id=1)
|
||||
timer = session.query(Timer).filter_by(title='test_timer_1').first()
|
||||
|
||||
assert timer is None
|
||||
|
||||
|
||||
def test_list_timers(test_db: sessionmaker):
|
||||
"""Тест crud получения списка таймеров"""
|
||||
|
||||
timer_data_1 = TimerModel(
|
||||
title='test_timer_1',
|
||||
pomodoro_time=60,
|
||||
break_long_time=30,
|
||||
break_time=6,
|
||||
count_pomodoro=5
|
||||
)
|
||||
timer_data_2 = TimerModel(
|
||||
title='test_timer_2',
|
||||
pomodoro_time=55,
|
||||
break_long_time=25,
|
||||
break_time=5,
|
||||
count_pomodoro=6
|
||||
)
|
||||
|
||||
with test_db() as session:
|
||||
new_timer(data=timer_data_1)
|
||||
new_timer(data=timer_data_2)
|
||||
|
||||
timers = list_timers()
|
||||
|
||||
assert timers
|
||||
assert timers[0].id == 1
|
||||
assert timers[0].title == 'test_timer_1'
|
||||
assert timers[1].id == 2
|
||||
assert timers[1].title == 'test_timer_2'
|
||||
5
kawai_focus/tests/json/timer.json
Normal file
5
kawai_focus/tests/json/timer.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"sound_timer": "alarm-beep.mp3",
|
||||
"zero_time": "00:00:00",
|
||||
"custom_time": "00:00:12"
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from kawai_focus.utils.errors import ErrorMessage
|
||||
from kawai_focus.utils.validators import TimerValidator
|
||||
from kawai_focus.schemas import TimerTimeModel
|
||||
|
||||
|
||||
def test_valid_time():
|
||||
"""Тест корректных данных"""
|
||||
|
||||
timer = TimerValidator(hh=10, mm=30, ss=15)
|
||||
timer = TimerTimeModel(hh=10, mm=30, ss=15)
|
||||
assert timer.hh == 10
|
||||
assert timer.mm == 30
|
||||
assert timer.ss == 15
|
||||
@@ -15,43 +17,43 @@ def test_valid_time():
|
||||
def test_not_int_type():
|
||||
"""Тест исключения для некорректного типа данных"""
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
TimerValidator(hh="10", mm=30, ss=15)
|
||||
|
||||
assert str(excinfo.value) == ErrorMessage.NOT_INT_TYPE.value
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
TimerTimeModel(hh=None, mm=30, ss=15)
|
||||
|
||||
assert 'Input should be a valid integer' in str(excinfo.value)
|
||||
|
||||
|
||||
def test_no_time():
|
||||
"""Тест исключения ситуации когда не указано время"""
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
TimerValidator(hh=0, mm=0, ss=0)
|
||||
|
||||
assert str(excinfo.value) == ErrorMessage.NO_TIME.value
|
||||
TimerTimeModel(hh=0, mm=0, ss=0)
|
||||
|
||||
assert ErrorMessage.NO_TIME.value in str(excinfo.value)
|
||||
|
||||
|
||||
def test_negative_time():
|
||||
"""Тест исключения для отрицательного времени"""
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
TimerValidator(hh=-1, mm=30, ss=15)
|
||||
|
||||
assert str(excinfo.value) == ErrorMessage.NEGATIVE_TIME.value
|
||||
TimerTimeModel(hh=-1, mm=30, ss=15)
|
||||
|
||||
assert 'Input should be greater than or equal to 0' in str(excinfo.value)
|
||||
|
||||
|
||||
def test_exceed_seconds_or_minutes():
|
||||
"""Тест исключения для секунд/минут больше 59"""
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
TimerValidator(hh=0, mm=60, ss=10)
|
||||
|
||||
assert str(excinfo.value) == ErrorMessage.SS_MM_BIG.value
|
||||
TimerTimeModel(hh=0, mm=60, ss=10)
|
||||
|
||||
assert 'Input should be less than or equal to 59' in str(excinfo.value)
|
||||
|
||||
|
||||
def test_exceed_hours():
|
||||
"""Тест исключения для часов больше 23"""
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
TimerValidator(hh=24, mm=0, ss=0)
|
||||
|
||||
assert str(excinfo.value) == ErrorMessage.HH_BIG.value
|
||||
TimerTimeModel(hh=24, mm=0, ss=0)
|
||||
|
||||
assert 'Input should be less than or equal to 23' in str(excinfo.value)
|
||||
BIN
kawai_focus/tests/timer.db
Normal file
BIN
kawai_focus/tests/timer.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user