mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 23:55:46 +02:00
Remove mongo & fix tests
This commit is contained in:
parent
7b2109fb07
commit
8a4baf6bb8
56 changed files with 406 additions and 539 deletions
|
|
@ -2,12 +2,10 @@ import asyncio
|
|||
from typing import AsyncGenerator, Generator
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import motor.motor_asyncio
|
||||
import pytest
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
import settings
|
||||
from database.engine import mongo_client, postgres_engine
|
||||
from database.engine import postgres_engine
|
||||
from database.migrations import ensure_latest_migration, migrate
|
||||
from didier import Didier
|
||||
|
||||
|
|
@ -56,14 +54,6 @@ async def postgres(tables) -> AsyncGenerator[AsyncSession, None]:
|
|||
await connection.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mongodb() -> motor.motor_asyncio.AsyncIOMotorDatabase:
|
||||
"""Fixture to get a MongoDB connection"""
|
||||
database = mongo_client[settings.MONGO_DB]
|
||||
yield database
|
||||
mongo_client.drop_database(settings.MONGO_DB)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_client() -> Didier:
|
||||
"""Fixture to get a mock Didier instance
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import pytest
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import users
|
||||
from database.schemas.relational import (
|
||||
from database.schemas import (
|
||||
Bank,
|
||||
UforaAnnouncement,
|
||||
UforaCourse,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||
|
||||
from database.crud import birthdays as crud
|
||||
from database.crud import users
|
||||
from database.schemas.relational import User
|
||||
from database.schemas import User
|
||||
|
||||
|
||||
async def test_add_birthday_not_present(postgres: AsyncSession, user: User):
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||
|
||||
from database.crud import currency as crud
|
||||
from database.exceptions import currency as exceptions
|
||||
from database.schemas.relational import Bank
|
||||
from database.schemas import Bank
|
||||
|
||||
|
||||
async def test_add_dinks(postgres: AsyncSession, bank: Bank):
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||
from database.crud import custom_commands as crud
|
||||
from database.exceptions.constraints import DuplicateInsertException
|
||||
from database.exceptions.not_found import NoResultFoundException
|
||||
from database.schemas.relational import CustomCommand
|
||||
from database.schemas import CustomCommand
|
||||
|
||||
|
||||
async def test_create_command_non_existing(postgres: AsyncSession):
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from sqlalchemy import select
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import dad_jokes as crud
|
||||
from database.schemas.relational import DadJoke
|
||||
from database.schemas import DadJoke
|
||||
|
||||
|
||||
async def test_add_dad_joke(postgres: AsyncSession):
|
||||
|
|
|
|||
|
|
@ -1,63 +0,0 @@
|
|||
import pytest
|
||||
from freezegun import freeze_time
|
||||
|
||||
from database.crud import game_stats as crud
|
||||
from database.mongo_types import MongoDatabase
|
||||
from database.schemas.mongo.game_stats import GameStats
|
||||
from database.utils.datetime import today_only_date
|
||||
|
||||
|
||||
async def insert_game_stats(mongodb: MongoDatabase, stats: GameStats):
|
||||
"""Helper function to insert some stats"""
|
||||
collection = mongodb[GameStats.collection()]
|
||||
await collection.insert_one(stats.dict(by_alias=True))
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_get_stats_non_existent_creates(mongodb: MongoDatabase, test_user_id: int):
|
||||
"""Test getting a user's stats when the db is empty"""
|
||||
collection = mongodb[GameStats.collection()]
|
||||
assert await collection.find_one({"user_id": test_user_id}) is None
|
||||
await crud.get_game_stats(mongodb, test_user_id)
|
||||
assert await collection.find_one({"user_id": test_user_id}) is not None
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_get_stats_existing_returns(mongodb: MongoDatabase, test_user_id: int):
|
||||
"""Test getting a user's stats when there's already an entry present"""
|
||||
stats = GameStats(user_id=test_user_id)
|
||||
stats.wordle.games = 20
|
||||
await insert_game_stats(mongodb, stats)
|
||||
found_stats = await crud.get_game_stats(mongodb, test_user_id)
|
||||
assert found_stats.wordle.games == 20
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_complete_wordle_game_won(mongodb: MongoDatabase, test_user_id: int):
|
||||
"""Test completing a wordle game when you win"""
|
||||
await crud.complete_wordle_game(mongodb, test_user_id, win=True, guesses=2)
|
||||
stats = await crud.get_game_stats(mongodb, test_user_id)
|
||||
assert stats.wordle.guess_distribution == [0, 1, 0, 0, 0, 0]
|
||||
assert stats.wordle.games == 1
|
||||
assert stats.wordle.wins == 1
|
||||
assert stats.wordle.current_streak == 1
|
||||
assert stats.wordle.max_streak == 1
|
||||
assert stats.wordle.last_win == today_only_date()
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_complete_wordle_game_lost(mongodb: MongoDatabase, test_user_id: int):
|
||||
"""Test completing a wordle game when you lose"""
|
||||
stats = GameStats(user_id=test_user_id)
|
||||
stats.wordle.current_streak = 10
|
||||
await insert_game_stats(mongodb, stats)
|
||||
|
||||
await crud.complete_wordle_game(mongodb, test_user_id, win=False)
|
||||
stats = await crud.get_game_stats(mongodb, test_user_id)
|
||||
|
||||
# Check that streak was broken
|
||||
assert stats.wordle.current_streak == 0
|
||||
assert stats.wordle.games == 1
|
||||
assert stats.wordle.guess_distribution == [0, 0, 0, 0, 0, 0]
|
||||
|
|
@ -7,7 +7,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||
|
||||
from database.crud import tasks as crud
|
||||
from database.enums import TaskType
|
||||
from database.schemas.relational import Task
|
||||
from database.schemas import Task
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import datetime
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import ufora_announcements as crud
|
||||
from database.schemas.relational import UforaAnnouncement, UforaCourse
|
||||
from database.schemas import UforaAnnouncement, UforaCourse
|
||||
|
||||
|
||||
async def test_get_courses_with_announcements_none(postgres: AsyncSession):
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import ufora_courses as crud
|
||||
from database.schemas.relational import UforaCourse
|
||||
from database.schemas import UforaCourse
|
||||
|
||||
|
||||
async def test_get_course_by_name_exact(postgres: AsyncSession, ufora_course: UforaCourse):
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from sqlalchemy import select
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import users as crud
|
||||
from database.schemas.relational import User
|
||||
from database.schemas import User
|
||||
|
||||
|
||||
async def test_get_or_add_non_existing(postgres: AsyncSession):
|
||||
|
|
|
|||
|
|
@ -1,136 +1,140 @@
|
|||
from datetime import datetime, timedelta
|
||||
from datetime import date, timedelta
|
||||
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import wordle as crud
|
||||
from database.enums import TempStorageKey
|
||||
from database.mongo_types import MongoCollection, MongoDatabase
|
||||
from database.schemas.mongo.temporary_storage import TemporaryStorage
|
||||
from database.schemas.mongo.wordle import WordleGame
|
||||
from database.schemas import User, WordleGuess, WordleWord
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def wordle_collection(mongodb: MongoDatabase) -> MongoCollection:
|
||||
"""Fixture to get a reference to the wordle collection"""
|
||||
yield mongodb[WordleGame.collection()]
|
||||
async def wordle_guesses(postgres: AsyncSession, user: User) -> list[WordleGuess]:
|
||||
"""Fixture to generate some guesses"""
|
||||
guesses = []
|
||||
|
||||
for guess in ["TEST", "WORDLE", "WORDS"]:
|
||||
guess = WordleGuess(user_id=user.user_id, guess=guess)
|
||||
postgres.add(guess)
|
||||
await postgres.commit()
|
||||
|
||||
guesses.append(guess)
|
||||
|
||||
return guesses
|
||||
|
||||
|
||||
@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)
|
||||
await wordle_collection.insert_one(game.dict(by_alias=True))
|
||||
yield game
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_start_new_game(mongodb: MongoDatabase, wordle_collection: MongoCollection, test_user_id: int):
|
||||
"""Test starting a new game"""
|
||||
result = await wordle_collection.find_one({"user_id": test_user_id})
|
||||
assert result is None
|
||||
|
||||
await crud.start_new_wordle_game(mongodb, test_user_id)
|
||||
|
||||
result = await wordle_collection.find_one({"user_id": test_user_id})
|
||||
assert result is not None
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_get_active_wordle_game_none(mongodb: MongoDatabase, test_user_id: int):
|
||||
@pytest.mark.postgres
|
||||
async def test_get_active_wordle_game_none(postgres: AsyncSession, user: User):
|
||||
"""Test getting an active game when there is none"""
|
||||
result = await crud.get_active_wordle_game(mongodb, test_user_id)
|
||||
assert result is None
|
||||
result = await crud.get_active_wordle_game(postgres, user.user_id)
|
||||
assert not result
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_get_active_wordle_game(mongodb: MongoDatabase, wordle_game: WordleGame):
|
||||
@pytest.mark.postgres
|
||||
async def test_get_active_wordle_game(postgres: AsyncSession, wordle_guesses: list[WordleGuess]):
|
||||
"""Test getting an active game when there is one"""
|
||||
result = await crud.get_active_wordle_game(mongodb, wordle_game.user_id)
|
||||
assert result.dict(by_alias=True) == wordle_game.dict(by_alias=True)
|
||||
result = await crud.get_active_wordle_game(postgres, wordle_guesses[0].user_id)
|
||||
assert result == wordle_guesses
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_get_daily_word_none(mongodb: MongoDatabase):
|
||||
@pytest.mark.postgres
|
||||
async def test_get_daily_word_none(postgres: AsyncSession):
|
||||
"""Test getting the daily word when the database is empty"""
|
||||
result = await crud.get_daily_word(mongodb)
|
||||
result = await crud.get_daily_word(postgres)
|
||||
assert result is None
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
@pytest.mark.postgres
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_get_daily_word_not_today(mongodb: MongoDatabase):
|
||||
async def test_get_daily_word_not_today(postgres: AsyncSession):
|
||||
"""Test getting the daily word when there is an entry, but not for today"""
|
||||
day = datetime.today() - timedelta(days=1)
|
||||
collection = mongodb[TemporaryStorage.collection()]
|
||||
day = date.today() - timedelta(days=1)
|
||||
|
||||
word = "testword"
|
||||
await collection.insert_one({"key": TempStorageKey.WORDLE_WORD, "day": day, "word": word})
|
||||
word_instance = WordleWord(word=word, day=day)
|
||||
postgres.add(word_instance)
|
||||
await postgres.commit()
|
||||
|
||||
assert await crud.get_daily_word(mongodb) is None
|
||||
assert await crud.get_daily_word(postgres) is None
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
@pytest.mark.postgres
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_get_daily_word_present(mongodb: MongoDatabase):
|
||||
async def test_get_daily_word_present(postgres: AsyncSession):
|
||||
"""Test getting the daily word when there is one for today"""
|
||||
day = datetime.today()
|
||||
collection = mongodb[TemporaryStorage.collection()]
|
||||
day = date.today()
|
||||
|
||||
word = "testword"
|
||||
await collection.insert_one({"key": TempStorageKey.WORDLE_WORD, "day": day, "word": word})
|
||||
word_instance = WordleWord(word=word, day=day)
|
||||
postgres.add(word_instance)
|
||||
await postgres.commit()
|
||||
|
||||
assert await crud.get_daily_word(mongodb) == word
|
||||
daily_word = await crud.get_daily_word(postgres)
|
||||
assert daily_word is not None
|
||||
assert daily_word.word == word
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
@pytest.mark.postgres
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_set_daily_word_none_present(mongodb: MongoDatabase):
|
||||
async def test_set_daily_word_none_present(postgres: AsyncSession):
|
||||
"""Test setting the daily word when there is none"""
|
||||
assert await crud.get_daily_word(mongodb) is None
|
||||
assert await crud.get_daily_word(postgres) is None
|
||||
word = "testword"
|
||||
await crud.set_daily_word(mongodb, word)
|
||||
assert await crud.get_daily_word(mongodb) == word
|
||||
await crud.set_daily_word(postgres, word)
|
||||
|
||||
daily_word = await crud.get_daily_word(postgres)
|
||||
assert daily_word is not None
|
||||
assert daily_word.word == word
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
@pytest.mark.postgres
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_set_daily_word_present(mongodb: MongoDatabase):
|
||||
async def test_set_daily_word_present(postgres: AsyncSession):
|
||||
"""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
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
@pytest.mark.postgres
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_set_daily_word_force_overwrite(mongodb: MongoDatabase):
|
||||
async def test_set_daily_word_force_overwrite(postgres: AsyncSession):
|
||||
"""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)
|
||||
await crud.set_daily_word(postgres, word)
|
||||
word = "anotherword"
|
||||
await crud.set_daily_word(mongodb, word, forced=True)
|
||||
assert await crud.get_daily_word(mongodb) == word
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_make_wordle_guess(mongodb: MongoDatabase, wordle_game: WordleGame, test_user_id: int):
|
||||
@pytest.mark.postgres
|
||||
async def test_make_wordle_guess(postgres: AsyncSession, user: User):
|
||||
"""Test making a guess in your current game"""
|
||||
test_user_id = user.user_id
|
||||
|
||||
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]
|
||||
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]
|
||||
|
||||
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]
|
||||
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]
|
||||
|
||||
|
||||
@pytest.mark.mongo
|
||||
async def test_reset_wordle_games(mongodb: MongoDatabase, wordle_game: WordleGame, test_user_id: int):
|
||||
@pytest.mark.postgres
|
||||
async def test_reset_wordle_games(postgres: AsyncSession, wordle_guesses: list[WordleGuess], user: User):
|
||||
"""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
|
||||
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)
|
||||
|
|
|
|||
72
tests/test_database/test_crud/test_wordle_stats.py
Normal file
72
tests/test_database/test_crud/test_wordle_stats.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import datetime
|
||||
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import wordle_stats as crud
|
||||
from database.schemas import User, WordleStats
|
||||
|
||||
|
||||
async def insert_game_stats(postgres: AsyncSession, stats: WordleStats):
|
||||
"""Helper function to insert some stats"""
|
||||
postgres.add(stats)
|
||||
await postgres.commit()
|
||||
|
||||
|
||||
@pytest.mark.postgres
|
||||
async def test_get_stats_non_existent_creates(postgres: AsyncSession, user: User):
|
||||
"""Test getting a user's stats when the db is empty"""
|
||||
test_user_id = user.user_id
|
||||
|
||||
statement = select(WordleStats).where(WordleStats.user_id == test_user_id)
|
||||
assert (await postgres.execute(statement)).scalar_one_or_none() is None
|
||||
|
||||
await crud.get_wordle_stats(postgres, test_user_id)
|
||||
assert (await postgres.execute(statement)).scalar_one_or_none() is not None
|
||||
|
||||
|
||||
@pytest.mark.postgres
|
||||
async def test_get_stats_existing_returns(postgres: AsyncSession, user: User):
|
||||
"""Test getting a user's stats when there's already an entry present"""
|
||||
test_user_id = user.user_id
|
||||
|
||||
stats = WordleStats(user_id=test_user_id)
|
||||
stats.games = 20
|
||||
await insert_game_stats(postgres, stats)
|
||||
found_stats = await crud.get_wordle_stats(postgres, test_user_id)
|
||||
assert found_stats.games == 20
|
||||
|
||||
|
||||
@pytest.mark.postgres
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_complete_wordle_game_won(postgres: AsyncSession, user: User):
|
||||
"""Test completing a wordle game when you win"""
|
||||
test_user_id = user.user_id
|
||||
|
||||
await crud.complete_wordle_game(postgres, test_user_id, win=True)
|
||||
stats = await crud.get_wordle_stats(postgres, test_user_id)
|
||||
assert stats.games == 1
|
||||
assert stats.wins == 1
|
||||
assert stats.current_streak == 1
|
||||
assert stats.highest_streak == 1
|
||||
assert stats.last_win == datetime.date.today()
|
||||
|
||||
|
||||
@pytest.mark.postgres
|
||||
@freeze_time("2022-07-30")
|
||||
async def test_complete_wordle_game_lost(postgres: AsyncSession, user: User):
|
||||
"""Test completing a wordle game when you lose"""
|
||||
test_user_id = user.user_id
|
||||
|
||||
stats = WordleStats(user_id=test_user_id)
|
||||
stats.current_streak = 10
|
||||
await insert_game_stats(postgres, stats)
|
||||
|
||||
await crud.complete_wordle_game(postgres, test_user_id, win=False)
|
||||
stats = await crud.get_wordle_stats(postgres, test_user_id)
|
||||
|
||||
# Check that streak was broken
|
||||
assert stats.current_streak == 0
|
||||
assert stats.games == 1
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.schemas.relational import UforaCourse
|
||||
from database.schemas import UforaCourse
|
||||
from database.utils.caches import UforaCourseCache
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue