Exemplo n.º 1
0
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
    ])
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
 def test_multiplication_commutative(n: int, s: float) -> None:
     assert Currency(n) * s == s * Currency(n)
Exemplo n.º 4
0
 def test_str(i: int, frac: int) -> None:
     assert str(Currency(100 * i + frac)) == f"{i}.{frac:02d}"
Exemplo n.º 5
0
 def test_multiplication_floats(n: int, s: float) -> None:
     assert Currency(n) * s == Currency(floor(n * s))
Exemplo n.º 6
0
 def test_multiplication_integers(n: int, m: int) -> None:
     assert Currency(n) * m == Currency(n * m)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
 def test_init_negative(i: int) -> None:
     with pytest.raises(ValueError):
         Currency(i)
Exemplo n.º 9
0
 def test_init_ok(i: int) -> None:
     assert i == Currency(i).value