diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-09-20 13:24:28 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-09-20 13:24:28 +0200 |
commit | 299398ce87fd67a4a24074719b764ac5af300331 (patch) | |
tree | bfcf9e6f85b63dcb7522c07080147840ffd4c059 | |
parent | 5bd4b53d619ea14cd01a2e5adbfd1f2b8992ab9c (diff) |
Add the ability to set a default value for a field.
-rw-r--r-- | lesana/collection.py | 4 | ||||
-rw-r--r-- | tests/data/complex/settings.yaml | 5 | ||||
-rw-r--r-- | tests/test_collection.py | 14 |
3 files changed, 21 insertions, 2 deletions
diff --git a/lesana/collection.py b/lesana/collection.py index a8980eb..d2f029d 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -51,7 +51,9 @@ class Entry(object): if not field.get('help', None) is None: data += "# {name} ({type}): {help}\n".format(**field) t = field['type'] - if t == 'string': + if field.get('default', None): + data += ("{name}: '{default}'\n".format(**field)) + elif t == 'string': data += ("{name}: ''\n".format(**field)) elif t == 'text': data += ("{name}: |\n .\n".format(**field)) diff --git a/tests/data/complex/settings.yaml b/tests/data/complex/settings.yaml index 440f4e9..03110f3 100644 --- a/tests/data/complex/settings.yaml +++ b/tests/data/complex/settings.yaml @@ -26,3 +26,8 @@ fields: - name: exists type: boolean index: field + - name: with_default + type: string + default: 'default value' + - name: amount + type: integer diff --git a/tests/test_collection.py b/tests/test_collection.py index 09ff96c..4160c5e 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -260,6 +260,11 @@ class testEntries(unittest.TestCase): res = entry.render('tests/data/simple/templates/trivial_template.txt') self.assertIn(uid, res) + def test_empty_data(self): + entry = lesana.Entry(self.collection) + self.assertIn("name: ''", entry.yaml_data) + self.assertIn('quantity: 0', entry.yaml_data) + class testComplexCollection(unittest.TestCase): @classmethod @@ -276,7 +281,7 @@ class testComplexCollection(unittest.TestCase): self.collection.settings['name'], "Fully featured lesana collection" ) - self.assertEqual(len(self.collection.settings['fields']), 7) + self.assertEqual(len(self.collection.settings['fields']), 9) self.assertIsNotNone(self.collection.stemmer) self.assertEqual(len(self.collection.indexed_fields), 6) @@ -298,6 +303,13 @@ class testComplexCollection(unittest.TestCase): ) self.assertIsInstance(entry.data['exists'], bool) + def test_empty_data(self): + entry = lesana.Entry(self.collection) + print(entry.yaml_data) + self.assertIn("name: ''", entry.yaml_data) + self.assertIn('with_default', entry.yaml_data) + self.assertIn('amount: 0', entry.yaml_data) + class testCollectionWithErrors(unittest.TestCase): @classmethod |