Пример #1
0
 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
Пример #2
0
 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
Пример #3
0
 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
Пример #4
0
 def test_book_instance(self):
     """
     Test that Book() is a singleton.
     """
     book1 = Book()
     book2 = Book()
     assert id(book1) == id(book2)
Пример #5
0
 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)
Пример #6
0
 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
Пример #7
0
 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
Пример #8
0
 def get(self):
     ret = Book().orders()
     self.write(ret)