edit descriptor and his test

This commit is contained in:
Bastian Schroll 2018-01-07 23:32:09 +01:00
parent bf6777d095
commit b275abd86f
2 changed files with 59 additions and 22 deletions

View file

@ -34,29 +34,40 @@ class Descriptor:
def loadDescription(self, csvType):
"""!Build a new description list
@param csvType: Name of the CSV file without `.csv`"""
self._lists[csvType] = DescriptionList(csvType)
@param csvType: Name of the CSV file without `.csv`
@return True or False"""
bwDescriptionList = DescriptionList()
if bwDescriptionList.loadCSV(csvType):
self._lists[csvType] = bwDescriptionList
return True
return False
def addDescriptions(self, bwPacket):
"""!Add the short and long description to a bwPacket
@param bwPacket: bwPacket instance to add descriptions"""
@param bwPacket: bwPacket instance to add descriptions
@return True or False"""
logging.debug("add descriptions to bwPacket")
try:
bwPacket.setField("shortDescription",
self._lists[bwPacket.getField("mode")].getShortDescription(bwPacket.getField(bwPacket.getField("mode"))))
bwPacket.setField("longDescription",
self._lists[bwPacket.getField("mode")].getLongDescription(bwPacket.getField(bwPacket.getField("mode"))))
return True
except:
logging.exception("error while adding descriptions")
return False
class DescriptionList:
def __init__(self, csvType):
def __init__(self): # , csvType):
"""!Loads the given CSV file into internal list
@param csvType: Name of the CSV file without `.csv`"""
logging.debug("create new descriptionList")
self._descriptionList = {}
self._loadCSV(csvType)
# self.loadCSV(csvType)
def getShortDescription(self, id):
"""!Returns the short description of given id
@ -76,13 +87,7 @@ class DescriptionList:
except:
return ""
def _getFullList(self):
"""!Returns the full intern _descriptionList
@return descriptionList as dict"""
return self._descriptionList
def _loadCSV(self, csvType):
def loadCSV(self, csvType):
"""!Load descriptions from an csv file
@param csvType: Name of the CSV file without `.csv`

View file

@ -15,21 +15,53 @@
@description: Unittests for BOSWatch. File must be run as "pytest" unittest
"""
# import pytest # import the pytest framework
from boswatch.descriptor import descriptor
from boswatch.packet import packet
class Test_Descriptor:
"""!Unittests for the descriptor"""
def test_loadCSVnotExist(self):
"""!read CSV file where not exist"""
descList = descriptor.DescriptionList("boswatch")
assert bool(descList._getFullList()) is False
"""!read CSV file where not exist direct per DescriptionList class"""
descList = descriptor.DescriptionList()
assert descList.loadCSV("boswatch") is False
def test_loadCSV(self):
"""!read CSV file direct per DescriptionList class"""
descList = descriptor.DescriptionList()
assert descList.loadCSV("zvei") is True
def test_descriptorLoadFailed(self):
"""!read CSV file where not exist"""
bwDescriptor = descriptor.Descriptor()
assert bwDescriptor.loadDescription("boswatch") is False
def test_descriptorLoad(self):
"""!read CSV file"""
descList = descriptor.DescriptionList("zvei")
assert bool(descList._getFullList()) is True
bwDescriptor = descriptor.Descriptor()
assert bwDescriptor.loadDescription("zvei") is True
def test_loadDescriptionsNotSet(self):
"""!load descriptions where not set to an bwPacket"""
bwDescriptor = descriptor.Descriptor()
assert bwDescriptor.loadDescription("zvei") is True
bwPacket = packet.Packet()
bwPacket.setField("mode", "zvei")
bwPacket.setField("zvei", "54321")
assert bwDescriptor.addDescriptions(bwPacket) is True
assert bwPacket.getField("shortDescription") is ""
assert bwPacket.getField("longDescription") is ""
def test_loadDescriptions(self):
"""!load descriptions to an bwPacket"""
bwDescriptor = descriptor.Descriptor()
assert bwDescriptor.loadDescription("zvei") is True
bwPacket = packet.Packet()
bwPacket.setField("mode", "zvei")
bwPacket.setField("zvei", "12345")
assert bwDescriptor.addDescriptions(bwPacket) is True
assert bwPacket.getField("shortDescription") is not ""
assert bwPacket.getField("longDescription") is not ""