didier/tests/test_database/test_crud/test_currency.py

83 lines
2.5 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
2024-03-01 14:18:58 +01:00
from database.schemas import Bank, BankSavings
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
2024-03-01 14:18:58 +01:00
async def test_save(postgres: AsyncSession, bank: Bank, savings: BankSavings):
"""Test saving some Dinks"""
2022-07-03 19:26:30 +02:00
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
2024-03-01 14:18:58 +01:00
await crud.save(postgres, bank.user_id, 20, bank=bank, savings=savings)
2022-07-25 19:12:27 +02:00
await postgres.refresh(bank)
2024-03-01 14:18:58 +01:00
await postgres.refresh(savings)
2022-07-03 19:26:30 +02:00
assert bank.dinks == 80
2024-03-01 14:18:58 +01:00
assert savings.saved == 20
2022-07-03 19:26:30 +02:00
2024-03-01 14:18:58 +01:00
async def test_save_all(postgres: AsyncSession, bank: Bank, savings: BankSavings):
"""Test saving all dinks"""
2022-07-03 19:26:30 +02:00
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
2024-03-01 14:18:58 +01:00
await crud.save(postgres, bank.user_id, "all", bank=bank, savings=savings)
2022-07-25 19:12:27 +02:00
await postgres.refresh(bank)
2024-03-01 14:18:58 +01:00
await postgres.refresh(savings)
2022-07-03 19:26:30 +02:00
assert bank.dinks == 0
2024-03-01 14:18:58 +01:00
assert savings.saved == 100
2022-07-03 19:26:30 +02:00
2024-03-01 14:18:58 +01:00
async def test_save_more_than_owned(postgres: AsyncSession, bank: Bank, savings: BankSavings):
"""Test saving more Dinks than you own"""
2022-07-03 19:26:30 +02:00
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
2024-03-01 14:18:58 +01:00
await crud.save(postgres, bank.user_id, 200, bank=bank, savings=savings)
2022-07-25 19:12:27 +02:00
await postgres.refresh(bank)
2024-03-01 14:18:58 +01:00
await postgres.refresh(savings)
2022-07-03 19:26:30 +02:00
assert bank.dinks == 0
2024-03-01 14:18:58 +01:00
assert savings.saved == 100