summaryrefslogtreecommitdiff
path: root/tests/test_collection.py
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-09-16 12:23:54 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-09-16 12:23:54 +0200
commitb3e0bcb8da7bea6c0b0636fdf68d0aac6ab74754 (patch)
treebbd329234a6fedd6691225364a4ba2cb26757b8e /tests/test_collection.py
parent6d920f6aad7f17a676883c6f3054b7b0d8be9844 (diff)
Add a method to create an entry from a rendered template
Diffstat (limited to 'tests/test_collection.py')
-rw-r--r--tests/test_collection.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/test_collection.py b/tests/test_collection.py
index d05415f..2308633 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -1,4 +1,5 @@
import datetime
+import decimal
import logging
import os.path
import shutil
@@ -294,6 +295,76 @@ class testSimpleCollection(unittest.TestCase):
{'value': None, 'frequency': 1},
])
+ def test_entry_from_template(self):
+ # TODO: make finding the templates less prone to breaking and
+ # then remove the cwd change from here
+ old_cwd = os.getcwd()
+ os.chdir(self.tmpdir)
+ data = {
+ "name": "This is a name",
+ }
+ entry = self.collection.entry_from_rendered_template(
+ "templates/new_entry_from_data.yaml",
+ data
+ )
+ os.chdir(old_cwd)
+ self.assertIsInstance(entry, lesana.Entry)
+ self.assertEqual(entry.data["name"], "This is a name")
+
+ def test_entry_from_template_multiple_data_sources(self):
+ # TODO: make finding the templates less prone to breaking and
+ # then remove the cwd change from here
+ old_cwd = os.getcwd()
+ os.chdir(self.tmpdir)
+ data = {
+ "name": "This is a name",
+ }
+ values = {
+ "quantity": 5,
+ "cost": decimal.Decimal("3.5"),
+ }
+ entry = self.collection.entry_from_rendered_template(
+ "templates/new_entry_from_multiple_data.yaml",
+ {
+ "data": data,
+ "values": values
+ }
+ )
+ os.chdir(old_cwd)
+ self.assertIsInstance(entry, lesana.Entry)
+ self.assertEqual(entry.data["name"], "This is a name")
+ self.assertEqual(entry.data["quantity"], 5)
+
+ def test_entry_from_bad_template(self):
+ # TODO: make finding the templates less prone to breaking and
+ # then remove the cwd change from here
+ old_cwd = os.getcwd()
+ os.chdir(self.tmpdir)
+ data = {
+ "name": "This is a name",
+ }
+ with self.assertRaises(lesana.collection.TemplatingError):
+ self.collection.entry_from_rendered_template(
+ "templates/new_entry_from_data_broken.yaml",
+ data
+ )
+ os.chdir(old_cwd)
+
+ def test_entry_from_bad_yaml(self):
+ # TODO: make finding the templates less prone to breaking and
+ # then remove the cwd change from here
+ old_cwd = os.getcwd()
+ os.chdir(self.tmpdir)
+ data = {
+ "name": "This is a name",
+ }
+ with self.assertRaises(lesana.collection.TemplatingError):
+ self.collection.entry_from_rendered_template(
+ "templates/new_entry_from_data_invalid_yaml.yaml",
+ data
+ )
+ os.chdir(old_cwd)
+
class testComplexCollection(unittest.TestCase):
def setUp(self):