def test_request_pair_api_returns_empty_result_for_empty_api(self): left, right = 'BTC', 'USD' self.assertEquals(BitstampCrawler.request_pair_api("", left, right), "") self.assertEquals(BitstampCrawler.request_pair_api(None, left, right), "")
def test_save_pair_ticker_for_not_existing_pair(self): ticker = BitstampCrawler.parse_pair_ticker(self.ticker_response) exchange_pair = ExchangePair() result = BitstampCrawler.save_pair_ticker(exchange_pair, ticker[0], ticker[1]) self.assertFalse(result)
def test_save_pair_orderbook_for_invalid_pair(self): orderbook = BitstampCrawler.parse_pair_orderbook( self.orderbook_response) result = BitstampCrawler.save_pair_orderbook(None, orderbook[0], orderbook[1]) self.assertFalse(result)
def test_parse_pair_ticker_returns_empty_list_for_empty_response(self): result = BitstampCrawler.parse_pair_ticker("") self.assertIsNone(result[0]) self.assertIsNone(result[1]) result = BitstampCrawler.parse_pair_ticker(None) self.assertIsNone(result[0]) self.assertIsNone(result[1])
def test_save_pair_ticker_for_valid_pair_and_empty_orderbook(self): exchange = self.exchange crawler = BitstampCrawler(exchange) result = BitstampCrawler.save_pair_ticker( crawler.exchange.pairs.first(), None, None) self.assertFalse(result)
def test_save_pair_orderbook_for_not_existing_pair(self): orderbook = BitstampCrawler.parse_pair_orderbook( self.orderbook_response) exchange_pair = ExchangePair() result = BitstampCrawler.save_pair_orderbook(exchange_pair, orderbook[0], orderbook[1]) self.assertFalse(result)
def test_request_pair_api_raise_exception_for_invalid_url(self): exchange = self.exchange exchange.orderbook_api = 'invalid_api_url' left, right = 'BTC', 'USD' with self.assertRaises(ConnectionError) as context: BitstampCrawler.request_pair_api(exchange.orderbook_api, left, right) self.assertTrue('Api request failed!' in str(context.exception))
def test_save_pair_ticker_for_valid_inputs(self): ticker = BitstampCrawler.parse_pair_ticker(self.ticker_response) exchange = self.exchange crawler = BitstampCrawler(exchange) exchange_pair = crawler.exchange.pairs.first() result = BitstampCrawler.save_pair_ticker(exchange_pair, ticker[0], ticker[1]) self.assertTrue(result) self.assertEqual(exchange_pair.last_bid, self.saved_ticker_bid) self.assertEqual(exchange_pair.last_ask, self.saved_ticker_ask)
def test_save_pair_orderbook_for_valid_inputs(self): orderbook = BitstampCrawler.parse_pair_orderbook( self.orderbook_response) exchange = self.exchange crawler = BitstampCrawler(exchange) exchange_pair = crawler.exchange.pairs.first() result = BitstampCrawler.save_pair_orderbook(exchange_pair, orderbook[0], orderbook[1]) self.assertTrue(result) self.assertEqual(exchange_pair.bids, self.saved_orderbook_bids) self.assertEqual(exchange_pair.asks, self.saved_orderbook_asks)
def test_parse_pair_orderbook_returns_empty_lists_for_empty_response(self): result = BitstampCrawler.parse_pair_orderbook("") self.assertEquals(type(result[0]), list) self.assertFalse(result[0]) self.assertEquals(type(result[1]), list) self.assertFalse(result[1]) result = BitstampCrawler.parse_pair_orderbook(None) self.assertEquals(type(result[0]), list) self.assertFalse(result[0]) self.assertEquals(type(result[1]), list) self.assertFalse(result[1])
def test_init_raise_exception_for_mismatched_exchange(self): exchange = Exchange.objects.get(name="Cex.io") with self.assertRaises(TypeError) as context: BitstampCrawler(exchange) self.assertTrue('Mismatched Exchange' in str(context.exception))
def test_request_pair_api_returns_any_result_for_exchange_url(self): exchange = self.exchange exchange.orderbook_api = self.url left, right = 'BTC', 'USD' result = BitstampCrawler.request_pair_api(exchange.orderbook_api, left, right) self.assertTrue(result)
def test_parse_pair_ticker_returns_parsed_data_for_mocked_response(self): response = self.ticker_response result = BitstampCrawler.parse_pair_ticker(response) self.assertTrue(result[0]) # "bid":17152.01 self.assertEquals(result[0], 17152.01) self.assertTrue(result[1]) # "ask":17699.94 self.assertEquals(result[1], 17699.94)
def test_request_pair_api_returns_proper_result_for_orderbook_api(self): exchange = self.exchange exchange.orderbook_api = self.orderbook_api left, right = 'BTC', 'USD' result = BitstampCrawler.request_pair_api(exchange.orderbook_api, left, right) self.assertTrue('"bids":' in result) self.assertTrue('"asks":' in result)
def test_parse_pair_orderbook_returns_parsed_data_for_mocked_response( self): response = self.orderbook_response result = BitstampCrawler.parse_pair_orderbook(response) self.assertTrue(result[0]) # [17151.01, 0.055] self.assertEquals(len(result[0]), 1) self.assertEquals(result[0][0][0], 17151.01) self.assertEquals(result[0][0][1], 0.055) self.assertTrue(result[1]) # [17699.99, 0.01529216] self.assertEquals(len(result[1]), 1) self.assertEquals(result[1][0][0], 17699.99) self.assertEquals(result[1][0][1], 0.01529216)
def test_save_pair_ticker_for_invalid_pair(self): ticker = BitstampCrawler.parse_pair_ticker(self.ticker_response) result = BitstampCrawler.save_pair_ticker(None, ticker[0], ticker[1]) self.assertFalse(result)
def test_init_raise_exception_for_wrong_input(self): with self.assertRaises(TypeError) as context: BitstampCrawler([]) self.assertTrue( 'Given argument is not Exchange type' in str(context.exception))
def test_crawler_is_properly_created(self): exchange = self.exchange crawler = BitstampCrawler(exchange) self.assertEquals(exchange.id, crawler.exchange.id)