didier/tests/test_database/test_crud/test_wordle.py

136 lines
4.9 KiB
Python
Raw Normal View History

2022-07-30 00:24:03 +02:00
from datetime import datetime, timedelta
2022-07-25 22:58:02 +02:00
import pytest
2022-07-30 00:24:03 +02:00
from freezegun import freeze_time
2022-07-25 22:58:02 +02:00
from database.crud import wordle as crud
2022-07-30 00:24:03 +02:00
from database.enums import TempStorageKey
2022-07-25 22:58:02 +02:00
from database.mongo_types import MongoCollection, MongoDatabase
2022-07-30 00:24:03 +02:00
from database.schemas.mongo import TemporaryStorage, WordleGame
2022-07-25 22:58:02 +02:00
@pytest.fixture
async def wordle_collection(mongodb: MongoDatabase) -> MongoCollection:
"""Fixture to get a reference to the wordle collection"""
yield mongodb[WordleGame.collection()]
@pytest.fixture
async def wordle_game(wordle_collection: MongoCollection, test_user_id: int) -> WordleGame:
"""Fixture to create a new game"""
game = WordleGame(user_id=test_user_id)
2022-07-25 22:58:02 +02:00
await wordle_collection.insert_one(game.dict(by_alias=True))
yield game
2022-07-30 16:14:32 +02:00
@pytest.mark.mongo
2022-07-27 21:10:43 +02:00
async def test_start_new_game(mongodb: MongoDatabase, wordle_collection: MongoCollection, test_user_id: int):
2022-07-25 22:58:02 +02:00
"""Test starting a new game"""
result = await wordle_collection.find_one({"user_id": test_user_id})
assert result is None
2022-07-27 21:10:43 +02:00
await crud.start_new_wordle_game(mongodb, test_user_id)
2022-07-25 22:58:02 +02:00
result = await wordle_collection.find_one({"user_id": test_user_id})
assert result is not None
2022-07-30 16:14:32 +02:00
@pytest.mark.mongo
2022-07-27 21:10:43 +02:00
async def test_get_active_wordle_game_none(mongodb: MongoDatabase, test_user_id: int):
2022-07-25 22:58:02 +02:00
"""Test getting an active game when there is none"""
2022-07-27 21:10:43 +02:00
result = await crud.get_active_wordle_game(mongodb, test_user_id)
2022-07-25 22:58:02 +02:00
assert result is None
2022-07-30 16:14:32 +02:00
@pytest.mark.mongo
2022-07-27 21:10:43 +02:00
async def test_get_active_wordle_game(mongodb: MongoDatabase, wordle_game: WordleGame):
2022-07-30 00:24:03 +02:00
"""Test getting an active game when there is one"""
2022-07-27 21:10:43 +02:00
result = await crud.get_active_wordle_game(mongodb, wordle_game.user_id)
assert result.dict(by_alias=True) == wordle_game.dict(by_alias=True)
2022-07-30 00:24:03 +02:00
2022-07-30 16:14:32 +02:00
@pytest.mark.mongo
2022-07-30 00:24:03 +02:00
async def test_get_daily_word_none(mongodb: MongoDatabase):
"""Test getting the daily word when the database is empty"""
result = await crud.get_daily_word(mongodb)
assert result is None
2022-07-30 16:14:32 +02:00
@pytest.mark.mongo
2022-07-30 00:24:03 +02:00
@freeze_time("2022-07-30")
async def test_get_daily_word_not_today(mongodb: MongoDatabase):
"""Test getting the daily word when there is an entry, but not for today"""
day = datetime.today() - timedelta(days=1)
collection = mongodb[TemporaryStorage.collection()]
word = "testword"
await collection.insert_one({"key": TempStorageKey.WORDLE_WORD, "day": day, "word": word})
assert await crud.get_daily_word(mongodb) is None
2022-07-30 16:14:32 +02:00
@pytest.mark.mongo
2022-07-30 00:24:03 +02:00
@freeze_time("2022-07-30")
async def test_get_daily_word_present(mongodb: MongoDatabase):
"""Test getting the daily word when there is one for today"""
day = datetime.today()
collection = mongodb[TemporaryStorage.collection()]
word = "testword"
await collection.insert_one({"key": TempStorageKey.WORDLE_WORD, "day": day, "word": word})
assert await crud.get_daily_word(mongodb) == word
2022-07-30 16:14:32 +02:00
@pytest.mark.mongo
@freeze_time("2022-07-30")
async def test_set_daily_word_none_present(mongodb: MongoDatabase):
"""Test setting the daily word when there is none"""
assert await crud.get_daily_word(mongodb) is None
word = "testword"
await crud.set_daily_word(mongodb, word)
assert await crud.get_daily_word(mongodb) == word
@pytest.mark.mongo
@freeze_time("2022-07-30")
async def test_set_daily_word_present(mongodb: MongoDatabase):
"""Test setting the daily word when there already is one"""
word = "testword"
await crud.set_daily_word(mongodb, word)
await crud.set_daily_word(mongodb, "another word")
assert await crud.get_daily_word(mongodb) == word
@pytest.mark.mongo
@freeze_time("2022-07-30")
async def test_set_daily_word_force_overwrite(mongodb: MongoDatabase):
"""Test setting the daily word when there already is one, but "forced" is set to True"""
word = "testword"
await crud.set_daily_word(mongodb, word)
word = "anotherword"
await crud.set_daily_word(mongodb, word, forced=True)
assert await crud.get_daily_word(mongodb) == word
@pytest.mark.mongo
async def test_make_wordle_guess(mongodb: MongoDatabase, wordle_game: WordleGame, test_user_id: int):
"""Test making a guess in your current game"""
guess = "guess"
await crud.make_wordle_guess(mongodb, test_user_id, guess)
wordle_game = await crud.get_active_wordle_game(mongodb, test_user_id)
assert wordle_game.guesses == [guess]
other_guess = "otherguess"
await crud.make_wordle_guess(mongodb, test_user_id, other_guess)
wordle_game = await crud.get_active_wordle_game(mongodb, test_user_id)
assert wordle_game.guesses == [guess, other_guess]
@pytest.mark.mongo
async def test_reset_wordle_games(mongodb: MongoDatabase, wordle_game: WordleGame, test_user_id: int):
"""Test dropping the collection of active games"""
assert await crud.get_active_wordle_game(mongodb, test_user_id) is not None
await crud.reset_wordle_games(mongodb)
assert await crud.get_active_wordle_game(mongodb, test_user_id) is None