didier/tests/test_database/test_crud/test_wordle.py

141 lines
4.6 KiB
Python
Raw Normal View History

2022-08-29 20:24:42 +02:00
from datetime import date, timedelta
2022-07-30 00:24:03 +02:00
2022-07-25 22:58:02 +02:00
import pytest
2022-07-30 00:24:03 +02:00
from freezegun import freeze_time
2022-08-29 20:24:42 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
2022-07-25 22:58:02 +02:00
from database.crud import wordle as crud
2022-08-29 20:24:42 +02:00
from database.schemas import User, WordleGuess, WordleWord
2022-07-25 22:58:02 +02:00
@pytest.fixture
2022-08-29 20:24:42 +02:00
async def wordle_guesses(postgres: AsyncSession, user: User) -> list[WordleGuess]:
"""Fixture to generate some guesses"""
guesses = []
2022-07-25 22:58:02 +02:00
2022-08-29 20:24:42 +02:00
for guess in ["TEST", "WORDLE", "WORDS"]:
guess = WordleGuess(user_id=user.user_id, guess=guess)
postgres.add(guess)
await postgres.commit()
2022-07-25 22:58:02 +02:00
2022-08-29 20:24:42 +02:00
guesses.append(guess)
2022-07-25 22:58:02 +02:00
2022-08-29 20:24:42 +02:00
return guesses
2022-07-25 22:58:02 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
async def test_get_active_wordle_game_none(postgres: AsyncSession, user: User):
2022-07-25 22:58:02 +02:00
"""Test getting an active game when there is none"""
2022-08-29 20:24:42 +02:00
result = await crud.get_active_wordle_game(postgres, user.user_id)
assert not result
2022-07-25 22:58:02 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
async def test_get_active_wordle_game(postgres: AsyncSession, wordle_guesses: list[WordleGuess]):
2022-07-30 00:24:03 +02:00
"""Test getting an active game when there is one"""
2022-08-29 20:24:42 +02:00
result = await crud.get_active_wordle_game(postgres, wordle_guesses[0].user_id)
assert result == wordle_guesses
2022-07-30 00:24:03 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
async def test_get_daily_word_none(postgres: AsyncSession):
2022-07-30 00:24:03 +02:00
"""Test getting the daily word when the database is empty"""
2022-08-29 20:24:42 +02:00
result = await crud.get_daily_word(postgres)
2022-07-30 00:24:03 +02:00
assert result is None
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
2022-07-30 00:24:03 +02:00
@freeze_time("2022-07-30")
2022-08-29 20:24:42 +02:00
async def test_get_daily_word_not_today(postgres: AsyncSession):
2022-07-30 00:24:03 +02:00
"""Test getting the daily word when there is an entry, but not for today"""
2022-08-29 20:24:42 +02:00
day = date.today() - timedelta(days=1)
2022-07-30 00:24:03 +02:00
word = "testword"
2022-08-29 20:24:42 +02:00
word_instance = WordleWord(word=word, day=day)
postgres.add(word_instance)
await postgres.commit()
2022-07-30 00:24:03 +02:00
2022-08-29 20:24:42 +02:00
assert await crud.get_daily_word(postgres) is None
2022-07-30 00:24:03 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
2022-07-30 00:24:03 +02:00
@freeze_time("2022-07-30")
2022-08-29 20:24:42 +02:00
async def test_get_daily_word_present(postgres: AsyncSession):
2022-07-30 00:24:03 +02:00
"""Test getting the daily word when there is one for today"""
2022-08-29 20:24:42 +02:00
day = date.today()
2022-07-30 00:24:03 +02:00
word = "testword"
2022-08-29 20:24:42 +02:00
word_instance = WordleWord(word=word, day=day)
postgres.add(word_instance)
await postgres.commit()
2022-07-30 00:24:03 +02:00
2022-08-29 20:24:42 +02:00
daily_word = await crud.get_daily_word(postgres)
assert daily_word is not None
assert daily_word.word == word
2022-07-30 16:14:32 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
2022-07-30 16:14:32 +02:00
@freeze_time("2022-07-30")
2022-08-29 20:24:42 +02:00
async def test_set_daily_word_none_present(postgres: AsyncSession):
2022-07-30 16:14:32 +02:00
"""Test setting the daily word when there is none"""
2022-08-29 20:24:42 +02:00
assert await crud.get_daily_word(postgres) is None
2022-07-30 16:14:32 +02:00
word = "testword"
2022-08-29 20:24:42 +02:00
await crud.set_daily_word(postgres, word)
2022-07-30 16:14:32 +02:00
2022-08-29 20:24:42 +02:00
daily_word = await crud.get_daily_word(postgres)
assert daily_word is not None
assert daily_word.word == word
2022-07-30 16:14:32 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
2022-07-30 16:14:32 +02:00
@freeze_time("2022-07-30")
2022-08-29 20:24:42 +02:00
async def test_set_daily_word_present(postgres: AsyncSession):
2022-07-30 16:14:32 +02:00
"""Test setting the daily word when there already is one"""
word = "testword"
2022-08-29 20:24:42 +02:00
await crud.set_daily_word(postgres, word)
await crud.set_daily_word(postgres, "another word")
daily_word = await crud.get_daily_word(postgres)
assert daily_word is not None
assert daily_word.word == word
2022-07-30 16:14:32 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
2022-07-30 16:14:32 +02:00
@freeze_time("2022-07-30")
2022-08-29 20:24:42 +02:00
async def test_set_daily_word_force_overwrite(postgres: AsyncSession):
2022-07-30 16:14:32 +02:00
"""Test setting the daily word when there already is one, but "forced" is set to True"""
word = "testword"
2022-08-29 20:24:42 +02:00
await crud.set_daily_word(postgres, word)
2022-07-30 16:14:32 +02:00
word = "anotherword"
2022-08-29 20:24:42 +02:00
await crud.set_daily_word(postgres, word, forced=True)
daily_word = await crud.get_daily_word(postgres)
assert daily_word is not None
assert daily_word.word == word
2022-07-30 16:14:32 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
async def test_make_wordle_guess(postgres: AsyncSession, user: User):
2022-07-30 16:14:32 +02:00
"""Test making a guess in your current game"""
2022-08-29 20:24:42 +02:00
test_user_id = user.user_id
2022-07-30 16:14:32 +02:00
guess = "guess"
2022-08-29 20:24:42 +02:00
await crud.make_wordle_guess(postgres, test_user_id, guess)
wordle_guesses = await crud.get_active_wordle_game(postgres, test_user_id)
assert list(map(lambda x: x.guess, wordle_guesses)) == [guess]
2022-07-30 16:14:32 +02:00
other_guess = "otherguess"
2022-08-29 20:24:42 +02:00
await crud.make_wordle_guess(postgres, test_user_id, other_guess)
wordle_guesses = await crud.get_active_wordle_game(postgres, test_user_id)
assert list(map(lambda x: x.guess, wordle_guesses)) == [guess, other_guess]
2022-07-30 16:14:32 +02:00
2022-08-29 20:24:42 +02:00
@pytest.mark.postgres
async def test_reset_wordle_games(postgres: AsyncSession, wordle_guesses: list[WordleGuess], user: User):
2022-07-30 16:14:32 +02:00
"""Test dropping the collection of active games"""
2022-08-29 20:24:42 +02:00
test_user_id = user.user_id
assert await crud.get_active_wordle_game(postgres, test_user_id)
await crud.reset_wordle_games(postgres)
assert not await crud.get_active_wordle_game(postgres, test_user_id)