def test_process_config_data_no_changes(self): user = '******' players.create_player(user) symbol = "ASDF" config_data = { 'symbol': symbol, 'full_name': "A S D F Corp", } userconfig.process_config_data(user, config_data) new_symbol = 'XXXX' new_config_data = { 'symbol': new_symbol, 'full_name': "X X X X Corp", } userconfig.process_config_data(user, new_config_data) company = companies.get_company_by_symbol(symbol) self.assertEqual(company.get('symbol', None), symbol) self.assertEqual(company.get('full_name', None), config_data['full_name']) self.assertEqual(company.get('creator', None), user) player = players.get_player(user) self.assertEqual(player.get('name', None), user) self.assertEqual(player.get('cash', 0.0), 1000.0) player = players.get_player(user) new_company = companies.get_company_by_symbol(new_symbol) self.assertEqual(new_company, None)
def test_process_config_data_already_exists(self): user = '******' symbol = "ASDF" config_data = { 'symbol': symbol, 'full_name': "A S D F Corp", } userconfig.process_config_data(user, config_data) userconfig.process_config_data(user, config_data) company = companies.get_company_by_symbol(symbol) company = companies.get_company_by_symbol(symbol) self.assertEqual(company.get('symbol', None), symbol) self.assertEqual(company.get('full_name', None), config_data['full_name']) self.assertEqual(company.get('creator', None), user) player = players.get_player(user) self.assertEqual(player.get('name', None), user) self.assertEqual(player.get('cash', 0.0), 1000.0) player = players.get_player(user)
def test_process_all_config_files(self): test_dir = os.path.dirname( os.path.abspath(__file__) ) test_data_dir = os.path.join( test_dir, 'testdata' ) userconfig.process_all_config_files(test_data_dir) test_companies = [ {'symbol': "NEWU", 'full_name': "New Corp", 'creator': "newuser"}, {'symbol': "ACOR", 'full_name': "A Corp", 'creator': "usera"}, {'symbol': "BMO", 'full_name': "BMO Corp", 'creator': "userb"}, {'symbol': "CLLC", 'full_name': "C LLC", 'creator': "userc"}, ] database = db.get_database() company_count_query = """ SELECT count(*) as count from companies """ results = database.fetch_as_dict(company_count_query) self.assertEqual(results['count'], len(test_companies)) player_count_query = """ SELECT count(*) as count from players """ results = database.fetch_as_dict(player_count_query) self.assertEqual(results['count'], len(test_companies)) for test_company in test_companies: created_company = companies.get_company_by_symbol( test_company['symbol'] ) self.assertEqual(test_company, created_company) player = players.get_player(test_company['creator']) self.assertIsNotNone(player) shares = stocks.get_all_company_shares(test_company['symbol']) expected_shares = [ { 'company': test_company['symbol'], 'player': test_company['creator'], 'quantity': stocks.COMPANY_STARTING_SHARES, } ] self.assertEqual(shares, expected_shares) badcorp = companies.get_company_by_symbol('BADU') self.assertIsNone(badcorp)
def new_company_shares(player_name, num_shares=COMPANY_STARTING_SHARES): # Add the shares for a new company to the market player = players.get_player(player_name) if player is None: return False company = companies.get_company_by_creator(player_name) if company is None: return False existing_shares = get_all_company_shares(company['symbol']) if existing_shares: return False database = db.get_database() query = """ INSERT INTO shares ( player, company, quantity ) VALUES (?, ?, ?) """ database.query(query, params=(player['name'], company['symbol'], COMPANY_STARTING_SHARES), commit=True) return True
def process_config_data(user, config_data): if not validate_config_data(user, config_data): return None symbol = config_data.get('symbol') full_name = config_data.get('full_name') exact_company = companies.get_exact_company(user, symbol, full_name) if exact_company: # no changes to config return True unique_properties = [ ('User', user, companies.get_company_by_creator), ('Symbol', symbol, companies.get_company_by_symbol), ('Name', full_name, companies.get_company_by_full_name), ] error_messages = [] for varname, value, method in unique_properties: found_row = method(value) if found_row is not None: error_messages.append( f"{varname} {value} in use. Must choose a different one") if error_messages: error_message = '\n'.join(error_messages) mail.send_template_email(user, 'user_config_json_error', error_message=error_message) return False player = players.get_player(user) if not player: players.create_player(user) companies.create_company(symbol, full_name, user) stocks.new_company_shares(user)
def test_create_get_player(self): player_name = "joeyjoejoe" players.create_player(player_name) player = players.get_player(player_name) self.assertEqual(player['name'], player_name)