async def get_trades(db: Session = Depends(get_db)): """Returns a list of all trades, sorted in descending order by timetamps. """ trade_rows = db.query(Trade).order_by(Trade.timestamp.desc()) return TradesResponse(trades=[ TradeModel( id=r.trade_id, sell_ccy=r.sell_ccy, sell_amount=Currency(r.sell_amount), buy_ccy=r.buy_ccy, buy_amount=Currency(r.sell_amount) * r.rate, rate=r.rate, timestamp=r.timestamp, ) for r in trade_rows ])
def test_json_roundtrip(i: int) -> None: class Model(BaseModel): c: Currency model = Model(c=Currency(i)) serialized = json.loads(model.json()) assert model == Model(**serialized)
def test_multiplication_commutative(n: int, s: float) -> None: assert Currency(n) * s == s * Currency(n)
def test_str(i: int, frac: int) -> None: assert str(Currency(100 * i + frac)) == f"{i}.{frac:02d}"
def test_multiplication_floats(n: int, s: float) -> None: assert Currency(n) * s == Currency(floor(n * s))
def test_multiplication_integers(n: int, m: int) -> None: assert Currency(n) * m == Currency(n * m)
def test_validation_ok(i: int) -> None: class Model(BaseModel): c: Currency assert Model(c=i).c == Currency(i) assert Model(c=Currency(i)).c == Currency(i)
def test_init_negative(i: int) -> None: with pytest.raises(ValueError): Currency(i)
def test_init_ok(i: int) -> None: assert i == Currency(i).value