def test_match_buy_against_complete_matching_sells(self): """ Test buy against complete matching sell. """ buy = Buy(qty=1, prc=1) sell1 = Sell(qty=1, prc=1) sell2 = Sell(qty=1, prc=2) fills1 = Book().match(sell1) fills2 = Book().match(sell2) fills3 = Book().match(buy) orders = Book().orders() expected = { "sells": [{ "qty": 1, "prc": 2 }], "buys": [], } assert fills1 == {"fills": []} assert fills2 == {"fills": []} assert fills3 == { "fills": [{ "qty": 1, "prc": 1, }] } assert orders == expected
def test_is_valid_with_not_valid_order(self): """ Test is_valid method with not valid order object. """ sell = Sell(qty=0, prc=1) flag = sell.is_valid() assert flag == False
def test_order_sub(self): """ Test __sub__ magic method on Order objects. """ buy = Buy(qty=1, prc=1) sell = Sell(qty=2, prc=1) diff = sell - buy expected = Sell(**{"qty": 1, "prc": 1}) assert diff.qty == expected.qty assert diff.prc == expected.prc
def post(self, **kwargs): order = None resp = None body = json.loads(self.request.body) if self.request.uri == "{}".format(constants.URL_PATH_BUY): order = Buy(**body) if self.request.uri == "{}".format(constants.URL_PATH_SELL): order = Sell(**body) if not order.is_valid(): resp = {"message": responses[constants.HTTP_400_BAD_REQUEST]} self.set_status(constants.HTTP_400_BAD_REQUEST) self.write(resp) return try: resp = Book().match(order) http_status_code = constants.HTTP_201_CREATED except Exception as e: resp = {"message": e.message} http_status_code = constants.HTTP_500_INTERNAL_SERVER_ERROR self.set_header( "location", "{}://{}{}".format( self.request.protocol, self.request.host, self.reverse_url("{}".format(constants.URL_NAME_BOOK)))) self.set_status(http_status_code) self.write(resp)
def test_match_sell_against_partial_matching_buy(self): """ Test sell against partial matching buys. """ Book()._clear() sell = Sell(qty=1, prc=2) buy1 = Buy(qty=1, prc=1) buy2 = Buy(qty=2, prc=2) fills1 = Book().match(buy1) fills2 = Book().match(buy2) fills3 = Book().match(sell) orders = Book().orders() expected = { "sells": [], "buys": [{ "qty": 1, "prc": 2 }, { "qty": 1, "prc": 1 }], } assert fills1 == {"fills": []} assert fills2 == {"fills": []} assert fills3 == { "fills": [{ "qty": 1, "prc": 2, }] } assert orders == expected
def test_orders(self): """ Test that un-matched orders return properly. """ Book()._clear() sell = Sell(qty=1, prc=3) buy = Buy(qty=1, prc=2) fills1 = Book().match(sell) fills2 = Book().match(buy) orders = Book().orders() expected = { "sells": [ { "qty": 1, "prc": 3 }, ], "buys": [ { "qty": 1, "prc": 2 }, ], } assert fills1 == {"fills": []} assert fills2 == {"fills": []} assert orders == expected
def test_post_order_sell(self): """ Test post order with sell. """ order = Buy(qty=1, prc=1) entry = Sell(qty=1, prc=1) flag = Book()._post(order, entry) assert flag is True
def test_ismatch_sell(self): """ Test that order=Sell matches entry=Buy. """ order = Sell(qty=1, prc=1) entry = Buy(qty=1, prc=1) flag = Book()._ismatch(order, entry) assert flag is True
def test_order_sub_short(self): """ Test __sub__ magic method on Order objects. """ buy = Buy(qty=2, prc=1) sell = Sell(qty=1, prc=1) with pytest.raises(Exception) as excinfo: diff = sell - buy assert excinfo.value[0] == "Short of shares to fill."