Skip to content

Commit

Permalink
Completed code coverage of get_suggestions() per issue #11
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronTraas committed Mar 4, 2019
1 parent 480318c commit a3f21ed
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
15 changes: 10 additions & 5 deletions crtools/crtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

HISTORY_FILE_NAME = 'history.json'

MAX_CLAN_SIZE = 50

SUGGESTION_RECRUIT ='<strong>Recruit new members!</strong> The team needs some fresh blood.'
SUGGESTION_NO_SUGGESTIONS = 'No suggestions at this time. The clan is in good order.'

logger = logging.getLogger(__name__)

def write_object_to_file(file_path, obj):
Expand Down Expand Up @@ -201,13 +206,13 @@ def war_score(config, war):

return war_score

def get_suggestions(config, members):
def get_suggestions(config, processed_members):
""" Returns list of suggestions for the clan leadership to perform.
Suggestions are to kick, demote, or promote. Suggestions are based on
user score, and various thresholds in configuration. """

# sort members by score, and preserve trophy order if relevant
members_by_score = sorted(members, key=lambda k: (k['score'], k['trophies']))
members_by_score = sorted(processed_members, key=lambda k: (k['score'], k['trophies']))

logger.debug("min_clan_size: {}".format(config['score']['min_clan_size']))
logger.debug("# members: {}".format(len(members_by_score)))
Expand Down Expand Up @@ -237,10 +242,10 @@ def get_suggestions(config, members):

# If there are no other suggestions, give some sort of message
if len(suggestions) == 0:
if config['score']['min_clan_size'] >= len(members_by_score):
suggestions.append('<strong>Recruit new members!</strong> The team needs some fresh blood.')
if len(members_by_score) < MAX_CLAN_SIZE:
suggestions.append(SUGGESTION_RECRUIT)
else:
suggestions.append('No suggestions at this time. The clan is in good order.')
suggestions.append(SUGGESTION_NO_SUGGESTIONS)

return suggestions

Expand Down
88 changes: 87 additions & 1 deletion tests/test_crtools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from datetime import datetime, timedelta
import copy
import json
import os
import shutil

from crtools import crtools, load_config_file
from crtools import crtools, load_config_file, history

CLAN_TAG = '#FakeClanTag'

Expand Down Expand Up @@ -86,6 +87,19 @@
"name": "Arena 7"
},
"join_date": 0
},
{
"tag": "#EEEEEE",
"name": "MemberPersonToBePromoted",
"role": "member",
"expLevel": 8,
"trophies": 2144,
"donations": 100000000,
"arena": {
"id": 54000008,
"name": "Arena 7"
},
"join_date": 0
}
]
}
Expand Down Expand Up @@ -353,6 +367,78 @@ def test_get_scoring_rules(tmpdir):
assert rules[4]['yes_status'] == 'good'
assert rules[4]['no_status'] == 'normal'

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_clan__['memberList'], None)

members = crtools.process_members(config, __fake_clan__, __fake_warlog__, {"state": "notInWar"}, h)

suggestions = crtools.get_suggestions(config, members)

print(suggestions)

assert len(suggestions) == 1
assert suggestions[0] == crtools.SUGGESTION_RECRUIT

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
})

suggestions = crtools.get_suggestions(config, members)

assert len(suggestions) == 1
assert suggestions[0] == crtools.SUGGESTION_NO_SUGGESTIONS

def test_get_suggestions_kick(tmpdir):
config_file = tmpdir.mkdir('test_get_suggestions').join('testfile')
config_file.write(__config_file_score__ + '\nmin_clan_size=1')
config = load_config_file(config_file.realpath())

h = history.get_member_history(__fake_clan__['memberList'], None)

members = crtools.process_members(config, __fake_clan__, __fake_warlog__, {"state": "notInWar"}, h)

suggestions = crtools.get_suggestions(config, members)

print(suggestions)

assert suggestions[0].startswith('Kick')
assert members[3]['name'] in suggestions[0]

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_clan__['memberList'], None)

members = crtools.process_members(config, __fake_clan__, __fake_warlog__, {"state": "notInWar"}, h)

suggestions = crtools.get_suggestions(config, members)

print(suggestions)

assert suggestions[0].startswith('Demote')
assert members[2]['name'] in suggestions[0]
assert suggestions[1].startswith('Promote')
assert members[4]['name'] in suggestions[1]

def test_process_clan(tmpdir):
config_file = tmpdir.mkdir('test_process_clan').join('testfile')
config_file.write(__config_file_score__)
Expand Down

0 comments on commit a3f21ed

Please sign in to comment.