def test_clean_file_no_uploaded_file(self):
     workflow = Workflow.create_and_init()
     tab = workflow.tabs.first()
     step = tab.steps.create(module_id_name="uploadfile", order=0, slug="step-1")
     context = self._render_context(step_id=step.id)
     result = clean_value(ParamDType.File(), str(uuid.uuid4()), context)
     self.assertIsNone(result)
     # Assert that if a temporary file was created to house the download, it
     # no longer exists.
     self.assertListEqual(list(self.basedir.iterdir()), [])
 def test_clean_file_wrong_step(self):
     workflow = Workflow.create_and_init()
     tab = workflow.tabs.first()
     step = tab.steps.create(module_id_name="uploadfile", order=0, slug="step-1")
     step2 = tab.steps.create(module_id_name="uploadfile", order=1, slug="step-2")
     id = str(uuid.uuid4())
     key = f"wf-${workflow.id}/wfm-${step.id}/${id}"
     s3.put_bytes(s3.UserFilesBucket, key, b"1234")
     UploadedFile.objects.create(
         step=step2, name="x.csv.gz", size=4, uuid=id, key=key
     )
     context = self._render_context(step_id=step.id)
     result = clean_value(ParamDType.File(), id, context)
     self.assertIsNone(result)
     # Assert that if a temporary file was created to house the download, it
     # no longer exists.
     self.assertListEqual(list(self.basedir.iterdir()), [])
    def test_clean_file_happy_path(self):
        workflow = Workflow.create_and_init()
        tab = workflow.tabs.first()
        step = tab.steps.create(module_id_name="uploadfile", order=0, slug="step-1")
        id = str(uuid.uuid4())
        key = f"wf-${workflow.id}/wfm-${step.id}/${id}"
        s3.put_bytes(s3.UserFilesBucket, key, b"1234")
        UploadedFile.objects.create(
            step=step, name="x.csv.gz", size=4, uuid=id, key=key
        )
        with ExitStack() as inner_stack:
            context = self._render_context(step_id=step.id, exit_stack=inner_stack)
            result: Path = clean_value(ParamDType.File(), id, context)
            self.assertIsInstance(result, Path)
            self.assertEqual(result.read_bytes(), b"1234")
            self.assertEqual(result.suffixes, [".csv", ".gz"])

        # Assert that once `exit_stack` goes out of scope, file is deleted
        self.assertFalse(result.exists())
 def test_clean_file_error(self):
     with self.assertRaisesRegex(RuntimeError, "Unsupported: fetch file"):
         clean_value(ParamDType.File(), None, None)
 def test_clean_file_none(self):
     result = clean_value(ParamDType.File(), None, None)
     self.assertEqual(result, None)
Example #6
0
 def dtype(self) -> Optional[ParamDType]:
     return ParamDType.File()