def stubbed(*coins): tickers = [] for coin in coins: last_ticker = (Ticker .select() .where(Ticker.coin == coin) .order_by(Ticker.captured_at.desc()) .limit(1)) if last_ticker.count() == 0: price = random.uniform(500, 12000) print(f'Creating first ticker for {coin.symbol}: {price} 0%') ticker = Ticker.create( coin=coin, price=price, price_change_day_pct=0, ) tickers.append(ticker) else: last_ticker = last_ticker.get() new_price = last_ticker.price * Decimal(random.uniform(0.98, 1.02)) price_change_day_pct = (new_price - last_ticker.price) / last_ticker.price print(f'{coin.symbol}: {new_price} {price_change_day_pct}%') ticker = Ticker.create(coin=coin, price=new_price, price_change_day_pct=price_change_day_pct) check_price_alerts(ticker) check_global_timed_game() tickers.append(ticker) return tickers
def test_buy_coin_without_cash(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) Ticker.create(coin=1, price=10, price_change_day_pct=1.1) res = self.client.post( '/game/1/coin', data=json.dumps({ 'coinId': '1', 'coinAmount': '1', }), content_type='application/json', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.BAD_REQUEST), res._status_code)
def test_liquefy_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') game_profile = GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) GameProfileCoin.create(game_profile=game_profile, coin=1, coin_amount=2) Ticker.create( coin=1, price=10, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.delete( '/game/1/coins', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
def test_get_coins_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=10000) GameCoin.create( game=1, coin=1, ) for coin in Coin.select(): Ticker.create( coin=coin, price=30.0, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.get( '/game/1/coins?timeSpan=1&sortBy=0&numPerPage=10&pageNum=1', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
def test_sell_coin_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') game_profile = GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) GameProfileCoin.create(game_profile=game_profile, coin=1, coin_amount=2) Ticker.create( coin=1, price=10, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.post( '/game/1/coin', data=json.dumps({ 'coinId': '1', 'coinAmount': '-1', }), content_type='application/json', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
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