cr-connect-workflow/tests/test_complete_template_scri...

66 lines
2.2 KiB
Python
Raw Normal View History

import unittest
2021-10-21 17:37:56 +00:00
from tests.base_test import BaseTest
import copy
2021-10-21 17:37:56 +00:00
import docx
from docxtpl import Listing
2021-10-21 17:37:56 +00:00
from io import BytesIO
2021-10-21 17:37:56 +00:00
from crc import session
from crc.models.file import FileModel, FileDataModel
2021-10-21 14:31:02 +00:00
from crc.services.jinja_service import JinjaService
class TestCompleteTemplate(unittest.TestCase):
def test_rich_text_update(self):
2021-10-21 14:31:02 +00:00
script = JinjaService()
data = {"name": "Dan"}
data_copy = copy.deepcopy(data)
script.rich_text_update(data_copy)
2020-06-05 18:08:46 +00:00
self.assertEqual(data, data_copy)
def test_rich_text_update_new_line(self):
2021-10-21 14:31:02 +00:00
script = JinjaService()
data = {"name": "Dan\n Funk"}
data_copy = copy.deepcopy(data)
script.rich_text_update(data_copy)
self.assertNotEqual(data, data_copy)
self.assertIsInstance(data_copy["name"], Listing)
def test_rich_text_nested_new_line(self):
2021-10-21 14:31:02 +00:00
script = JinjaService()
data = {"names": [{"name": "Dan\n Funk"}]}
data_copy = copy.deepcopy(data)
script.rich_text_update(data_copy)
self.assertNotEqual(data, data_copy)
self.assertIsInstance(data_copy["names"][0]["name"], Listing)
2021-10-21 17:37:56 +00:00
class TestEmbeddedTemplate(BaseTest):
def test_embedded_template(self):
workflow = self.create_workflow('docx_embedded')
workflow_api = self.get_workflow_api(workflow)
task = workflow_api.next_task
data = {'include_me': 'Hello {{ name }}!',
'name': 'World',
'file_name': 'simple.docx',
'irb_doc_code': 'Study_App_Doc'}
self.complete_form(workflow, task, data)
# Get the file data created for us in the workflow
file_model = session.query(FileModel).\
filter(FileModel.workflow_id == workflow.id).\
filter(FileModel.irb_doc_code == 'Study_App_Doc').\
first()
file_data_model = session.query(FileDataModel). \
filter(FileDataModel.file_model_id == file_model.id).\
first()
# read the data as a word document
document = docx.Document(BytesIO(file_data_model.data))
# Make sure 'Hello World!' is there
self.assertEqual('Hello World!', document.paragraphs[4].text)