Test all crud stuff up until now

pull/124/head
stijndcl 2022-07-30 16:14:32 +02:00
parent a0781a046b
commit e4e77502e8
4 changed files with 64 additions and 2 deletions

View File

@ -61,7 +61,7 @@ async def set_daily_word(database: MongoDatabase, word: str, *, forced: bool = F
"""
collection = database[TemporaryStorage.collection()]
current_word = None if forced else await get_daily_word(collection)
current_word = None if forced else await get_daily_word(database)
if current_word is not None:
return current_word

View File

@ -52,7 +52,6 @@ class Games(commands.Cog):
return await interaction.followup.send(embed=embed)
guess = guess.lower()
await make_wordle_guess(self.client.mongo_db, interaction.user.id, guess)
# Don't re-request the game, we already have it

View File

@ -53,3 +53,7 @@ env = [
"POSTGRES_PORT = 5433",
"DISCORD_TOKEN = token"
]
markers = [
"mongo: tests that use MongoDB",
"postgres: tests that use PostgreSQL"
]

View File

@ -23,6 +23,7 @@ async def wordle_game(wordle_collection: MongoCollection, test_user_id: int) ->
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})
@ -34,24 +35,28 @@ async def test_start_new_game(mongodb: MongoDatabase, wordle_collection: MongoCo
assert result is not None
@pytest.mark.mongo
async def test_get_active_wordle_game_none(mongodb: MongoDatabase, test_user_id: int):
"""Test getting an active game when there is none"""
result = await crud.get_active_wordle_game(mongodb, test_user_id)
assert result is None
@pytest.mark.mongo
async def test_get_active_wordle_game(mongodb: MongoDatabase, wordle_game: WordleGame):
"""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)
@pytest.mark.mongo
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
@pytest.mark.mongo
@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"""
@ -64,6 +69,7 @@ async def test_get_daily_word_not_today(mongodb: MongoDatabase):
assert await crud.get_daily_word(mongodb) is None
@pytest.mark.mongo
@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"""
@ -74,3 +80,56 @@ async def test_get_daily_word_present(mongodb: MongoDatabase):
await collection.insert_one({"key": TempStorageKey.WORDLE_WORD, "day": day, "word": word})
assert await crud.get_daily_word(mongodb) == word
@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