def test_initialize_game(): simulator = Simulator() inital_game = simulator.initialize_game( Position(0, 0), 3, [{"id": 0, "position_x": 500, "position_y": 600}, {"id": 1, "position_x": 700, "position_y": 300}, {"id": 2, "position_x": 100, "position_y": 900}], 1, [{"id": 0, "position_x": 2000, "position_y": 1000}] ) assert len(inital_game.humans) == 3 assert len(inital_game.zombies) == 1 inital_game = simulator.initialize_game( Position(0, 0), 4, [], 2, [] ) assert len(inital_game.humans) == 4 assert len(inital_game.zombies) == 2
def seed(dbpath=DBPATH): ORM.dbpath = dbpath mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password('password') mike_bloom.api_key = '00000' mike_bloom.save() buy_trade = Trade(accounts_pk=mike_bloom.pk, ticker='appl', volume=10, price=100.0) buy_trade = Trade(accounts_pk=mike_bloom.pk, ticker='tsla', volume=10, price=100.0) sell_trade = Trade(accounts_pk=mike_bloom.pk, ticker='tsla', volume=-5, price=200.0) buy_trade.save() sell_trade.save() tsla_position = Position(ticker='tsla', shares=5, accounts_pk=mike_bloom.pk) tsla_position.save()
def test_position_to_node_index(): graph = Graph() assert graph.position_to_node_index(Position(0, 0), 4) == 0 assert graph.position_to_node_index(Position(1, 0), 4) == 1 assert graph.position_to_node_index(Position(3, 3), 4) == 15 assert graph.position_to_node_index(Position(2, 2), 4) == 10
def test_node_index_to_position(): graph = Graph() assert graph.node_index_to_position(0, 4).is_equal(Position(0, 0)) assert graph.node_index_to_position(1, 4).is_equal(Position(1, 0)) assert graph.node_index_to_position(10, 4).is_equal(Position(2, 2)) assert graph.node_index_to_position(12, 4).is_equal(Position(0, 3)) assert graph.node_index_to_position(15, 4).is_equal(Position(3, 3))
def test_find_position1(): map = Map(11, 7) map.load_from_file("resources/map1.txt") assert map.find_stable_position("DISH").is_equal(Position(5, 0)) assert map.find_stable_position("window").is_equal(Position(5, 6)) assert map.find_stable_position("BLUEBERRIES").is_equal(Position(0, 3)) assert map.find_stable_position("ICE_CREAM").is_equal(Position(2, 6))
def testPosition(self): position = Position() position.account_pk = 1 self.assertEqual(position.tablename, "positions") self.assertEqual(position.fields, ['ticker', 'number_shares', 'account_pk']) self.assertIsInstance(position, Position) self.assertIsNone(position.ticker) self.assertEqual(position.account_pk, 1)
def testPosition(self): position = Position() self.assertEqual(position.tablename, "positions") self.assertEqual(position.fields, ['ticker', 'number_shares', 'account_pk']) self.assertIsInstance(position, Position) position1 = Position(ticker="ibm", number_shares=2, account_pk=1) self.assertEqual(position1.ticker, "ibm") self.assertEqual(position1.number_shares, 2) self.assertEqual(position1.account_pk, 1)
def testSaveInsert(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() self.assertIsNotNone(apple.id, "save should set an id value for new input") with sqlite3.connect(DBPATH) as connection: cursor = connection.cursor() SQL = "SELECT * FROM positions WHERE shares=100;" cursor.execute(SQL) rows = cursor.fetchall() self.assertEqual(len(rows), 1, "save should create 1 new row in the database")
def testDelete(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() orange = Position(ticker="ORNG", shares=150, account_id=3) orange.save() apple.delete() with sqlite3.connect(DBPATH) as connection: cursor = connection.cursor() SQL = "SELECT * FROM positions;" cursor.execute(SQL) rows = cursor.fetchall() self.assertEqual(len(rows), 1, "delete should delete 1 row in the database")
def seed(dbpath=DBPATH): ORM.dbpath = dbpath print("DATABASE: ", dbpath) mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password("password") print(mike_bloom.dbpath) mike_bloom.save() tsla_position = Position(ticker='tsla', shares=5, account_pk=mike_bloom.pk) tsla_position.save() fake_trade = Trade(ticker='tsla', quantity=2, type=1) fake_trade.save()
def seed(dbpath=DBPATH): ORM.dbpath = dbpath mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password('password') mike_bloom.save() # trade for a purchase of 10 shares yesterday # trade for a sell of 5 shares today tsla_position = Position(ticker='tsla', shares=5, accounts_pk=mike_bloom.pk) tsla_position.save() ms_position = Position(ticker='ms', shares=10, accounts_pk=mike_bloom.pk) ms_position.save() tsla_trade = Trade( ticker='tsla', volume=5, price=95.20, time=time.time(), accounts_pk=mike_bloom.pk, ) tsla_trade.save() ms_trade = Trade( ticker='ms', volume=10, price=25.50, time=time.time(), accounts_pk=mike_bloom.pk, ) ms_trade.save()
def test_play_round_move(): simulator = Simulator() inital_game = simulator.initialize_game( Position(0, 0), 1, [{"id": 0, "position_x": 8250, "position_y": 4500}], 1, [{"id": 0, "position_x": 8250, "position_y": 8999}] ) ash_target_position = Position(8250, 8999) updated_game, round_score = simulator.play_round(inital_game.ash, inital_game.humans, inital_game.zombies, ash_target_position) assert updated_game.zombies[0].position.is_equal(Position(8250, 8599)) assert updated_game.ash.position.is_equal(Position(675, 737))
def test_simulate_move(): map = Map(4) map.load_from_file("resources/map1.txt") tile = "1111" simulated_map, item_position, my_player_position, items = map.simulate_move( Position(0, 0), tile, 0, "RIGHT", []) assert [str(tile) for tile in simulated_map.tiles[0] ] == ["1111", "0101", "0111", "0011"] assert [str(tile) for tile in simulated_map.tiles[1] ] == ["0110", "1010", "0110", "0101"] assert [str(tile) for tile in simulated_map.tiles[2] ] == ["0011", "1101", "1001", "0111"] assert [str(tile) for tile in simulated_map.tiles[3] ] == ["1001", "1110", "1101", "0101"] simulated_map, item_position, my_player_position, items = map.simulate_move( Position(0, 0), tile, 2, "LEFT", []) assert [str(tile) for tile in simulated_map.tiles[0] ] == ["0110", "0101", "0110", "0011"] assert [str(tile) for tile in simulated_map.tiles[1] ] == ["0011", "1010", "1001", "0101"] assert [str(tile) for tile in simulated_map.tiles[2] ] == ["1001", "1101", "1101", "0111"] assert [str(tile) for tile in simulated_map.tiles[3] ] == ["1101", "1110", "1111", "0101"] simulated_map, item_position, my_player_position, items = map.simulate_move( Position(0, 0), tile, 1, "UP", []) assert [str(tile) for tile in simulated_map.tiles[0] ] == ["0110", "0101", "0111", "0011"] assert [str(tile) for tile in simulated_map.tiles[1] ] == ["1010", "0110", "0101", "1111"] assert [str(tile) for tile in simulated_map.tiles[2] ] == ["1001", "1101", "1001", "0111"] assert [str(tile) for tile in simulated_map.tiles[3] ] == ["1101", "1110", "1101", "0101"] simulated_map, item_position, my_player_position, items = map.simulate_move( Position(0, 0), tile, 2, "DOWN", []) assert [str(tile) for tile in simulated_map.tiles[0] ] == ["0110", "0101", "0111", "0011"] assert [str(tile) for tile in simulated_map.tiles[1] ] == ["0011", "1010", "0110", "0101"] assert [str(tile) for tile in simulated_map.tiles[2] ] == ["1111", "1001", "1101", "1001"] assert [str(tile) for tile in simulated_map.tiles[3] ] == ["1101", "1110", "1101", "0101"]
def test_testCase7(): simulator = Simulator() initial_game = simulator.initialize_game( Position(0, 4000), 2, [ {"id": 0, "position_x": 0, "position_y": 1000}, {"id": 1, "position_x": 0, "position_y": 8000} ], 16, [ {"id": 0, "position_x": 5000, "position_y": 1000}, {"id": 1, "position_x": 5000, "position_y": 8000}, {"id": 2, "position_x": 7000, "position_y": 1000}, {"id": 3, "position_x": 7000, "position_y": 8000}, {"id": 4, "position_x": 9000, "position_y": 1000}, {"id": 5, "position_x": 9000, "position_y": 8000}, {"id": 6, "position_x": 11000, "position_y": 1000}, {"id": 7, "position_x": 11000, "position_y": 8000}, {"id": 8, "position_x": 13000, "position_y": 1000}, {"id": 9, "position_x": 13000, "position_y": 8000}, {"id": 10, "position_x": 14000, "position_y": 1000}, {"id": 11, "position_x": 14000, "position_y": 8000}, {"id": 12, "position_x": 14500, "position_y": 1000}, {"id": 13, "position_x": 14500, "position_y": 8000}, {"id": 14, "position_x": 15000, "position_y": 1000}, {"id": 15, "position_x": 15000, "position_y": 8000} ] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase1(): simulator = Simulator() initial_game = simulator.initialize_game(Position(0, 0), 1, [{"id": 0, "position_x": 8250, "position_y": 4500}], 1, [{"id": 0, "position_x": 8250, "position_y": 8999}]) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score == 10
def test_testCase9(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8000, 4500), 4, [ {"id": 0, "position_x": 4000, "position_y": 2250}, {"id": 1, "position_x": 4000, "position_y": 6750}, {"id": 2, "position_x": 12000, "position_y": 2250}, {"id": 3, "position_x": 12000, "position_y": 6750} ], 12, [ {"id": 0, "position_x": 4000, "position_y": 3375}, {"id": 1, "position_x": 12000, "position_y": 3375}, {"id": 2, "position_x": 4000, "position_y": 4500}, {"id": 3, "position_x": 12000, "position_y": 4500}, {"id": 4, "position_x": 4000, "position_y": 5625}, {"id": 5, "position_x": 12000, "position_y": 5625}, {"id": 6, "position_x": 6000, "position_y": 2250}, {"id": 7, "position_x": 8000, "position_y": 2250}, {"id": 8, "position_x": 10000, "position_y": 2250}, {"id": 9, "position_x": 6000, "position_y": 6750}, {"id": 10, "position_x": 8000, "position_y": 6750}, {"id": 11, "position_x": 10000, "position_y": 6750} ] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def get_num_shares(api_key, ticker): user = Account.api_authenticate(api_key) if user: total_shares = Position.one_from_account(user.pk, ticker) return jsonify({"total": total_shares}) return jsonify({'error': 'invalid key'})
def test_testCase20(): simulator = Simulator() initial_game = simulator.initialize_game( Position(0, 4500), 2, [{"id": 0, "position_x": 7000, "position_y": 3500}, {"id": 1, "position_x": 0, "position_y": 500}, {"id": 2, "position_x": 7000, "position_y": 5500}, {"id": 3, "position_x": 3500, "position_y": 1000}, {"id": 4, "position_x": 9250, "position_y": 8000}, {"id": 5, "position_x": 13000, "position_y": 4500}] , 2, [{"id": 0, "position_x": 3600, "position_y": 3500}, {"id": 1, "position_x": 3700, "position_y": 4500}, {"id": 2, "position_x": 3400, "position_y": 6500}, {"id": 3, "position_x": 9000, "position_y": 3500}, {"id": 4, "position_x": 8990, "position_y": 4500}, {"id": 5, "position_x": 9000, "position_y": 5500}, {"id": 6, "position_x": 11000, "position_y": 4000}, {"id": 7, "position_x": 9100, "position_y": 10}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def get_equ(token): account = Account.api_authenticate(token) if account: res = Position.from_position_equity(account.pk) total = res.total_equ(account.pk) return jsonify({'equity': total}) return jsonify({'error': 'invalid key'})
def test_testCase21(): simulator = Simulator() initial_game = simulator.initialize_game( Position(7992, 8304), 15, [{"id": 0, "position_x": 757, "position_y": 3545}, {"id": 1, "position_x": 510, "position_y": 8170}, {"id": 2, "position_x": 1119, "position_y": 733}, {"id": 3, "position_x": 1416, "position_y": 7409}, {"id": 4, "position_x": 1110, "position_y": 8488}, {"id": 5, "position_x": 2118, "position_y": 1983}, {"id": 6, "position_x": 3167, "position_y": 480}, {"id": 7, "position_x": 6576, "position_y": 664}, {"id": 8, "position_x": 8704, "position_y": 1276}, {"id": 9, "position_x": 13340, "position_y": 5663}, {"id": 10, "position_x": 13808, "position_y": 4731}, {"id": 11, "position_x": 15355, "position_y": 3528}, {"id": 12, "position_x": 15495, "position_y": 5035}, {"id": 13, "position_x": 15182, "position_y": 6184}, {"id": 14, "position_x": 15564, "position_y": 7640}] , 7, [{"id": 0, "position_x": 3996, "position_y": 4152}, {"id": 1, "position_x": 3996, "position_y": 4844}, {"id": 2, "position_x": 3996, "position_y": 7612}, {"id": 3, "position_x": 5328, "position_y": 1384}, {"id": 4, "position_x": 7992, "position_y": 3460}, {"id": 5, "position_x": 11322, "position_y": 5536}, {"id": 6, "position_x": 11322, "position_y": 8304}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase16(): simulator = Simulator() initial_game = simulator.initialize_game( Position(3989, 3259), 3, [{"id": 0, "position_x": 302, "position_y": 6109}, {"id": 1, "position_x": 3671, "position_y": 981}, {"id": 2, "position_x": 6863, "position_y": 809}] , 11, [{"id": 0, "position_x": 208, "position_y": 156}, {"id": 1, "position_x": 10129, "position_y": 711}, {"id": 2, "position_x": 13229, "position_y": 413}, {"id": 3, "position_x": 203, "position_y": 3627}, {"id": 4, "position_x": 7310, "position_y": 3912}, {"id": 5, "position_x": 9814, "position_y": 3223}, {"id": 6, "position_x": 13556, "position_y": 3668}, {"id": 7, "position_x": 3923, "position_y": 6251}, {"id": 8, "position_x": 6720, "position_y": 6574}, {"id": 9, "position_x": 10387, "position_y": 6136}, {"id": 10, "position_x": 13093, "position_y": 6253}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase11(): simulator = Simulator() initial_game = simulator.initialize_game( Position(9000, 684), 3, [{"id": 0, "position_x": 15999, "position_y": 4500}, {"id": 1, "position_x": 8000, "position_y": 7999}, {"id": 2, "position_x": 0, "position_y": 4500}] , 9, [{"id": 0, "position_x": 0, "position_y": 3033}, {"id": 1, "position_x": 1500, "position_y": 6251}, {"id": 2, "position_x": 3000, "position_y": 2502}, {"id": 3, "position_x": 4500, "position_y": 6556}, {"id": 4, "position_x": 6000, "position_y": 3905}, {"id": 5, "position_x": 7500, "position_y": 5472}, {"id": 6, "position_x": 10500, "position_y": 2192}, {"id": 7, "position_x": 12000, "position_y": 6568}, {"id": 8, "position_x": 13500, "position_y": 7448}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_find_position3(): map = Map(11, 7) map.load_from_file("resources/map3.txt") assert map.find_stable_position("DISH").is_equal(Position(5, 0)) assert map.find_stable_position("window").is_equal(Position(5, 6)) assert map.find_stable_position("BLUEBERRIES").is_equal(Position(7, 6)) assert map.find_stable_position("ICE_CREAM").is_equal(Position(5, 3)) assert map.find_stable_position("STRAWBERRIES").is_equal(Position(10, 6)) assert map.find_stable_position("chopping_board").is_equal(Position(10, 0)) assert map.find_stable_position("oven").is_equal(Position(1, 0)) assert map.find_stable_position("DOUGH").is_equal(Position(4, 2))
def test_play_round_kill_zombies(): simulator = Simulator() inital_game = simulator.initialize_game( Position(0, 0), 3, [{"id": 0, "position_x": 8250, "position_y": 4500}, {"id": 1, "position_x": 8000, "position_y": 7000}, {"id": 2, "position_x": 2500, "position_y": 1000}], 2, [{"id": 0, "position_x": 100, "position_y": 100}, {"id": 0, "position_x": 200, "position_y": 200}] ) ash_target_position = Position(8250, 8999) updated_game, round_score = simulator.play_round(inital_game.ash, inital_game.humans, inital_game.zombies, ash_target_position) assert inital_game.zombies_count == 2 assert updated_game.zombies_count == 0 assert len(updated_game.zombies) == 0 assert round_score == 270
def test_testCase10(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8000, 0), 3, [ {"id": 0, "position_x": 0, "position_y": 4500}, {"id": 1, "position_x": 15999, "position_y": 4500}, {"id": 2, "position_x": 8000, "position_y": 7999} ] , 23, [ {"id": 0, "position_x": 2000, "position_y": 1200}, {"id": 1, "position_x": 3000, "position_y": 1800}, {"id": 2, "position_x": 4000, "position_y": 2400}, {"id": 3, "position_x": 5000, "position_y": 3000}, {"id": 4, "position_x": 6000, "position_y": 3600}, {"id": 5, "position_x": 9000, "position_y": 5400}, {"id": 6, "position_x": 10000, "position_y": 6000}, {"id": 7, "position_x": 11000, "position_y": 6600}, {"id": 8, "position_x": 12000, "position_y": 7200}, {"id": 9, "position_x": 13000, "position_y": 7800}, {"id": 10, "position_x": 14000, "position_y": 8400}, {"id": 11, "position_x": 14000, "position_y": 600}, {"id": 12, "position_x": 13000, "position_y": 1200}, {"id": 13, "position_x": 12000, "position_y": 1800}, {"id": 14, "position_x": 11000, "position_y": 2400}, {"id": 15, "position_x": 10000, "position_y": 3000}, {"id": 16, "position_x": 9000, "position_y": 3600}, {"id": 17, "position_x": 6000, "position_y": 5400}, {"id": 18, "position_x": 5000, "position_y": 6000}, {"id": 19, "position_x": 4000, "position_y": 6600}, {"id": 20, "position_x": 3000, "position_y": 7200}, {"id": 21, "position_x": 2000, "position_y": 7800}, {"id": 22, "position_x": 1000, "position_y": 8400} ] ) # pr = cProfile.Profile() # pr.enable() final_score, move = initial_game.get_best_move() # pr.disable() # s = io.StringIO() # sortby = 'tottime' # ps = pstats.Stats(pr, stream=s).sort_stats(sortby) # ps.print_stats() # print(s.getvalue()) pytest.total_score += final_score assert final_score > 0
def testBuy(self): alex = Account(username="******", password_hash="password", balance=20000000, first_name="Alex", last_name="C", email="*****@*****.**") alex.save() # Tests buy for an already existing position stok_position = Position(ticker="STOK", shares=100, account_id=alex.id) stok_position.save() alex.buy(ticker="STOK", volume=100) check_stok = Position.from_account_id_and_ticker(account_id=alex.id, ticker="STOK") self.assertEqual(check_stok.shares, 200, msg="buy() should increase position shares") # Tests buy trade for a position that doesn't exist alex.buy(ticker="P2P", volume=101) check_p2p = Position.from_account_id_and_ticker(account_id=alex.id, ticker="P2P") self.assertEqual(check_p2p.shares, 101, "buy() should increase position shares") # Test bad ticker symbol with self.assertRaises(NoSuchTickerError, msg="buy() should raise NoSUchTickerError"): alex.buy(ticker="xyz1234", volume=100)
def test_testCase4(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8000, 2000), 1, [{"id": 0, "position_x": 8000, "position_y": 4500}], 2, [{"id": 0, "position_x": 2000, "position_y": 6500}, {"id": 1, "position_x": 14000, "position_y": 6500}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase2(): simulator = Simulator() initial_game = simulator.initialize_game( Position(5000, 0), 1, [{"id": 0, "position_x": 950, "position_y": 6000}, {"id": 1, "position_x": 8000, "position_y": 6100}], 1, [{"id": 0, "position_x": 3100, "position_y": 7000}, {"id": 1, "position_x": 11500, "position_y": 7100}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase3(): simulator = Simulator() initial_game = simulator.initialize_game( Position(10999, 0), 1, [{"id": 0, "position_x": 8000, "position_y": 5500}, {"id": 1, "position_x": 4000, "position_y": 5500}], 1, [{"id": 0, "position_x": 1250, "position_y": 5500}, {"id": 1, "position_x": 15999, "position_y": 5500}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_find_position2(): map = Map(11, 7) map.load_from_file("resources/map2.txt") assert map.find_stable_position("DISH").is_equal(Position(5, 0)) assert map.find_stable_position("window").is_equal(Position(5, 6)) assert map.find_stable_position("BLUEBERRIES").is_equal(Position(6, 0)) assert map.find_stable_position("ICE_CREAM").is_equal(Position(0, 1)) assert map.find_stable_position("STRAWBERRIES").is_equal(Position(0, 4)) assert map.find_stable_position("chopping_board").is_equal(Position(6, 4))