def test_get_role_label(): config = load_config_file(False) factory = MemberFactory(config, None, None, None) vacation_end = 999999999999999999 assert factory.get_role_label( '#fff', 'member', 0, 'good', MemberVacation(tag='#AAA'), True, False) == config['strings']['roleBlacklisted'] assert factory.get_role_label( '#fff', 'President', 100, 'bad', MemberVacation(tag='#AAA'), True, False) == config['strings']['roleBlacklisted'] assert factory.get_role_label('#fff', 'President', 100, 'bad', MemberVacation(tag='#AAA'), False, False) == config['strings']['roleVacation'] assert factory.get_role_label( '#fff', 'President', 100, 'bad', None, False, False) == config['strings']['roleInactive'].format(days=100) assert factory.get_role_label('#fff', 'President', 0, 'good', None, False, False) == config['strings']['roleLeader'] assert factory.get_role_label( '#fff', 'VicePresident', 0, 'good', None, False, False) == config['strings']['roleVicePresident'] assert factory.get_role_label('#fff', 'senior', 0, 'good', None, False, False) == config['strings']['roleSenior'] assert factory.get_role_label('#fff', 'member', 0, 'good', None, False, False) == config['strings']['roleMember'] assert factory.get_role_label('#fff', 'President', 0, 'good', None, False, True) == config['strings']['roleNoPromote']
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, pybrawl.WarCurrent(state = 'collectionDay'), None) == True assert discord.trigger_webhooks(config, pybrawl.WarCurrent(state = 'warDay'), None) == True
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 = bstools.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_process_current_war_nowar(): config = load_config_file(False) war = ProcessedCurrentWar(config=config, current_war=pybrawl.WarCurrent(state='notInWar')) assert war.state_label == 'The Club is not currently engaged in a war.'
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 = pybrawl.WarCurrent( state = 'collectionDay', collection_end_time = datetime.utcnow().strftime("%Y%m%dT%H%M%S.xxxx"), participants = [ pybrawl.WarParticipant( name = 'AAA', tag = '#AAA', battles_played = 1, number_of_battles = 3 ), pybrawl.WarParticipant( name = 'BBB', tag = '#BBB', battles_played = 1, number_of_battles = 3 ) ] ) fake_members = [pybrawl.WarParticipant(tag='#AAA')] assert discord.send_war_nag(config, fake_current_war, fake_members) == False
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_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_process_club(tmpdir): config_file = tmpdir.mkdir('test_process_club').join('testfile') config_file.write(__config_file_score__) config = load_config_file(config_file.realpath()) club = ProcessedClub( config=config, club=__fake_club__ )
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['bstools']['debug'] == True
def test_config_boolean(tmpdir): """ 'True' and 'False' should properly be parsed as booleans. Also, Should be case insensitive. """ config_file = tmpdir.mkdir('test_config_boolean').join('config.ini') config_file.write(__config_file_booleans__) config = load_config_file(config_file.realpath()) # input was 'False' assert config['score']['min_club_size'] == False assert type(config['score']['min_club_size']) == type(False)
def test_bad_locale_aborts_with_error(tmpdir, capsys): config_file = tmpdir.mkdir('test_bad_locale_defaults_to_default').join( 'config.ini') bad_locale = 'sdlkjfasldfjalsdfjalsdf' config_file.write('[bstools]\nlocale=' + bad_locale) with pytest.raises(SystemExit): config = load_config_file(config_file.realpath()) 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_current_war_collection(): config = load_config_file(False) war = ProcessedCurrentWar(config=config, current_war=pybrawl.WarCurrent( state='collectionDay', collection_end_time='20190209T212846.354Z', Club=__fake_war_club__, participants=__fake_war_participants__)) assert war.collection_end_time assert war.end_label
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']['club_logo'].endswith('/templates/bstools-logo.png') assert config['paths']['favicon'].endswith( '/templates/bstools-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['bstools']['latest_version'] == config['bstools']['version'] assert config['bstools']['update_available'] == False
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 = pybrawl.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_current_war_warday(): config = load_config_file(False) war = ProcessedCurrentWar(config=config, current_war=pybrawl.WarCurrent( state='warDay', war_end_time='20190209T212846.354Z', Club=__fake_war_club__, participants=__fake_war_participants__, clubs=[__fake_war_club__])) assert war.state_label == 'War Day' assert war.collection_end_time_label == 'Complete' assert war.end_label
def test_process_absent_members(tmpdir): config_file = tmpdir.mkdir('test_get_suggestions').join('testfile') config_file.write(__config_file_score__ + '\nthreshold_demote=-999999\nthreshold_promote=9999999') config = load_config_file(config_file.realpath()) h = history.get_member_history(__fake_club__.members, config['bstools']['timestamp'], __fake_history_old_member__) absent_members = bstools.process_absent_members(config, h['members']) assert len(absent_members) == 1 assert absent_members[0].tag == '#ZZZZZZ'
def test_version_update(requests_mock, tmpdir): latest_version = '99.99.99' mock_object = {'releases': {latest_version: []}} requests_mock.get(PYPI_URL, json=mock_object, status_code=200) 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['bstools']['latest_version'] != config['bstools']['version'] assert config['bstools']['latest_version'] == latest_version assert config['bstools']['update_available'] == True
def test_calc_donation_status(tmpdir): config_file = tmpdir.mkdir('test_calc_donation_status').join('config.ini') config_file.write(''' [Score] max_donations_bonus=40 min_donations_daily=10 ''') config = load_config_file(config_file.realpath()) factory = MemberFactory(config, None, None, None) assert factory.calc_donation_status(1000, 100, 6) == 'good' assert factory.calc_donation_status(0, 0, 6) == 'bad' assert factory.calc_donation_status(0, 5, 6) == 'ok' assert factory.calc_donation_status(0, 0, 0) == 'normal'
def test_calc_member_status(tmpdir): config_file = tmpdir.mkdir('test_calc_member_status').join('config.ini') config_file.write(''' [Score] threshold_promote=100 threshold_warn=10 ''') config = load_config_file(config_file.realpath()) factory = MemberFactory(config, None, None, None) assert factory.calc_member_status(-1, False) == 'bad' assert factory.calc_member_status(5, False) == 'ok' assert factory.calc_member_status(10, False) == 'normal' assert factory.calc_member_status(100, False) == 'good' assert factory.calc_member_status(100, True) == 'normal'
def test_get_member_data_from_sheets_mock_data(tmpdir, monkeypatch): def mock_get_sheet(*args, **kwargs): return MockSheet() monkeypatch.setattr(gdoc, "get_sheet", mock_get_sheet) config_file = tmpdir.mkdir( 'test_get_member_data_from_sheets_mock_data').join('config.ini') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) config = gdoc.get_member_data_from_sheets(config) assert config['members']['blacklist']['#AAA'].tag == '#AAA' assert config['members']['no_promote']['#BBB'].tag == '#BBB' assert config['members']['vacation']['#CCC'].tag == '#CCC'
def test_config_list(tmpdir): """ If the template property contains a list, parse the contents as a list """ config_file = tmpdir.mkdir('test_config_list').join('config.ini') config_file.write(__config_file_list__) config = load_config_file(config_file.realpath()) # did not parse ['api']['api_key'] as list, because was not a list in template assert type(config['api']['api_key']) == type('') # Parsed ['members']['blacklist'] in config as a list, because the template was a list assert type(config['members']['blacklist']) == type({}) assert config['members']['blacklist']['Foo'].tag == 'Foo' # Properly stripped whitespace from list element assert config['members']['vacation']['Baz'].tag == 'Baz' assert config['members']['vacation']['Baz'].start_date == 0
def test_get_member_data_from_sheets_null_sheets_object(tmpdir, monkeypatch): def mock_get_sheet(*args, **kwargs): return None monkeypatch.setattr(gdoc, "get_sheet", mock_get_sheet) config_file = tmpdir.mkdir( 'test_get_member_data_from_sheets_null_sheets_object').join( 'config.ini') config_file.write(__config_file__) config = load_config_file(config_file.realpath()) config = gdoc.get_member_data_from_sheets(config) assert config['members']['blacklist'] == {} assert config['members']['no_promote'] == {} assert config['members']['vacation'] == {}
def test_get_suggestions_kick(tmpdir): config_file = tmpdir.mkdir('test_get_suggestions').join('testfile') config_file.write(__config_file_score__ + '\nmin_club_size=1') config = load_config_file(config_file.realpath()) h = history.get_member_history(__fake_club__.members, config['bstools']['timestamp']) members = bstools.process_members(config, __fake_club__, __fake_warlog__, __fake_currentwar_notinwar__, h) suggestions = bstools.get_suggestions(config, members, __fake_club__.required_trophies) print(suggestions) assert suggestions[0].startswith('Kick') assert members[3].name in suggestions[0]
def test_get_suggestions_recruit(tmpdir): config_file = tmpdir.mkdir('test_get_suggestions').join('testfile') config_file.write(__config_file_score__ + '\nthreshold_demote=-999999\nthreshold_promote=9999999') config = load_config_file(config_file.realpath()) h = history.get_member_history(__fake_club__.members, config['bstools']['timestamp']) members = bstools.process_members(config, __fake_club__, __fake_warlog__, __fake_currentwar_notinwar__, h) suggestions = bstools.get_suggestions(config, members, __fake_club__) print(suggestions) assert len(suggestions) == 1 assert suggestions[0] == config['strings']['suggestionRecruit']
def test_get_suggestions_promote_demote(tmpdir): config_file = tmpdir.mkdir('test_get_suggestions').join('testfile') config_file.write(__config_file_score__ + '\nthreshold_promote=10') config = load_config_file(config_file.realpath()) h = history.get_member_history(__fake_club__.members, config['bstools']['timestamp']) members = bstools.process_members(config, __fake_club__, __fake_warlog__, __fake_currentwar_notinwar__, h) suggestions = bstools.get_suggestions(config, members, __fake_club__.required_trophies) print(suggestions) assert suggestions[0].startswith('Demote') assert members[2].name in suggestions[0] assert suggestions[1].startswith('Promote') or suggestions[2].startswith( 'Promote') assert members[4].name in suggestions[1] or members[4].name in suggestions[ 2]
def test_get_member_data_from_sheets_no_sheet_id_does_nothing(tmpdir, capsys): """ 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_get_member_data_from_sheets_no_sheet_id_does_nothing').join( 'config.ini') config_file.write(""" [google_docs] sheet_id=FAKE_SHEET_ID [bstools] debug=True """) config = load_config_file(config_file.realpath()) config = gdoc.get_member_data_from_sheets(config) assert config['members']['blacklist'] == {} assert config['members']['no_promote'] == {} assert config['members']['vacation'] == {}
def test_config_paths_valid(tmpdir): test_tmpdir = tmpdir.mkdir('test_config_paths_valid') logo = test_tmpdir.join('logo.png') favicon = test_tmpdir.join('favicon.ico') description = test_tmpdir.join('description.html') config_file = test_tmpdir.join('config.ini') config_file.write( __config_paths_template__.format(logo=str(logo.realpath()), favicon=favicon.realpath(), description=description.realpath())) logo.write('foo') favicon.write('foo') description.write('foo') config = load_config_file(config_file.realpath()) assert config['paths']['club_logo'].endswith( '/test_config_paths_valid/logo.png') assert config['paths']['favicon'].endswith( '/test_config_paths_valid/favicon.ico') assert config['paths']['description_html'].endswith( '/test_config_paths_valid/description.html')