Repository: XLSForm/pyxform
Pull request #386 (2019-10-14 21:59:56)

#292 Resolve field references upload question
Address #292 by processing field references in upload question.
Establishes tests for upload (image, audio, file) question types


Patch:
diff --git a/pyxform/question.py b/pyxform/question.py
index 816f669..5a240a2 100644
--- a/pyxform/question.py
+++ b/pyxform/question.py
@@ -82,6 +82,10 @@ class UploadQuestion(Question):
 
     def xml_control(self):
         control_dict = self.control
+        survey = self.get_root()
+        # Resolve field references in attributes
+        for key, value in control_dict.items():
+            control_dict[key] = survey.insert_xpaths(value, self)
         control_dict["ref"] = self.get_xpath()
         control_dict["mediatype"] = self._get_media_type()
         return node("upload", *self.xml_label_and_hint(), **control_dict)


Test patch:
diff --git a/pyxform/tests_v1/test_upload_question.py b/pyxform/tests_v1/test_upload_question.py
new file mode 100644
index 0000000..c5c6951
--- /dev/null
+++ b/pyxform/tests_v1/test_upload_question.py
@@ -0,0 +1,83 @@
+"""
+Test upload (image, audio, file) question types in XLSForm
+"""
+from pyxform.tests_v1.pyxform_test_case import PyxformTestCase
+
+
+class UploadTest(PyxformTestCase):
+    def test_image_question(self):
+        self.assertPyxformXform(
+            name="data",
+            md="""
+            | survey |      |         |       |
+            |        | type | name    | label |
+            |        | image | photo | Take a photo: |
+            """,
+            errored=False,
+            xml__contains=[
+                '<bind nodeset="/data/photo" type="binary"/>',
+                '<upload mediatype="image/*" ref="/data/photo">',
+                "<label>Take a photo:</label>",
+                "</upload>",
+            ],
+        )
+
+    def test_audio_question(self):
+        self.assertPyxformXform(
+            name="data",
+            md="""
+            | survey |      |         |       |
+            |        | type | name    | label |
+            |        | audio | recording1 | Record a sound: |
+            """,
+            errored=False,
+            xml__contains=[
+                '<bind nodeset="/data/recording1" type="binary"/>',
+                '<upload mediatype="audio/*" ref="/data/recording1">',
+                "<label>Record a sound:</label>",
+                "</upload>",
+            ],
+        )
+
+    def test_file_question(self):
+        self.assertPyxformXform(
+            name="data",
+            md="""
+            | survey |      |         |       |
+            |        | type | name    | label |
+            |        | file | file1 | Upload a file: |
+            """,
+            errored=False,
+            xml__contains=[
+                '<bind nodeset="/data/file1" type="binary"/>',
+                '<upload mediatype="application/*" ref="/data/file1">',
+                "<label>Upload a file:</label>",
+                "</upload>",
+            ],
+        )
+
+    def test_file_question_restrict_filetype(self):
+        self.assertPyxformXform(
+            name="data",
+            md="""
+            | survey |      |         |       |       |
+            |        | type | name    | label | body::accept |
+            |        | file | upload_a_pdf | Upload a PDF: | application/pdf |
+            """,
+            errored=False,
+            xml__contains=['<upload accept="application/pdf"'],
+        )
+
+    def test_image_question_custom_col_calc(self):
+        self.assertPyxformXform(
+            name="data",
+            md="""
+            | survey |       |                  |                 |                               |
+            |        | type  | name             | label           | body:esri:style               |
+            |        | text  | watermark_phrase | Watermark Text: |                               |
+            |        | text  | text1            | Text            |                               |
+            |        | image | image1           | Take a Photo:   | watermark=${watermark_phrase} |
+            """,  # noqa
+            errored=False,
+            xml__contains=["watermark= /data/watermark_phrase "],
+        )