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) != \
int(skillValue):
actorChanged = True
setActorSkillLevel(actorJson, skillName, int(skillValue))
setActorSkillLevel(actorJson,
skillName, int(skillValue))
skillsStr = self.server.translate['Skills']
setHashtagCategory(baseDir, skillName,
skillsStr.lower())

View File

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

View File

@ -67,6 +67,8 @@ from person import setDisplayNickname
from person import setBio
# from person import generateRSAKey
from skills import setSkillLevel
from skills import setSkillsFromDict
from skills import getSkillsFromString
from roles import setRole
from roles import outboxDelegate
from auth import constantTimeStringCheck
@ -3731,9 +3733,32 @@ def testSpoofGeolocation() -> None:
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():
print('Running tests...')
testFunctions()
testSkills()
testSpoofGeolocation()
testRemovePostInteractions()
testExtractPGPPublicKey()