def test_save_to_db_price_log(self): # Insert new rows coins = [None, None] coins[0] = Coin("bitcoin", symbol="btc", price=123.123, price_previous=123, in_message=False, last_updated=datetime.utcnow()) coins[1] = Coin("testing123", symbol=None, price=None, price_previous=None, in_message=False, last_updated=datetime.utcnow()) backend.update_db_coin_data(coins, db_conn) db_conn.commit() backend.save_to_db_price_log(db_conn) cursor = db_conn.execute("SELECT * FROM price_log;") for index, row in enumerate(cursor): self.assertEqual(type(row[0]), datetime) self.assertGreaterEqual(datetime.utcnow(), row[0]) self.assertEqual(row[1], coins[index].get_id()) self.assertEqual(row[2], coins[index].get_symbol()) self.assertEqual(row[3], coins[index].get_price())
def test_read_coins_from_db(self): # Read new coins coins = [None, None] coins[0] = Coin("bitcoin", symbol="btc", price=123.123, price_previous=123, in_message=False, last_updated=datetime.utcnow()) coins[1] = Coin("testing123", symbol=None, price=None, price_previous=None, in_message=False, last_updated=datetime.utcnow()) backend.add_currency("bitcoin", db_conn) backend.add_currency("testing123", db_conn) backend.update_db_coin_data(coins, db_conn) db_conn.commit() coins_read = backend.read_coins_from_db(db_conn, []) self.assertEqual(len(coins_read), len(coins)) self.assertEqual(coins_read[0].get_id(), coins[0].get_id()) self.assertEqual(coins_read[0].get_in_message(), coins[0].get_in_message()) self.assertEqual(coins_read[0].get_symbol(), coins[0].get_symbol()) self.assertEqual(coins_read[0].get_price(), coins[0].get_price()) self.assertAlmostEqual(coins_read[0].get_price_change(), 0.123) self.assertEqual(coins_read[0].get_price_previous(), 123) self.assertEqual(coins_read[0].get_last_updated(), coins[0].get_last_updated()) self.assertEqual(coins_read[1].get_id(), coins[1].get_id()) self.assertEqual(coins_read[1].get_in_message(), coins[1].get_in_message()) self.assertEqual(coins_read[1].get_symbol(), coins[1].get_symbol()) self.assertEqual(coins_read[1].get_price(), coins[1].get_price()) self.assertEqual(coins_read[1].get_price_change(), None) self.assertEqual(coins_read[1].get_last_updated(), coins[1].get_last_updated()) # Read existing coins backend.update_db_coin_data(coins, db_conn) db_conn.commit() coins_read = backend.read_coins_from_db(db_conn, coins) self.assertEqual(len(coins_read), len(coins)) self.assertEqual(coins_read[0].get_id(), coins[0].get_id()) self.assertEqual(coins_read[0].get_in_message(), coins[0].get_in_message()) self.assertEqual(coins_read[0].get_symbol(), coins[0].get_symbol()) self.assertEqual(coins_read[0].get_price(), coins[0].get_price()) self.assertEqual(coins_read[0].get_price_change(), coins[0].get_price_change()) self.assertEqual(coins_read[0].get_last_updated(), coins[0].get_last_updated())
def run(): db_conn = sqlite3.connect(db_path) # Socket connection to dashboard address = ('localhost', dashboard_conn_socket) dashboard_conn = Client(address) # General setup coingecko = CoinGeckoApi() coin_info = backend.get_coins_list( coingecko, dashboard_conn=dashboard_conn) # available coin information coins = [] override_message = None # Main loop while True: coins = backend.read_coins_from_db( db_conn, coins) # adding/deleting coins is controlled by the dashboard for coin in coins: coin.refresh(coin_info, coingecko, vs_currency, dashboard_conn=dashboard_conn) backend.update_db_coin_data(coins, db_conn) print(backend.generate_print_str(coins, db_conn, override_message)) # Listens for messages from dashboard during sleep override_message = backend.sleep_with_interrupt( dashboard_conn, update_interval, override_message)
def test_update_db_coin_data(self): # Insert 2 new rows coins = [None, None] coins[0] = Coin("bitcoin", symbol="btc", price=123.123, price_previous=123, in_message=False, last_updated=datetime.utcnow()) coins[1] = Coin("testing123", symbol=None, price=None, price_previous=None, in_message=False, last_updated=datetime.utcnow()) backend.add_currency("bitcoin", db_conn) backend.add_currency("testing123", db_conn) backend.update_db_coin_data(coins, db_conn) cursor = db_conn.execute("SELECT * FROM coin_data;") db_conn.commit() for index, row in enumerate(cursor): self.assertIn(row[0], coins[index].get_id()) self.assertEqual(row[1], coins[index].get_symbol()) self.assertEqual(row[2], coins[index].get_price()) self.assertEqual(row[3], coins[index].get_price_previous()) self.assertEqual(row[4], coins[index].get_in_message()) self.assertEqual(type(row[5]), datetime) self.assertEqual(row[5], coins[index].get_last_updated()) # Update row coins = [None] coins[0] = Coin("bitcoin", symbol="btc", price=123.2, price_previous=123.123, in_message=False, last_updated=datetime.utcnow()) backend.update_db_coin_data(coins, db_conn) cursor = db_conn.execute("SELECT * FROM coin_data;") db_conn.commit() rows = cursor.fetchall() self.assertEqual(rows[0][0], coins[0].get_id()) self.assertEqual(rows[0][1], coins[0].get_symbol()) self.assertEqual(rows[0][2], coins[0].get_price()) self.assertEqual(rows[0][3], coins[0].get_price_previous()) self.assertEqual(rows[0][4], coins[0].get_in_message()) self.assertEqual(type(rows[0][5]), datetime) self.assertEqual(rows[0][5], coins[0].get_last_updated())
def test_save_prices_to_db(self): # Set up new coins coins = [None, None] coins[0] = Coin("bitcoin", symbol="btc", price=123.123, price_previous=123, in_message=False, last_updated=datetime.utcnow()) coins[1] = Coin("testing123", symbol=None, price=None, price_previous=None, in_message=False, last_updated=datetime.utcnow()) backend.add_currency("bitcoin", db_conn) backend.add_currency("testing123", db_conn) backend.update_db_coin_data(coins, db_conn) db_conn.commit() # Save, read and check backend.save_prices_to_db(db_conn) cursor = db_conn.execute("SELECT * FROM price_log;") rows = cursor.fetchall() self.assertEqual(type(rows[0][0]), datetime) self.assertEqual(rows[0][1], "bitcoin") self.assertEqual(rows[0][2], "btc") self.assertEqual(rows[0][3], 123.123) self.assertEqual(type(rows[1][0]), datetime) self.assertEqual(rows[1][1], "testing123") self.assertEqual(rows[1][2], None) self.assertEqual(rows[1][3], None)