def test_get_coin_ids(self): backend.add_currency("bitcoin", db_conn) backend.add_currency("testing123", db_conn) coin_ids = backend.get_coin_ids(db_conn) self.assertListEqual(coin_ids, ["bitcoin", "testing123"])
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 test_delete_currency(self): # Add and delete backend.add_currency("bitcoin", db_conn) backend.delete_currency("bitcoin", db_conn) cursor = db_conn.execute("SELECT * FROM coin_data;") rows = cursor.fetchall() self.assertEqual(len(rows), 0)
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_change_in_message(self): # Add and change backend.add_currency("bitcoin", db_conn) backend.change_in_message("bitcoin", True, db_conn) cursor = db_conn.execute("SELECT * FROM coin_data;") rows = cursor.fetchall() self.assertEqual(rows[0][0], "bitcoin") self.assertEqual(rows[0][4], True) # Change back backend.change_in_message("bitcoin", False, db_conn) cursor = db_conn.execute("SELECT * FROM coin_data;") rows = cursor.fetchall() self.assertEqual(rows[0][0], "bitcoin") self.assertEqual(rows[0][4], False)
def test_add_currency(self): # Add first backend.add_currency("bitcoin", db_conn) cursor = db_conn.execute("SELECT * FROM coin_data;") rows = cursor.fetchall() self.assertEqual(rows[0][0], "bitcoin") self.assertEqual(rows[0][1], None) self.assertEqual(rows[0][2], None) self.assertEqual(rows[0][3], None) self.assertEqual(rows[0][4], False) self.assertEqual(rows[0][5], None) # Add second backend.add_currency("tron", db_conn) cursor = db_conn.execute("SELECT * FROM coin_data;") rows = cursor.fetchall() self.assertEqual(len(rows), 2) self.assertEqual(rows[1][0], "tron") # Add existing backend.add_currency("tron", db_conn) self.assertEqual(len(rows), 2)
def price_viewer(): db_conn = sqlite3.connect(db_path, detect_types=sqlite3.PARSE_DECLTYPES) if request.method == "POST" and "case" in request.values: # Case variable used to determine what to do if request.values["case"] == "UPDATE_PRICES": backend.request_price_update(dashboard_conn) if request.values["case"] == "ADD_CURRENCY": new_currency_id = request.values["new_currency_id"] new_currency_id = new_currency_id.lower() backend.add_currency(new_currency_id, db_conn) backend.request_price_update(dashboard_conn) if request.values["case"] == "CHANGE_IN_MESSAGE": currency_id = request.values["currency_id"].replace("'", "") checkbox_value = request.values["in_message_checked"] == "true" backend.change_in_message(currency_id, checkbox_value, db_conn) backend.request_price_update(dashboard_conn) if request.values["case"] == "DELETE_CURRENCY": currency_id = request.values["delete_currency_id"] was_in_message = request.values["in_message_checked"] == "checked" backend.delete_currency(currency_id, db_conn) if was_in_message: backend.request_price_update(dashboard_conn) db_conn.close() return jsonify(success=True) # Normal update coins = backend.read_coins_from_db(db_conn) next_update_str = backend.get_next_update_string(coins, update_interval, dashboard_conn) db_conn.close() return render_template("price_viewer.html", coins=coins, next_update_str=next_update_str)
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)