2021-08-26 12:44:22 +00:00
|
|
|
from tests.base_test import BaseTest
|
|
|
|
|
|
|
|
from crc import session
|
|
|
|
from crc.models.file import FileModel, FileDataModel
|
|
|
|
from crc.models.workflow import WorkflowModel
|
|
|
|
from crc.models.study import StudyModel
|
|
|
|
from crc.scripts.get_zipped_files import GetZippedFiles
|
|
|
|
from crc.services.file_service import FileService
|
|
|
|
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetZippedFiles(BaseTest):
|
|
|
|
|
|
|
|
def test_get_zipped_files(self):
|
|
|
|
self.load_example_data()
|
2021-09-14 14:41:01 +00:00
|
|
|
study = session.query(StudyModel).order_by(StudyModel.id.desc()).first()
|
2021-08-26 12:44:22 +00:00
|
|
|
workflow = session.query(WorkflowModel).first()
|
2021-09-14 14:41:01 +00:00
|
|
|
workflow_api = self.get_workflow_api(workflow)
|
|
|
|
task = workflow_api.next_task
|
|
|
|
|
|
|
|
# Add files to use in the test
|
2021-08-26 12:44:22 +00:00
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
name="document_1.png", content_type="text",
|
2021-09-14 14:41:01 +00:00
|
|
|
task_spec_name=task.name,
|
2021-08-26 12:44:22 +00:00
|
|
|
binary_data=b'1234', irb_doc_code='Study_Protocol_Document')
|
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
name="document_2.txt", content_type="text",
|
2021-09-14 14:41:01 +00:00
|
|
|
task_spec_name=task.name,
|
2021-08-26 12:44:22 +00:00
|
|
|
binary_data=b'1234', irb_doc_code='Study_App_Doc')
|
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
name="document_3.pdf", content_type="text",
|
2021-09-14 14:41:01 +00:00
|
|
|
task_spec_name=task.name,
|
2021-08-26 12:44:22 +00:00
|
|
|
binary_data=b'1234', irb_doc_code='Admin_AdvChklst')
|
2021-09-14 14:41:01 +00:00
|
|
|
|
|
|
|
# Gather the file_ids
|
2021-08-26 12:44:22 +00:00
|
|
|
file_ids = []
|
|
|
|
files = session.query(FileModel).filter(FileModel.irb_doc_code != '').all()
|
|
|
|
for file in files:
|
|
|
|
file_ids.append(file.id)
|
2021-09-14 14:41:01 +00:00
|
|
|
|
|
|
|
# Send files to the zipper, receive file_model in return
|
|
|
|
file_model = GetZippedFiles().do_task(task, study.id, workflow.id, file_ids=file_ids, filename='another_attachment.zip')
|
2021-08-26 12:44:22 +00:00
|
|
|
file_data = session.query(FileDataModel).filter(FileDataModel.file_model_id == file_model.id).first()
|
2021-09-14 14:41:01 +00:00
|
|
|
|
|
|
|
# Test what we get back in the file_model
|
2021-08-26 12:44:22 +00:00
|
|
|
with zipfile.ZipFile(io.BytesIO(file_data.data), 'r') as zf:
|
|
|
|
self.assertIsInstance(zf, zipfile.ZipFile)
|
|
|
|
for name in zf.namelist():
|
|
|
|
info = zf.getinfo(name)
|
2021-09-14 14:41:01 +00:00
|
|
|
self.assertIn(os.path.basename(info.filename), ['1 Protocol document_1.png', '1 Application document_2.txt', '1 document_3.pdf'])
|
2021-08-26 12:44:22 +00:00
|
|
|
file = zf.read(name)
|
|
|
|
self.assertEqual(b'1234', file)
|