Files
habit-tracker/tests/test_schemas_habit.py
T
shinalokandClaude Sonnet 5 d212451fe0 add habit goal-period difficulty and habit formation research notes
Ground the 21-day habit myth in actual research (Lally et al. 2010) and let
users pick a target period (21/66/254 days or unlimited) matching that
study's easy/median/hard automaticity timelines when creating a habit,
with progress shown on the habits list.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-19 10:12:17 +09:00

67 lines
1.9 KiB
Python

import pytest
from pydantic import ValidationError
from app.models.habit import ALL_WEEKDAYS_MASK, HabitDifficulty, HabitType
from app.schemas.habit import HabitCreate
def _base_kwargs(**overrides):
kwargs = {"name": "습관", "habit_type": HabitType.BUILD}
kwargs.update(overrides)
return kwargs
def test_blank_name_rejected():
with pytest.raises(ValidationError):
HabitCreate(**_base_kwargs(name=" "))
def test_name_is_stripped():
habit = HabitCreate(**_base_kwargs(name=" 아침 운동 "))
assert habit.name == "아침 운동"
def test_blank_condition_text_becomes_none():
habit = HabitCreate(**_base_kwargs(condition_text=" "))
assert habit.condition_text is None
def test_condition_text_is_stripped_when_present():
habit = HabitCreate(**_base_kwargs(condition_text=" 30분 이상 "))
assert habit.condition_text == "30분 이상"
def test_weekdays_mask_zero_rejected():
with pytest.raises(ValidationError):
HabitCreate(**_base_kwargs(weekdays_mask=0))
def test_weekdays_mask_over_max_rejected():
with pytest.raises(ValidationError):
HabitCreate(**_base_kwargs(weekdays_mask=ALL_WEEKDAYS_MASK + 1))
def test_weekdays_mask_single_day_accepted():
habit = HabitCreate(**_base_kwargs(weekdays_mask=0b0000001))
assert habit.weekdays_mask == 0b0000001
def test_weekdays_mask_defaults_to_all_days():
habit = HabitCreate(**_base_kwargs())
assert habit.weekdays_mask == ALL_WEEKDAYS_MASK
def test_difficulty_defaults_to_medium():
habit = HabitCreate(**_base_kwargs())
assert habit.difficulty == HabitDifficulty.MEDIUM
def test_difficulty_accepts_explicit_value():
habit = HabitCreate(**_base_kwargs(difficulty=HabitDifficulty.UNLIMITED))
assert habit.difficulty == HabitDifficulty.UNLIMITED
def test_difficulty_rejects_invalid_value():
with pytest.raises(ValidationError):
HabitCreate(**_base_kwargs(difficulty="impossible"))