didier/tests/test_database/test_crud/test_wordle.py

77 lines
2.7 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-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-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-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
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
@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
@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