Skip to content

Commit

Permalink
Fixed more flak8 formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
masonhargrave committed Jan 4, 2025
1 parent c1a63b7 commit 3637c1f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def decorated(*args, **kwargs):

except jwt.ExpiredSignatureError:
raise AuthError(
{"code": "token_expired", "description": "Token is expired"}, 401
{"code": "token_expired", "description": "Token is expired"}, 401,
)
except jwt.JWTClaimsError:
raise AuthError(
Expand Down
1 change: 0 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# models.py

import time
import uuid
from dataclasses import dataclass, field


Expand Down
9 changes: 5 additions & 4 deletions order_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from typing import List
import uuid
from sqlalchemy.orm import Session

from models import Order, Trade
from db_models import DbOrder, DbTrade
Expand Down Expand Up @@ -51,7 +50,8 @@ def add_order(self, order: Order) -> List[Trade]:

def _match_buy(self, buy_order: Order) -> List[Trade]:
"""
Match a BUY order against the best available SELL orders in ascending price order.
Match a BUY order against the best available SELL orders
in ascending price order.
:param buy_order: The BUY Order object to be matched.
:return: A list of executed Trade objects from this matching session.
Expand Down Expand Up @@ -106,7 +106,8 @@ def _match_buy(self, buy_order: Order) -> List[Trade]:

def _match_sell(self, sell_order: Order) -> List[Trade]:
"""
Match a SELL order against the best available BUY orders in descending price order.
Match a SELL order against the best available
BUY orders in descending price order.
:param sell_order: The SELL Order object to be matched.
:return: A list of executed Trade objects from this matching session.
Expand Down Expand Up @@ -200,7 +201,7 @@ def _save_trade(self, trade: Trade) -> None:
db.commit()
except Exception as e:
db.rollback()
logger.error(f"Error inserting trade {trade.trade_id} in DB: {e}")
logger.error(f"Error inserting trade {trade.trade_id} in DB: {e}")

def get_order_book(self):
"""
Expand Down
21 changes: 18 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def test_create_order_with_auth(client, valid_token):
headers = {"Authorization": f"Bearer {valid_token}"}
response = client.post(
"/orders",
json={"user_id": "Alice", "side": "BUY", "price": 100.0, "quantity": 10},
json={
"user_id": "Alice",
"side": "BUY",
"price": 100.0,
"quantity": 10,
},
headers=headers,
)
assert response.status_code == 200
Expand All @@ -61,7 +66,12 @@ def test_create_order_without_auth(client):
"""Test creating an order without JWT"""
response = client.post(
"/orders",
json={"user_id": "Alice", "side": "BUY", "price": 100.0, "quantity": 10},
json={
"user_id": "Alice",
"side": "BUY",
"price": 100.0,
"quantity": 10,
},
)
assert response.status_code == 401
data = response.get_json()
Expand All @@ -78,7 +88,12 @@ def test_rate_limiting(client, valid_token):
for i in range(9):
response = client.post(
"/orders",
json={"user_id": "Bob", "side": "SELL", "price": 95.0, "quantity": 5},
json={
"user_id": "Bob",
"side": "SELL",
"price": 95.0,
"quantity": 5,
},
headers=headers,
)
assert response.status_code == 200, f"Request {i + 1} failed unexpectedly"
Expand Down
13 changes: 6 additions & 7 deletions tests/test_order_book.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from order_book import OrderBook
from models import Order

Expand All @@ -11,7 +10,7 @@ def test_add_buy_order_no_match():
"""
ob = OrderBook()
buy_order = Order(
order_id="test_buy_1", user_id="UserA", side="BUY", price=100.0, quantity=10
order_id="test_buy_1", user_id="UserA", side="BUY", price=100.0, quantity=10,
)

trades = ob.add_order(buy_order)
Expand All @@ -31,7 +30,7 @@ def test_add_sell_order_no_match():
"""
ob = OrderBook()
sell_order = Order(
order_id="test_sell_1", user_id="UserB", side="SELL", price=105.0, quantity=5
order_id="test_sell_1", user_id="UserB", side="SELL", price=105.0, quantity=5,
)

trades = ob.add_order(sell_order)
Expand All @@ -52,13 +51,13 @@ def test_buy_and_sell_match_exact():

# Add BUY
buy_order = Order(
order_id="test_buy_2", user_id="UserA", side="BUY", price=100.0, quantity=5
order_id="test_buy_2", user_id="UserA", side="BUY", price=100.0, quantity=5,
)
ob.add_order(buy_order)

# Add SELL that matches (SELL price <= BUY price)
sell_order = Order(
order_id="test_sell_2", user_id="UserB", side="SELL", price=100.0, quantity=5
order_id="test_sell_2", user_id="UserB", side="SELL", price=100.0, quantity=5,
)
trades = ob.add_order(sell_order)

Expand All @@ -84,13 +83,13 @@ def test_partial_fill():

# BUY 10 @ 100
buy_order = Order(
order_id="buy_partial", user_id="UserA", side="BUY", price=100.0, quantity=10
order_id="buy_partial", user_id="UserA", side="BUY", price=100.0, quantity=10,
)
ob.add_order(buy_order)

# SELL 6 @ 95
sell_order = Order(
order_id="sell_partial", user_id="UserB", side="SELL", price=95.0, quantity=6
order_id="sell_partial", user_id="UserB", side="SELL", price=95.0, quantity=6,
)
trades = ob.add_order(sell_order)

Expand Down

0 comments on commit 3637c1f

Please sign in to comment.