Unit tests for skills functions

main
Bob Mottram 2021-05-13 15:13:27 +01:00
parent 40ed35c239
commit 57015d6000
3 changed files with 59 additions and 27 deletions

View File

@ -4217,7 +4217,8 @@ class PubServer(BaseHTTPRequestHandler):
if actorSkillValue(actorJson, skillName) != \ if actorSkillValue(actorJson, skillName) != \
int(skillValue): int(skillValue):
actorChanged = True actorChanged = True
setActorSkillLevel(actorJson, skillName, int(skillValue)) setActorSkillLevel(actorJson,
skillName, int(skillValue))
skillsStr = self.server.translate['Skills'] skillsStr = self.server.translate['Skills']
setHashtagCategory(baseDir, skillName, setHashtagCategory(baseDir, skillName,
skillsStr.lower()) skillsStr.lower())

View File

@ -17,14 +17,16 @@ from utils import getDomainFromActor
from utils import loadJson from utils import loadJson
def _setSkillsFromDict(actorJson: {}, skills: {}) -> None: def setSkillsFromDict(actorJson: {}, skillsDict: {}) -> str:
"""Converts a dict containing skills to a string """Converts a dict containing skills to a string
Returns the string version of the dictionary
""" """
skillsStr = '' skillsStr = ''
for name, value in skills.items(): for name, value in skillsDict.items():
if skillsStr: if skillsStr:
skillsStr += ', ' skillsStr += ', '
skillsStr += name + ':' + str(value) skillsStr += name + ':' + str(value)
actorJson['hasOccupation']['skills'] = skillsStr
return skillsStr return skillsStr
@ -32,7 +34,7 @@ def getSkillsFromString(skillsStr: str) -> {}:
"""Returns a dict of skills from a string """Returns a dict of skills from a string
""" """
skillsList = skillsStr.split(',') skillsList = skillsStr.split(',')
skills = {} skillsDict = {}
for skill in skillsList: for skill in skillsList:
if ':' not in skill: if ':' not in skill:
continue continue
@ -40,28 +42,30 @@ def getSkillsFromString(skillsStr: str) -> {}:
valueStr = skill.split(':')[1] valueStr = skill.split(':')[1]
if not valueStr.isdigit(): if not valueStr.isdigit():
continue continue
skills[name] = int(valueStr) skillsDict[name] = int(valueStr)
return skills return skillsDict
def actorHasSkill(actorJson: {}, skillName: str) -> bool: def actorHasSkill(actorJson: {}, skillName: str) -> bool:
"""Returns true if the actor has the given skill """Returns true if the actor has the given skill
""" """
skills = getSkillsFromString(actorJson['hasOccupation']['skills']) skillsDict = \
if not skills: getSkillsFromString(actorJson['hasOccupation']['skills'])
if not skillsDict:
return False return False
return skills.get(skillName.lower()) return skillsDict.get(skillName.lower())
def actorSkillValue(actorJson: {}, skillName: str) -> int: def actorSkillValue(actorJson: {}, skillName: str) -> int:
"""Returns The skill level from an actor """Returns The skill level from an actor
""" """
skills = getSkillsFromString(actorJson['hasOccupation']['skills']) skillsDict = \
if not skills: getSkillsFromString(actorJson['hasOccupation']['skills'])
if not skillsDict:
return 0 return 0
skillName = skillName.lower() skillName = skillName.lower()
if skills.get(skillName): if skillsDict.get(skillName):
return skills[skillName] return skillsDict[skillName]
return 0 return 0
@ -83,20 +87,22 @@ def setActorSkillLevel(actorJson: {},
if skillLevelPercent < 0 or skillLevelPercent > 100: if skillLevelPercent < 0 or skillLevelPercent > 100:
return False return False
if actorJson: if not actorJson:
if not actorJson.get('hasOccupation'): return True
actorJson['hasOccupation'] = { if not actorJson.get('hasOccupation'):
'@type': 'Occupation', actorJson['hasOccupation'] = {
'name': '', '@type': 'Occupation',
'skills': '' 'name': '',
} 'skills': ''
skills = getSkillsFromString(actorJson['hasOccupation']['skills']) }
if skillLevelPercent > 0: skillsDict = \
skills[skill] = skillLevelPercent getSkillsFromString(actorJson['hasOccupation']['skills'])
else: if skillLevelPercent > 0:
if skills.get(skill): skillsDict[skill] = skillLevelPercent
del skills[skill] else:
_setSkillsFromDict(actorJson, skills) if skillsDict.get(skill):
del skillsDict[skill]
setSkillsFromDict(actorJson, skillsDict)
return True return True

View File

@ -67,6 +67,8 @@ from person import setDisplayNickname
from person import setBio from person import setBio
# from person import generateRSAKey # from person import generateRSAKey
from skills import setSkillLevel from skills import setSkillLevel
from skills import setSkillsFromDict
from skills import getSkillsFromString
from roles import setRole from roles import setRole
from roles import outboxDelegate from roles import outboxDelegate
from auth import constantTimeStringCheck from auth import constantTimeStringCheck
@ -3731,9 +3733,32 @@ def testSpoofGeolocation() -> None:
kmlFile.close() kmlFile.close()
def testSkills() -> None:
print('testSkills')
actorJson = {
'hasOccupation': {
'@type': 'Occupation',
'name': "",
'skills': ""
}
}
skillsDict = {
'bakery': 40,
'gardening': 70
}
setSkillsFromDict(actorJson, skillsDict)
assert actorJson['hasOccupation']['skills']
skillsDict = getSkillsFromString(actorJson['hasOccupation']['skills'])
assert skillsDict.get('bakery')
assert skillsDict.get('gardening')
assert skillsDict['bakery'] == 40
assert skillsDict['gardening'] == 70
def runAllTests(): def runAllTests():
print('Running tests...') print('Running tests...')
testFunctions() testFunctions()
testSkills()
testSpoofGeolocation() testSpoofGeolocation()
testRemovePostInteractions() testRemovePostInteractions()
testExtractPGPPublicKey() testExtractPGPPublicKey()