Skip to content
Snippets Groups Projects
Commit 9059aa21 authored by Robert Habermann's avatar Robert Habermann
Browse files

fix #4 by adding a customizable field

parent 1f5af24b
No related branches found
No related tags found
No related merge requests found
......@@ -117,16 +117,26 @@ class Article(object):
else:
return []
def validate(self):
def validate(self, validation_map=None):
"""validate data against a mapping dict - if a key is not present
then set it with a default value according to dict
Args:
validation_map (dict): A mapping for all Article fields that have to be set
During validation every required field that is not set will be set to
a default value specified in this dict
.. note::
There is also a blacklist (fields to be removed) but this is currently
hardcoded to *list_dynamic_fields* and *list_attachments*
"""
validation_map = {"Body": "API created Article Body",
"Charset": "UTF8",
"MimeType": "text/plain",
"Subject": "API created Article",
"TimeUnit": 0}
if not validation_map:
validation_map = {"Body": "API created Article Body",
"Charset": "UTF8",
"MimeType": "text/plain",
"Subject": "API created Article",
"TimeUnit": 0}
dct = self.__dict__
......
......@@ -306,6 +306,29 @@ class ArticleTests(unittest.TestCase):
art.validate()
self.assertDictEqual(art.to_dct(), expected_validated)
def test_validation_custom(self):
"""Article validation; blacklisted fields should be removed, others should be added"""
custom_validation = {"Body": "API created Article Body",
"Charset": "UTF8",
"SpecialField": "SpecialValue",
"MimeType": "text/plain",
"Subject": "API created Article",
"TimeUnit": 0}
expected_validated = {'Article': {'Subject': 'This Article only has Subject',
'Body': 'and Body and needs to be completed.',
'TimeUnit': 0,
'MimeType': 'text/plain',
'Charset': 'UTF8',
'SpecialField': 'SpecialValue'}}
art = Article({'Subject': 'This Article only has Subject',
'Body': 'and Body and needs to be completed.'})
art.validate(validation_map=custom_validation)
self.assertDictEqual(art.to_dct(), expected_validated)
def main():
unittest.main()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment