def test_send_war_nag_participants_completed_returns_false_because_url_invalid( tmpdir): config_file = tmpdir.mkdir( 'test_send_war_nag_threshold_not_met_returns_true').join('testfile') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) fake_current_war = { 'state': 'collectionDay', 'collection_end_time': datetime.utcnow().strftime("%Y%m%dT%H%M%S.xxxx"), 'participants': [{ 'name': 'AAA', 'tag': '#AAA', 'battles_played': 1, 'number_of_battles': 3 }, { 'name': 'BBB', 'tag': '#BBB', 'battles_played': 1, 'number_of_battles': 3 }] } fake_members = [{'tag': '#AAA'}] assert discord.send_war_nag(config, fake_current_war, fake_members) == False
def test_get_role_label(): config = load_config_file(False) assert crtools.get_role_label( config, 'member', 0, 'good', False, True, False) == config['strings']['roleBlacklisted'] assert crtools.get_role_label( config, 'leader', 100, 'bad', True, True, False) == config['strings']['roleBlacklisted'] assert crtools.get_role_label(config, 'leader', 100, 'bad', True, False, False) == config['strings']['roleVacation'] assert crtools.get_role_label( config, 'leader', 100, 'bad', False, False, False) == config['strings']['roleInactive'].format(days=100) assert crtools.get_role_label(config, 'leader', 0, 'good', False, False, False) == config['strings']['roleLeader'] assert crtools.get_role_label(config, 'coLeader', 0, 'good', False, False, False) == config['strings']['roleCoLeader'] assert crtools.get_role_label(config, 'elder', 0, 'good', False, False, False) == config['strings']['roleElder'] assert crtools.get_role_label(config, 'member', 0, 'good', False, False, False) == config['strings']['roleMember'] assert crtools.get_role_label(config, 'leader', 0, 'good', False, False, True) == config['strings']['roleNoPromote']
def test_process_current_war_nowar(): config = load_config_file(False) war = ProcessedCurrentWar( config=config, current_war=pyroyale.WarCurrent(state='notInWar')) assert war.state_label == 'The clan is not currently engaged in a war.'
def test_get_suggestions_nosuggestions(tmpdir): config_file = tmpdir.mkdir('test_get_suggestions').join('testfile') config_file.write( __config_file_score__ + '\nthreshold_demote=-999999\nthreshold_promote=9999999\nmin_clan_size={}' .format(crtools.MAX_CLAN_SIZE)) config = load_config_file(config_file.realpath()) members = [] for i in range(0, crtools.MAX_CLAN_SIZE): members.append({ "role": "leader", "trophies": 9999, "donations": 999, "score": 999, "vacation": False, "safe": True, "blacklist": False, "no_promote": False }) suggestions = crtools.get_suggestions(config, members, __fake_clan__) assert len(suggestions) == 1 assert suggestions[0] == config['strings']['suggestionNone']
def test_get_scoring_rules(tmpdir): config_file = tmpdir.mkdir('test_get_scoring_rules').join('testfile') config_file.write(__config_file_score__) config = load_config_file(config_file.realpath()) rules = crtools.get_scoring_rules(config) assert rules[0]['yes'] == 0 assert rules[0]['no'] == -1 assert rules[0]['yes_status'] == 'normal' assert rules[0]['no_status'] == 'bad' assert rules[1]['yes'] == 0 assert rules[1]['no'] == -5 assert rules[1]['yes_status'] == 'normal' assert rules[1]['no_status'] == 'bad' assert rules[2]['yes'] == 2 assert rules[2]['no'] == 0 assert rules[2]['yes_status'] == 'good' assert rules[2]['no_status'] == 'normal' assert rules[3]['yes'] == 15 assert rules[3]['no'] == -30 assert rules[3]['yes_status'] == 'good' assert rules[3]['no_status'] == 'bad' assert rules[4]['yes'] == 5 assert rules[4]['no'] == 0 assert rules[4]['yes_status'] == 'good' assert rules[4]['no_status'] == 'normal'
def test_war_score(tmpdir): # FIXME: should replace once we test crtools.process_members() war_complete = { "battles_played": 1, "wins": 1, "collection_day_battles_played": 3, "collectionBattleWins": 2, "collectionBattleLosses": 0, "status": "na" } war_incomplete = { "battles_played": 0, "wins": 0, "collection_day_battles_played": 3, "collectionBattleWins": 2, "collectionBattleLosses": 0, "status": "na" } war_na = {"status": "na"} war_new = {"status": "not-in-clan"} config_file = tmpdir.mkdir('test_war_score').join('testfile') config_file.write(__config_file_score__) config = load_config_file(config_file.realpath()) assert crtools.war_score(config, war_complete) == 24 assert crtools.war_score(config, war_incomplete) == -26 assert crtools.war_score(config, war_na) == -1 assert crtools.war_score(config, war_new) == 0
def test_get_role_label(): config = load_config_file(False) factory = MemberFactory(config, None, None, None) vacation_end = 999999999999999999 assert factory.get_role_label( 'member', 0, 'good', MemberVacation(tag='#AAA'), True, False) == config['strings']['roleBlacklisted'] assert factory.get_role_label( 'leader', 100, 'bad', MemberVacation(tag='#AAA'), True, False) == config['strings']['roleBlacklisted'] assert factory.get_role_label('leader', 100, 'bad', MemberVacation(tag='#AAA'), False, False) == config['strings']['roleVacation'] assert factory.get_role_label( 'leader', 100, 'bad', None, False, False) == config['strings']['roleInactive'].format(days=100) assert factory.get_role_label('leader', 0, 'good', None, False, False) == config['strings']['roleLeader'] assert factory.get_role_label('coLeader', 0, 'good', None, False, False) == config['strings']['roleCoLeader'] assert factory.get_role_label('elder', 0, 'good', None, False, False) == config['strings']['roleElder'] assert factory.get_role_label('member', 0, 'good', None, False, False) == config['strings']['roleMember'] assert factory.get_role_label('leader', 0, 'good', None, False, True) == config['strings']['roleNoPromote']
def test_send_war_nag_participants_completed_returns_false_because_url_invalid( tmpdir): config_file = tmpdir.mkdir( 'test_send_war_nag_threshold_not_met_returns_true').join('testfile') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) fake_current_war = pyroyale.WarCurrent( state='collectionDay', collection_end_time=datetime.utcnow().strftime("%Y%m%dT%H%M%S.xxxx"), participants=[ pyroyale.WarParticipant(name='AAA', tag='#AAA', battles_played=1, number_of_battles=3), pyroyale.WarParticipant(name='BBB', tag='#BBB', battles_played=1, number_of_battles=3) ]) fake_members = [pyroyale.WarParticipant(tag='#AAA')] assert discord.send_war_nag(config, fake_current_war, fake_members) == False
def test_send_war_nag_not_in_war_returns_false(tmpdir): config_file = tmpdir.mkdir( 'test_send_war_nag_not_in_war_returns_false').join('testfile') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) assert discord.send_war_nag(config, {'state': 'notInWar'}, None) == True
def test_process_current_war_nowar(): config = load_config_file(False) processed_current_war = crtools.process_current_war( config, {"state": "notInWar"}) assert processed_current_war[ 'stateLabel'] == 'The clan is not currently engaged in a war.'
def test_get_member_data_from_sheets_no_google_doc_config_does_nothing(): config = load_config_file() config = gdoc.get_member_data_from_sheets(config) assert config['members']['blacklist'] == {} assert config['members']['no_promote'] == {} assert config['members']['vacation'] == {}
def test_process_current_war_collection(): config = load_config_file(False) processed_current_war = crtools.process_current_war( config, __fake_currentwar_collectionday__) assert 'collectionEndTimeLabel' in processed_current_war assert 'endLabel' in processed_current_war
def test_trigger_webhooks_no_url_returns_false(tmpdir): config_file = tmpdir.mkdir( 'test_trigger_webhooks_no_url_returns_false').join('testfile') config_file.write('') config = load_config_file(config_file.realpath()) result = discord.trigger_webhooks(config, None, None) assert result == False
def test_config_unknown_key(tmpdir): """ Sections and properties in INI files should never be added to config object if they aren't in the template """ config_file = tmpdir.mkdir('test_config_unknown_key').join('config.ini') config_file.write(__config_file_unknown_key) config = load_config_file(config_file.realpath()) assert ('garbage' in config) == False
def test_config_debug(tmpdir): """ Sections and properties in INI files should never be added to config object if they aren't in the template """ config_file = tmpdir.mkdir('test_config_unknown_key').join('config.ini') config_file.write(__config_debug) config = load_config_file(config_file.realpath()) assert config['crtools']['debug'] == True
def test_calc_donation_status(tmpdir): config_file = tmpdir.mkdir('test_calc_donation_status').join('config.ini') config_file.write(__config_file_score_donations__) config = load_config_file(config_file.realpath()) assert crtools.calc_donation_status(config, 1000, 100, 6) == 'good' assert crtools.calc_donation_status(config, 0, 0, 6) == 'bad' assert crtools.calc_donation_status(config, 0, 5, 6) == 'ok' assert crtools.calc_donation_status(config, 0, 0, 0) == 'normal'
def test_trigger_webhooks_no_nag_returns_false(tmpdir): config_file = tmpdir.mkdir( 'test_trigger_webhooks_no_nag_returns_false').join('testfile') config_file.write(__config_file_no_nag__) config = load_config_file(config_file.realpath()) assert discord.trigger_webhooks(config, {'state': 'collectionDay'}, None) == True assert discord.trigger_webhooks(config, {'state': 'warDay'}, None) == True
def test_calc_activity_status(): config = load_config_file(False) assert crtools.calc_activity_status(config, 0) == 'good' assert crtools.calc_activity_status(config, -1) == 'good' assert crtools.calc_activity_status(config, 1) == 'na' assert crtools.calc_activity_status(config, 3) == 'normal' assert crtools.calc_activity_status(config, 7) == 'ok' assert crtools.calc_activity_status(config, 400) == 'bad'
def test_process_current_war_warday(): config = load_config_file(False) processed_current_war = crtools.process_current_war( config, __fake_currentwar_warday__) assert processed_current_war['stateLabel'] == 'War Day' assert processed_current_war['collectionEndTimeLabel'] == 'Complete' assert 'endLabel' in processed_current_war
def test_calc_member_status(tmpdir): config_file = tmpdir.mkdir('test_calc_member_status').join('config.ini') config_file.write(__config_file_score_thresholds__) config = load_config_file(config_file.realpath()) assert crtools.calc_member_status(config, -1, False) == 'bad' assert crtools.calc_member_status(config, 5, False) == 'ok' assert crtools.calc_member_status(config, 10, False) == 'normal' assert crtools.calc_member_status(config, 100, False) == 'good' assert crtools.calc_member_status(config, 100, True) == 'normal'
def test_bad_locale_defaults_to_default(tmpdir, capsys): config_file = tmpdir.mkdir('test_bad_locale_defaults_to_default').join( 'config.ini') bad_locale = 'sdlkjfasldfjalsdfjalsdf' config_file.write('[crtools]\nlocale=' + bad_locale) config = load_config_file(config_file.realpath(), True) out, err = capsys.readouterr() expected = LOCALE_NOT_FOUND_ERROR_TEMPLATE.format(bad_locale) assert expected in out
def test_calc_activity_status(): config = load_config_file(False) factory = MemberFactory(config, None, None, None) assert factory.calc_activity_status(0) == 'good' assert factory.calc_activity_status(-1) == 'good' assert factory.calc_activity_status(1) == 'na' assert factory.calc_activity_status(3) == 'normal' assert factory.calc_activity_status(7) == 'ok' assert factory.calc_activity_status(400) == 'bad'
def test_process_recent_wars(tmpdir): config_file = tmpdir.mkdir('test_process_recent_wars').join('config.ini') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) processed_warlog = crtools.process_recent_wars(config, __fake_warlog__) assert processed_warlog[0]['clan']['tag'] == CLAN_TAG assert processed_warlog[0]['rank'] == 1 assert processed_warlog[0]['date'] == '2/3' assert processed_warlog[0]['trophy_change'] == 999
def test_send_war_nag_war_no_members_returns_true(tmpdir): config_file = tmpdir.mkdir( 'test_send_war_nag_war_no_members_returns_true').join('testfile') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) fake_current_war = pyroyale.WarCurrent( state='warDay', war_end_time=datetime.utcnow().strftime("%Y%m%dT%H%M%S.xxxx"), participants=[]) assert discord.send_war_nag(config, fake_current_war, []) == True
def test_process_recent_wars(tmpdir): config_file = tmpdir.mkdir('test_process_recent_wars').join('config.ini') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) processed_warlog = crtools.process_recent_wars(config, __fake_warlog__) assert processed_warlog[0].clan.tag == CLAN_TAG assert processed_warlog[0].rank == 1 assert processed_warlog[0].date == '2/9' assert processed_warlog[0].trophy_change == 111
def test_process_clan(tmpdir): config_file = tmpdir.mkdir('test_process_clan').join('testfile') config_file.write(__config_file_score__) config = load_config_file(config_file.realpath()) processed_clan = crtools.process_clan(config, __fake_clan__, __fake_currentwar_warday__) assert 'member_list' not in processed_clan assert processed_clan['warLeague'] == 'gold' assert processed_clan['warLeagueName'] == 'Gold League' assert processed_clan['currentWarState'] == 'warDay'
def test_config_paths_empty(tmpdir): config_file = tmpdir.mkdir('test_config_paths_empty').join('config.ini') config_file.write( __config_paths_template__.format(logo=False, favicon=False, description=False)) config = load_config_file(config_file.realpath()) assert config['paths']['clan_logo'].endswith('/templates/crtools-logo.png') assert config['paths']['favicon'].endswith( '/templates/crtools-favicon.ico') assert config['paths']['description_html_src'] == None
def test_version_update_request_fail(requests_mock, tmpdir): mock_object = {} requests_mock.get(PYPI_URL, json=mock_object, status_code=500) config_file = tmpdir.mkdir('test_config_unknown_key').join('config.ini') config_file.write(__config_debug) config = load_config_file(config_file.realpath(), True) assert config['crtools']['latest_version'] == config['crtools']['version'] assert config['crtools']['update_available'] == False
def test_member_warlog(tmpdir): config_file = tmpdir.mkdir('test_member_warlog').join('testfile') config_file.write(__config_file_score__) config = load_config_file(config_file.realpath()) warlog = crtools.member_warlog(config, __fake_clan__['member_list'][0], __fake_warlog__) assert warlog[0]['status'] == 'good' warlog = crtools.member_warlog(config, __fake_clan__['member_list'][1], __fake_warlog__) assert warlog[0]['status'] == 'na'
def test_process_current_war_collection(): config = load_config_file(False) war = ProcessedCurrentWar(config=config, current_war=pyroyale.WarCurrent( state='collectionDay', collection_end_time='20190209T212846.354Z', clan=__fake_war_clan__, participants=__fake_war_participants__)) assert war.collection_end_time assert war.end_label