didier/tests/test_database/test_crud/test_currency.py

80 lines
2.3 KiB
Python
Raw Normal View History

2022-07-23 20:59:02 +02:00
import datetime
2022-07-03 19:26:30 +02:00
import pytest
2022-07-23 20:59:02 +02:00
from freezegun import freeze_time
2022-07-27 21:10:43 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
2022-07-03 19:26:30 +02:00
from database.crud import currency as crud
from database.exceptions import currency as exceptions
2022-08-29 20:24:42 +02:00
from database.schemas import Bank
2022-07-03 19:26:30 +02:00
2022-07-27 21:10:43 +02:00
async def test_add_dinks(postgres: AsyncSession, bank: Bank):
2022-07-03 19:26:30 +02:00
"""Test adding dinks to an account"""
assert bank.dinks == 0
2022-07-25 19:12:27 +02:00
await crud.add_dinks(postgres, bank.user_id, 10)
await postgres.refresh(bank)
2022-07-03 19:26:30 +02:00
assert bank.dinks == 10
2022-07-23 20:59:02 +02:00
@freeze_time("2022/07/23")
2022-07-27 21:10:43 +02:00
async def test_claim_nightly_available(postgres: AsyncSession, bank: Bank):
2022-07-03 19:26:30 +02:00
"""Test claiming nightlies when it hasn't been done yet"""
2022-07-25 19:12:27 +02:00
await crud.claim_nightly(postgres, bank.user_id)
await postgres.refresh(bank)
2022-07-03 19:26:30 +02:00
assert bank.dinks == crud.NIGHTLY_AMOUNT
2022-07-25 19:12:27 +02:00
nightly_data = await crud.get_nightly_data(postgres, bank.user_id)
2022-07-23 20:59:02 +02:00
assert nightly_data.last_nightly == datetime.date(year=2022, month=7, day=23)
2022-07-03 19:26:30 +02:00
2022-07-23 20:59:02 +02:00
@freeze_time("2022/07/23")
2022-07-27 21:10:43 +02:00
async def test_claim_nightly_unavailable(postgres: AsyncSession, bank: Bank):
2022-07-03 19:26:30 +02:00
"""Test claiming nightlies twice in a day"""
2022-07-25 19:12:27 +02:00
await crud.claim_nightly(postgres, bank.user_id)
2022-07-03 19:26:30 +02:00
with pytest.raises(exceptions.DoubleNightly):
2022-07-25 19:12:27 +02:00
await crud.claim_nightly(postgres, bank.user_id)
2022-07-03 19:26:30 +02:00
2022-07-25 19:12:27 +02:00
await postgres.refresh(bank)
2022-07-03 19:26:30 +02:00
assert bank.dinks == crud.NIGHTLY_AMOUNT
2022-07-27 21:10:43 +02:00
async def test_invest(postgres: AsyncSession, bank: Bank):
2022-07-03 19:26:30 +02:00
"""Test investing some Dinks"""
bank.dinks = 100
2022-07-25 19:12:27 +02:00
postgres.add(bank)
await postgres.commit()
2022-07-03 19:26:30 +02:00
2022-07-25 19:12:27 +02:00
await crud.invest(postgres, bank.user_id, 20)
await postgres.refresh(bank)
2022-07-03 19:26:30 +02:00
assert bank.dinks == 80
assert bank.invested == 20
2022-07-27 21:10:43 +02:00
async def test_invest_all(postgres: AsyncSession, bank: Bank):
2022-07-03 19:26:30 +02:00
"""Test investing all dinks"""
bank.dinks = 100
2022-07-25 19:12:27 +02:00
postgres.add(bank)
await postgres.commit()
2022-07-03 19:26:30 +02:00
2022-07-25 19:12:27 +02:00
await crud.invest(postgres, bank.user_id, "all")
await postgres.refresh(bank)
2022-07-03 19:26:30 +02:00
assert bank.dinks == 0
assert bank.invested == 100
2022-07-27 21:10:43 +02:00
async def test_invest_more_than_owned(postgres: AsyncSession, bank: Bank):
2022-07-03 19:26:30 +02:00
"""Test investing more Dinks than you own"""
bank.dinks = 100
2022-07-25 19:12:27 +02:00
postgres.add(bank)
await postgres.commit()
2022-07-03 19:26:30 +02:00
2022-07-25 19:12:27 +02:00
await crud.invest(postgres, bank.user_id, 200)
await postgres.refresh(bank)
2022-07-03 19:26:30 +02:00
assert bank.dinks == 0
assert bank.invested == 100