def test_parsing_with_many_coins(self): ping('BTC', 'ETH') btc = Coin.get(Coin.symbol == 'BTC') eth = Coin.get(Coin.symbol == 'ETH') btc_ticker = Ticker.get(Ticker.coin == btc) eth_ticker = Ticker.get(Ticker.coin == eth) self.assertEqual(btc_ticker.price, Decimal('56.12345678')) self.assertEqual(btc_ticker.price_change_day_pct, Decimal('-1.23456789')) self.assertEqual(eth_ticker.price, Decimal('42.98765432')) self.assertEqual(eth_ticker.price_change_day_pct, Decimal('-8.98765432'))
def test_parsing_with_many_ping_calls(self): ping('BTC') btc = Coin.get(Coin.symbol == 'BTC') btc_ticker = Ticker.get(Ticker.coin == btc) self.assertEqual(btc_ticker.price, Decimal('56.12345678')) self.assertEqual(btc_ticker.price_change_day_pct, Decimal('-2.33334567')) Ticker.delete().execute() ping('BTC') btc = Coin.get(Coin.symbol == 'BTC') btc_ticker = Ticker.get(Ticker.coin == btc) self.assertEqual(btc_ticker.price, Decimal('56.12345678')) self.assertEqual(btc_ticker.price_change_day_pct, Decimal('-2.33334567'))
def test_parsing_with_one_coin(self): ping('BTC') btc = Coin.get(Coin.symbol == 'BTC') btc_ticker = Ticker.get(Ticker.coin == btc) self.assertEqual(btc_ticker.price, Decimal('56.12345678')) self.assertEqual(btc_ticker.price_change_day_pct, Decimal('-2.33334567'))
def test_parsing_with_four_character_symbol(self): coin = Coin.create(name='foocoin', symbol='DASH') ping('DASH') dash = Coin.get(Coin.symbol == 'DASH') ticker = Ticker.get(Ticker.coin == dash) self.assertEqual(ticker.price, Decimal('56.12345678')) self.assertEqual(ticker.price_change_day_pct, Decimal('-1.23456789'))
def ping(*coins): res = requests.get(get_api_url(*coins)) tickers = [] for coin_res in res.json(): symbol = coin_res['symbol'] coin = Coin.get(Coin.symbol == symbol) price = Decimal(coin_res['price']) price_change_day_pct = Decimal(coin_res['1d']['price_change_pct']) ticker = Ticker.create(coin=coin, price=price, price_change_day_pct=price_change_day_pct) tickers.append(ticker) check_price_alerts(ticker) check_global_timed_game() return tickers
def up(db): with db.atomic(): migrator = PostgresqlMigrator(db) db.bind(MODELS, bind_refs=False, bind_backrefs=False) db.create_tables(MODELS) if Coin.get_or_none(Coin.id == 1) is None: Coin.create(name='Bitcoin', symbol='BTC') Coin.create(name='Ethereum', symbol='ETH') Coin.create(name='Litecoin', symbol='LTC') Coin.create(name='Coin 3', symbol='CO3') Coin.create(name='Coin 4', symbol='CO4') Coin.create(name='Coin 5', symbol='CO5') global_indef = Game.create(name='Global Indefinite', starting_cash=10000.00, shareable_link='INDEF', shareable_code='INDEF', ends_at=None) # insert achievements into database Achievement.create( name="Win", description="Finish in first place in a private game") Achievement.create( name="Double net worth", description="Achieved by doubling your net worth in a game") Achievement.create(name="Identity Crisis", description="Change your username") # insert goals into database Goal.create(name="Entrepreneur", description="Create a private game") all_coins = Coin.select() for coin in all_coins: GameCoin.create(game=global_indef, coin=coin) global_timed = Game.create(name='Global Timed', starting_cash=10000.00, shareable_link='TIMED', shareable_code='TIMED', ends_at=datetime.utcnow() + timedelta(minutes=1)) # CHANGEME for devel purposes, making it 1 min for now GameCoin.create(game=global_timed, coin=Coin.get()) # from auth.services import register hashed = bcrypt.hashpw("admin".encode(), bcrypt.gensalt()).decode() admin = Profile.create(username="******", hashed_password=hashed, is_admin=True) # Required so that admin can still view graphs in the landing page GameProfile.create(profile=admin, game=global_indef, cash=0.0)
def check_global_timed_game(): game = Game.get(Game.shareable_link == 'TIMED') if game.ends_at < datetime.utcnow(): # end global timed game, and start another profiles = [] for game_profile in GameProfile.select().where(GameProfile.game == game): profiles.append(game_profile.profile) send_notification(game_profile.profile, 'The global timed game has expired') game_profile.delete_instance(recursive=True) game.delete_instance(recursive=True) global_timed = Game.create(name='Global Timed', starting_cash=10000.00, shareable_link='TIMED', shareable_code='TIMED', ends_at=datetime.utcnow() + timedelta(minutes=1)) # CHANGEME for devel purposes, making it 1 min for now GameCoin.create(game=global_timed, coin=Coin.get()) for profile in profiles: GameProfile.create( game=global_timed, profile=profile, cash=global_timed.starting_cash )