class TestDatabase(AbstractServer): @inlineCallbacks def setUp(self): yield super(TestDatabase, self).setUp() path = os.path.join(self.getStateDir(), 'sqlite') if not os.path.exists(path): os.makedirs(path) self.database = MarketDB(self.getStateDir(), 'market') self.order_id1 = OrderId(TraderId(b'3'), OrderNumber(4)) self.order_id2 = OrderId(TraderId(b'4'), OrderNumber(5)) self.order1 = Order(self.order_id1, AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')), Timeout(3600), Timestamp.now(), True) self.order2 = Order(self.order_id2, AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')), Timeout(3600), Timestamp.now(), False) self.order2.reserve_quantity_for_tick(OrderId(TraderId(b'3'), OrderNumber(4)), 3) self.transaction_id1 = TransactionId(TraderId(b"0"), TransactionNumber(4)) self.transaction1 = Transaction(self.transaction_id1, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), OrderId(TraderId(b"0"), OrderNumber(1)), OrderId(TraderId(b"1"), OrderNumber(2)), Timestamp(20.0)) self.payment1 = Payment(TraderId(b"0"), self.transaction_id1, AssetAmount(5, 'BTC'), WalletAddress('abc'), WalletAddress('def'), PaymentId("abc"), Timestamp(20.0), False) self.transaction1.add_payment(self.payment1) def test_add_get_order(self): """ Test the insertion and retrieval of an order in the database """ self.database.add_order(self.order1) self.database.add_order(self.order2) orders = self.database.get_all_orders() self.assertEqual(len(orders), 2) def test_get_specific_order(self): """ Test the retrieval of a specific order """ order_id = OrderId(TraderId(b'3'), OrderNumber(4)) self.assertIsNone(self.database.get_order(order_id)) self.database.add_order(self.order1) self.assertIsNotNone(self.database.get_order(order_id)) def test_delete_order(self): """ Test the deletion of an order from the database """ self.database.add_order(self.order1) self.assertEqual(len(self.database.get_all_orders()), 1) self.database.delete_order(self.order_id1) self.assertEqual(len(self.database.get_all_orders()), 0) def test_get_next_order_number(self): """ Test the retrieval of the next order number from the database """ self.assertEqual(self.database.get_next_order_number(), 1) self.database.add_order(self.order1) self.assertEqual(self.database.get_next_order_number(), 5) def test_add_delete_reserved_ticks(self): """ Test the retrieval, addition and deletion of reserved ticks in the database """ self.database.add_reserved_tick(self.order_id1, self.order_id2, self.order1.total_quantity) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 1) self.database.delete_reserved_ticks(self.order_id1) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 0) def test_add_get_transaction(self): """ Test the insertion and retrieval of a transaction in the database """ self.database.add_transaction(self.transaction1) transactions = self.database.get_all_transactions() self.assertEqual(len(transactions), 1) self.assertEqual(len(self.database.get_payments(self.transaction1.transaction_id)), 1) def test_insert_or_update_transaction(self): """ Test the conditional insertion or update of a transaction in the database """ # Test insertion self.database.insert_or_update_transaction(self.transaction1) transactions = self.database.get_all_transactions() self.assertEqual(len(transactions), 1) # Test try to update with older timestamp before_trans1 = Transaction(self.transaction1.transaction_id, self.transaction1.assets, self.transaction1.order_id, self.transaction1.partner_order_id, Timestamp(float(self.transaction1.timestamp) - 1.0)) self.database.insert_or_update_transaction(before_trans1) transaction = self.database.get_transaction(self.transaction1.transaction_id) self.assertEqual(float(transaction.timestamp), float(self.transaction1.timestamp)) # Test update with newer timestamp after_trans1 = Transaction(self.transaction1.transaction_id, self.transaction1.assets, self.transaction1.order_id, self.transaction1.partner_order_id, Timestamp(float(self.transaction1.timestamp) + 1.0)) self.database.insert_or_update_transaction(after_trans1) transaction = self.database.get_transaction(self.transaction1.transaction_id) self.assertEqual(float(transaction.timestamp), float(after_trans1.timestamp)) def test_get_specific_transaction(self): """ Test the retrieval of a specific transaction """ transaction_id = TransactionId(TraderId(b'0'), TransactionNumber(4)) self.assertIsNone(self.database.get_transaction(transaction_id)) self.database.add_transaction(self.transaction1) self.assertIsNotNone(self.database.get_transaction(transaction_id)) def test_delete_transaction(self): """ Test the deletion of a transaction from the database """ self.database.add_transaction(self.transaction1) self.assertEqual(len(self.database.get_all_transactions()), 1) self.database.delete_transaction(self.transaction_id1) self.assertEqual(len(self.database.get_all_transactions()), 0) def test_get_next_transaction_number(self): """ Test the retrieval of the next transaction number from the database """ self.assertEqual(self.database.get_next_transaction_number(), 1) self.database.add_transaction(self.transaction1) self.assertEqual(self.database.get_next_transaction_number(), 5) def test_add_get_payment(self): """ Test the insertion and retrieval of a payment in the database """ self.database.add_payment(self.payment1) payments = self.database.get_payments(self.transaction_id1) self.assertEqual(len(payments), 1) def test_add_remove_tick(self): """ Test addition, retrieval and deletion of ticks in the database """ ask = Tick.from_order(self.order1) self.database.add_tick(ask) bid = Tick.from_order(self.order2) self.database.add_tick(bid) self.assertEqual(len(self.database.get_ticks()), 2) self.database.delete_all_ticks() self.assertEqual(len(self.database.get_ticks()), 0) def test_add_get_trader_identity(self): """ Test the addition and retrieval of a trader identity in the database """ self.database.add_trader_identity(TraderId(b"a"), "123", 1234) self.database.add_trader_identity(TraderId(b"b"), "124", 1235) traders = self.database.get_traders() self.assertEqual(len(traders), 2) def test_check_database(self): """ Test the check of the database """ self.assertEqual(self.database.check_database(six.text_type(LATEST_DB_VERSION)), LATEST_DB_VERSION) def test_get_upgrade_script(self): """ Test fetching the upgrade script of the database """ self.assertTrue(self.database.get_upgrade_script(1)) def test_db_upgrade(self): self.database.execute(u"DROP TABLE orders;") self.database.execute(u"DROP TABLE ticks;") self.database.execute(u"CREATE TABLE orders(x INTEGER PRIMARY KEY ASC);") self.database.execute(u"CREATE TABLE ticks(x INTEGER PRIMARY KEY ASC);") self.assertEqual(self.database.check_database(u"1"), 3)
class TestDatabase(AbstractServer): @blocking_call_on_reactor_thread @inlineCallbacks def setUp(self, annotate=True): yield super(TestDatabase, self).setUp(annotate=annotate) path = os.path.join(self.getStateDir(), 'sqlite') if not os.path.exists(path): os.makedirs(path) self.database = MarketDB(self.getStateDir()) self.order_id1 = OrderId(TraderId('3'), OrderNumber(4)) self.order_id2 = OrderId(TraderId('4'), OrderNumber(5)) self.order1 = Order(self.order_id1, Price(5, 'EUR'), Quantity(6, 'BTC'), Timeout(3600), Timestamp.now(), True) self.order2 = Order(self.order_id2, Price(5, 'EUR'), Quantity(6, 'BTC'), Timeout(3600), Timestamp.now(), False) self.transaction_id1 = TransactionId(TraderId("0"), TransactionNumber(4)) self.transaction1 = Transaction(self.transaction_id1, Price(100, 'BTC'), Quantity(30, 'MC'), OrderId(TraderId("0"), OrderNumber(1)), OrderId(TraderId("1"), OrderNumber(2)), Timestamp(20.0)) self.payment1 = Payment(MessageId(TraderId("0"), MessageNumber("4")), self.transaction_id1, Quantity(5, 'MC'), Price(6, 'BTC'), WalletAddress('abc'), WalletAddress('def'), PaymentId("abc"), Timestamp(20.0), False) self.transaction1.add_payment(self.payment1) @blocking_call_on_reactor_thread def test_add_get_order(self): """ Test the insertion and retrieval of an order in the database """ self.database.add_order(self.order1) orders = self.database.get_all_orders() self.assertEqual(len(orders), 1) @blocking_call_on_reactor_thread def test_get_specific_order(self): """ Test the retrieval of a specific order """ order_id = OrderId(TraderId('3'), OrderNumber(4)) self.assertIsNone(self.database.get_order(order_id)) self.database.add_order(self.order1) self.assertIsNotNone(self.database.get_order(order_id)) @blocking_call_on_reactor_thread def test_delete_order(self): """ Test the deletion of an order from the database """ self.database.add_order(self.order1) self.assertEqual(len(self.database.get_all_orders()), 1) self.database.delete_order(self.order_id1) self.assertEqual(len(self.database.get_all_orders()), 0) @blocking_call_on_reactor_thread def test_get_next_order_number(self): """ Test the retrieval of the next order number from the database """ self.assertEqual(self.database.get_next_order_number(), 1) self.database.add_order(self.order1) self.assertEqual(self.database.get_next_order_number(), 5) @blocking_call_on_reactor_thread def test_add_delete_reserved_ticks(self): """ Test the retrieval, addition and deletion of reserved ticks in the database """ self.database.add_reserved_tick(self.order_id1, self.order_id2, self.order1.total_quantity) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 1) self.database.delete_reserved_ticks(self.order_id1) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 0) @blocking_call_on_reactor_thread def test_add_get_transaction(self): """ Test the insertion and retrieval of a transaction in the database """ self.database.add_transaction(self.transaction1) transactions = self.database.get_all_transactions() self.assertEqual(len(transactions), 1) self.assertEqual( len(self.database.get_payments(self.transaction1.transaction_id)), 1) @blocking_call_on_reactor_thread def test_get_specific_transaction(self): """ Test the retrieval of a specific transaction """ transaction_id = TransactionId(TraderId('0'), TransactionNumber(4)) self.assertIsNone(self.database.get_transaction(transaction_id)) self.database.add_transaction(self.transaction1) self.assertIsNotNone(self.database.get_transaction(transaction_id)) @blocking_call_on_reactor_thread def test_delete_transaction(self): """ Test the deletion of a transaction from the database """ self.database.add_transaction(self.transaction1) self.assertEqual(len(self.database.get_all_transactions()), 1) self.database.delete_transaction(self.transaction_id1) self.assertEqual(len(self.database.get_all_transactions()), 0) @blocking_call_on_reactor_thread def test_get_next_transaction_number(self): """ Test the retrieval of the next transaction number from the database """ self.assertEqual(self.database.get_next_transaction_number(), 1) self.database.add_transaction(self.transaction1) self.assertEqual(self.database.get_next_transaction_number(), 5) @blocking_call_on_reactor_thread def test_add_get_payment(self): """ Test the insertion and retrieval of a payment in the database """ self.database.add_payment(self.payment1) payments = self.database.get_payments(self.transaction_id1) self.assertEqual(len(payments), 1) @blocking_call_on_reactor_thread def test_add_remove_tick(self): """ Test addition, retrieval and deletion of ticks in the database """ ask = Tick.from_order( self.order1, MessageId(TraderId('0'), MessageNumber('message_number'))) self.database.add_tick(ask) bid = Tick.from_order( self.order2, MessageId(TraderId('0'), MessageNumber('message_number'))) self.database.add_tick(bid) self.assertEqual(len(self.database.get_ticks()), 2) self.database.delete_all_ticks() self.assertEqual(len(self.database.get_ticks()), 0) @blocking_call_on_reactor_thread def test_add_get_trader_identity(self): """ Test the addition and retrieval of a trader identity in the database """ self.database.add_trader_identity("a", "123", 1234) self.database.add_trader_identity("b", "124", 1235) traders = self.database.get_traders() self.assertEqual(len(traders), 2) self.assertEqual(traders, [("a", "123", 1234), ("b", "124", 1235)]) @blocking_call_on_reactor_thread def test_check_database(self): """ Test the check of the database """ self.assertEqual( self.database.check_database(unicode(LATEST_DB_VERSION)), LATEST_DB_VERSION)
class TransactionTestSuite(unittest.TestCase): """Transaction test cases.""" def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1)) self.transaction = Transaction(self.transaction_id, Price(100, 'BTC'), Quantity(30, 'MC'), OrderId(TraderId('3'), OrderNumber(2)), OrderId(TraderId('2'), OrderNumber(1)), Timestamp(0.0)) self.proposed_trade = Trade.propose( MessageId(TraderId('0'), MessageNumber('1')), OrderId(TraderId('0'), OrderNumber(2)), OrderId(TraderId('1'), OrderNumber(3)), Price(100, 'BTC'), Quantity(30, 'MC'), Timestamp(0.0)) self.payment = Payment( MessageId(TraderId("0"), MessageNumber("1")), TransactionId(TraderId('2'), TransactionNumber(2)), Quantity(3, 'MC'), Price(2, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4.0), True) def test_from_proposed_trade(self): # Test from proposed trade transaction = Transaction.from_proposed_trade(self.proposed_trade, self.transaction_id) self.assertEqual(transaction.price, self.transaction.price) self.assertEqual(transaction.total_quantity, self.transaction.total_quantity) self.assertEqual(transaction.timestamp, self.transaction.timestamp) def test_unitize(self): """ Test the unitize method of a Transaction """ self.assertEqual(Transaction.unitize(1, 1), 1) self.assertEqual(Transaction.unitize(0.03, 0.02), 0.04) self.assertEqual(Transaction.unitize(50, 0.05), 50) self.assertEqual(Transaction.unitize(50.1818, 25), 75) def test_add_payment(self): """ Test the addition of a payment to a transaction """ self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.transferred_price, Price(2, 'BTC')) self.assertEqual(self.transaction.transferred_quantity, Quantity(3, 'MC')) self.assertTrue(self.transaction.payments) def test_last_payment(self): """ Test the retrieval of the last payment """ self.assertIsNone(self.transaction.last_payment(True)) self.assertIsNone(self.transaction.last_payment(False)) self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.last_payment(True), self.payment) self.assertEqual(self.transaction.last_payment(False), self.payment) def test_next_payment(self): """ Test the process of determining the next payment details during a transaction """ def set_transaction_data(trans_price, trans_quantity, payment_price, payment_quantity): self.transaction._transferred_price = trans_price self.transaction._transferred_quantity = trans_quantity self.payment._transferee_price = payment_price self.payment._transferee_quantity = payment_quantity self.transaction._payments = [self.payment] # No incremental payments self.assertEqual( self.transaction.next_payment(True, 1, incremental=False), Quantity(30, 'MC')) self.assertEqual( self.transaction.next_payment(False, 2, incremental=False), Price(3000, 'BTC')) self.assertEqual( self.transaction.next_payment(True, 1, incremental=True), Quantity(1, 'MC')) self.assertEqual( self.transaction.next_payment(False, 2, incremental=True), Price(2, 'BTC')) set_transaction_data(Price(1, 'BTC'), Quantity(1, 'MC'), Price(1, 'BTC'), Quantity(1, 'MC')) self.assertEqual( self.transaction.next_payment(True, 0.1, incremental=True), Quantity(0.2, 'MC')) # Test completion of trade set_transaction_data(Price(3000, 'BTC'), Quantity(29, 'MC'), Price(1, 'BTC'), Quantity(1, 'MC')) self.assertEqual( self.transaction.next_payment(True, 1, incremental=True), Quantity(1, 'MC')) set_transaction_data(Price(2900, 'BTC'), Quantity(30, 'MC'), Price(1, 'BTC'), Quantity(1, 'MC')) self.assertEqual( self.transaction.next_payment(False, 1, incremental=True), Price(100, 'BTC')) # Test whether we don't transfer too much set_transaction_data(Price(2999, 'BTC'), Quantity(29, 'MC'), Price(2999, 'BTC'), Quantity(1, 'MC')) self.assertEqual( self.transaction.next_payment(True, 1, incremental=True), Quantity(1, 'MC')) set_transaction_data(Price(2999, 'BTC'), Quantity(29, 'MC'), Price(1, 'BTC'), Quantity(29, 'MC')) self.assertEqual( self.transaction.next_payment(False, 1, incremental=True), Price(1, 'BTC')) def test_to_dictionary(self): """ Test the to dictionary method of a transaction """ self.assertDictEqual( self.transaction.to_dictionary(), { 'trader_id': '0', 'transaction_number': 1, 'order_number': 2, 'partner_trader_id': '2', 'partner_order_number': 1, 'payment_complete': False, 'price': 100.0, 'price_type': 'BTC', 'quantity': 30.0, 'quantity_type': 'MC', 'transferred_price': 0.0, 'transferred_quantity': 0.0, 'timestamp': 0.0, 'status': 'pending' }) def test_status(self): """ Test the status of a transaction """ self.assertEqual(self.transaction.status, 'pending') self.payment._success = False self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.status, 'error')
class TransactionTestSuite(unittest.TestCase): """Transaction test cases.""" def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1)) self.transaction = Transaction(self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), OrderId(TraderId('3'), OrderNumber(2)), OrderId(TraderId('2'), OrderNumber(1)), Timestamp(0.0)) self.proposed_trade = Trade.propose(TraderId('0'), OrderId(TraderId('0'), OrderNumber(2)), OrderId(TraderId('1'), OrderNumber(3)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), Timestamp(0.0)) self.payment = Payment(TraderId("0"), TransactionId(TraderId('2'), TransactionNumber(2)), AssetAmount(3, 'MB'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4.0), True) def test_from_proposed_trade(self): """ Test creating a transaction from a proposed trade """ transaction = Transaction.from_proposed_trade(self.proposed_trade, self.transaction_id) self.assertEqual(transaction.assets, self.transaction.assets) def test_add_payment(self): """ Test the addition of a payment to a transaction """ self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.transferred_assets.first.amount, 0) self.assertEqual(self.transaction.transferred_assets.second.amount, 3) self.assertTrue(self.transaction.payments) def test_next_payment(self): """ Test the process of determining the next payment details during a transaction """ self.assertEqual(self.transaction.next_payment(True), AssetAmount(100, 'BTC')) self.assertEqual(self.transaction.next_payment(False), AssetAmount(100, 'MB')) def test_is_payment_complete(self): """ Test whether a payment is correctly marked as complete """ self.assertFalse(self.transaction.is_payment_complete()) self.transaction.add_payment(self.payment) self.assertFalse(self.transaction.is_payment_complete()) self.transaction._transferred_assets = AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')) self.assertTrue(self.transaction.is_payment_complete()) def test_to_dictionary(self): """ Test the to dictionary method of a transaction """ self.assertDictEqual(self.transaction.to_dictionary(), { 'trader_id': '0', 'transaction_number': 1, 'order_number': 2, 'partner_trader_id': '2', 'partner_order_number': 1, 'payment_complete': False, 'assets': { 'first': { 'amount': 100, 'type': 'BTC', }, 'second': { 'amount': 100, 'type': 'MB' } }, 'transferred': { 'first': { 'amount': 0, 'type': 'BTC', }, 'second': { 'amount': 0, 'type': 'MB' } }, 'timestamp': 0.0, 'status': 'pending' }) def test_status(self): """ Test the status of a transaction """ self.assertEqual(self.transaction.status, 'pending') self.payment._success = False self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.status, 'error')
class TransactionTestSuite(unittest.TestCase): """Transaction test cases.""" def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId("0"), TransactionNumber(1)) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), OrderId(TraderId('3'), OrderNumber(2)), OrderId(TraderId('2'), OrderNumber(1)), Timestamp(0.0)) self.proposed_trade = Trade.propose( TraderId('0'), OrderId(TraderId('0'), OrderNumber(2)), OrderId(TraderId('1'), OrderNumber(3)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), Timestamp(0.0)) self.payment = Payment( TraderId("0"), TransactionId(TraderId('2'), TransactionNumber(2)), AssetAmount(3, 'MB'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4.0), True) def test_from_proposed_trade(self): """ Test creating a transaction from a proposed trade """ transaction = Transaction.from_proposed_trade(self.proposed_trade, self.transaction_id) self.assertEqual(transaction.assets, self.transaction.assets) def test_add_payment(self): """ Test the addition of a payment to a transaction """ self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.transferred_assets.first.amount, 0) self.assertEqual(self.transaction.transferred_assets.second.amount, 3) self.assertTrue(self.transaction.payments) def test_next_payment(self): """ Test the process of determining the next payment details during a transaction """ self.assertEqual(self.transaction.next_payment(True), AssetAmount(100, 'BTC')) self.assertEqual(self.transaction.next_payment(False), AssetAmount(100, 'MB')) def test_is_payment_complete(self): """ Test whether a payment is correctly marked as complete """ self.assertFalse(self.transaction.is_payment_complete()) self.transaction.add_payment(self.payment) self.assertFalse(self.transaction.is_payment_complete()) self.transaction._transferred_assets = AssetPair( AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')) self.assertTrue(self.transaction.is_payment_complete()) def test_to_dictionary(self): """ Test the to dictionary method of a transaction """ self.assertDictEqual( self.transaction.to_dictionary(), { 'trader_id': '0', 'transaction_number': 1, 'order_number': 2, 'partner_trader_id': '2', 'partner_order_number': 1, 'payment_complete': False, 'assets': { 'first': { 'amount': 100, 'type': 'BTC', }, 'second': { 'amount': 100, 'type': 'MB' } }, 'transferred': { 'first': { 'amount': 0, 'type': 'BTC', }, 'second': { 'amount': 0, 'type': 'MB' } }, 'timestamp': 0.0, 'status': 'pending' }) def test_status(self): """ Test the status of a transaction """ self.assertEqual(self.transaction.status, 'pending') self.payment._success = False self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.status, 'error')