2021-10-18 20:56:03 +00:00
|
|
|
import unittest
|
2021-10-21 17:37:56 +00:00
|
|
|
from tests.base_test import BaseTest
|
2020-05-18 15:55:10 +00:00
|
|
|
import copy
|
2021-10-21 17:37:56 +00:00
|
|
|
import docx
|
2020-05-18 15:55:10 +00:00
|
|
|
|
|
|
|
from docxtpl import Listing
|
2021-10-21 17:37:56 +00:00
|
|
|
from io import BytesIO
|
2020-05-18 15:55:10 +00:00
|
|
|
|
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
|
2020-05-18 15:55:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestCompleteTemplate(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_rich_text_update(self):
|
2021-10-21 14:31:02 +00:00
|
|
|
script = JinjaService()
|
2020-05-18 15:55:10 +00:00
|
|
|
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)
|
2020-05-18 15:55:10 +00:00
|
|
|
|
|
|
|
def test_rich_text_update_new_line(self):
|
2021-10-21 14:31:02 +00:00
|
|
|
script = JinjaService()
|
2020-05-18 15:55:10 +00:00
|
|
|
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()
|
2020-05-18 15:55:10 +00:00
|
|
|
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)
|