|
| 1 | +from tests.pyxform_test_case import PyxformTestCase |
| 2 | +from tests.xpath_helpers.questions import xpq |
| 3 | + |
| 4 | + |
| 5 | +class TestRepeatCount(PyxformTestCase): |
| 6 | + """ |
| 7 | + Test usages of the survey repeat_count column. |
| 8 | + """ |
| 9 | + |
| 10 | + def test_single_reference__generated_element_same_name__ok(self): |
| 11 | + """Should not have a name clash, the referenced item should be used directly.""" |
| 12 | + md = """ |
| 13 | + | survey | |
| 14 | + | | type | name | label | repeat_count | |
| 15 | + | | integer | q1_count | Q1 | | |
| 16 | + | | begin repeat | r1 | R1 | ${q1_count} | |
| 17 | + | | text | q2 | Q2 | | |
| 18 | + | | end repeat | | | | |
| 19 | + """ |
| 20 | + self.assertPyxformXform( |
| 21 | + md=md, |
| 22 | + xml__xpath_match=[ |
| 23 | + # repeat_count target output as normal |
| 24 | + xpq.model_instance_item("q1_count"), |
| 25 | + xpq.model_instance_bind("q1_count", "int"), |
| 26 | + xpq.body_control("q1_count", "input"), |
| 27 | + # no instance element for generated *_count item |
| 28 | + """ |
| 29 | + /h:html/h:head/x:model/x:instance/x:test_name[not(./x:r1_count)] |
| 30 | + """, |
| 31 | + # no binding for generated *_count item |
| 32 | + """ |
| 33 | + /h:html/h:head/x:model[not(./x:bind[@nodeset='/test_name/r1_count'])] |
| 34 | + """, |
| 35 | + # repeat references existing count element directly. |
| 36 | + """ |
| 37 | + /h:html/h:body/x:group[@ref='/test_name/r1']/x:repeat[ |
| 38 | + @nodeset='/test_name/r1' |
| 39 | + and @jr:count=' /test_name/q1_count ' |
| 40 | + ] |
| 41 | + """, |
| 42 | + ], |
| 43 | + ) |
| 44 | + |
| 45 | + def test_single_reference__generated_element_different_name__ok(self): |
| 46 | + """Should find that a {repeat_name}_count element is generated for the calculation.""" |
| 47 | + md = """ |
| 48 | + | survey | |
| 49 | + | | type | name | label | repeat_count | |
| 50 | + | | integer | q1 | Q1 | | |
| 51 | + | | begin repeat | r1 | R1 | ${q1} | |
| 52 | + | | text | q2 | Q2 | | |
| 53 | + | | end repeat | | | | |
| 54 | + """ |
| 55 | + self.assertPyxformXform( |
| 56 | + md=md, |
| 57 | + xml__xpath_match=[ |
| 58 | + # repeat_count target output as normal |
| 59 | + xpq.model_instance_item("q1"), |
| 60 | + xpq.model_instance_bind("q1", "int"), |
| 61 | + xpq.body_control("q1", "input"), |
| 62 | + # no instance element for generated *_count item |
| 63 | + """ |
| 64 | + /h:html/h:head/x:model/x:instance/x:test_name[not(./x:r1_count)] |
| 65 | + """, |
| 66 | + # no binding for generated *_count item |
| 67 | + """ |
| 68 | + /h:html/h:head/x:model[not(./x:bind[@nodeset='/test_name/r1_count'])] |
| 69 | + """, |
| 70 | + # repeat references existing count element directly. |
| 71 | + """ |
| 72 | + /h:html/h:body/x:group[@ref='/test_name/r1']/x:repeat[ |
| 73 | + @nodeset='/test_name/r1' |
| 74 | + and @jr:count=' /test_name/q1 ' |
| 75 | + ] |
| 76 | + """, |
| 77 | + ], |
| 78 | + ) |
| 79 | + |
| 80 | + def test_expression__generated_element_same_name__error(self): |
| 81 | + """Should find that a duplicate {repeat_name}_count element raises an error.""" |
| 82 | + md = """ |
| 83 | + | survey | |
| 84 | + | | type | name | label | repeat_count | |
| 85 | + | | select_multiple l1 | r1_count | Q1 | | |
| 86 | + | | begin_repeat | r1 | R1 | count-selected(${r1_count}) | |
| 87 | + | | text | q2 | Q2 | | |
| 88 | + | | end_repeat | | | | |
| 89 | +
|
| 90 | + | choices | |
| 91 | + | | list_name | name | label | |
| 92 | + | | l1 | c1 | C1 | |
| 93 | + | | l1 | c2 | C2 | |
| 94 | + """ |
| 95 | + self.assertPyxformXform( |
| 96 | + md=md, |
| 97 | + errored=True, |
| 98 | + error__contains=[ |
| 99 | + "There are more than one survey elements named 'r1_count' " |
| 100 | + "(case-insensitive) in the section named 'test_name'." |
| 101 | + ], |
| 102 | + ) |
| 103 | + |
| 104 | + def test_expression__generated_element_different_name__ok(self): |
| 105 | + """Should find that a {repeat_name}_count element is generated for the calculation.""" |
| 106 | + # repro for pyxform 781 |
| 107 | + md = """ |
| 108 | + | survey | |
| 109 | + | | type | name | label | repeat_count | |
| 110 | + | | select_multiple l1 | q1 | Q1 | | |
| 111 | + | | begin_repeat | r1 | R1 | count-selected(${q1}) | |
| 112 | + | | text | q2 | Q2 | | |
| 113 | + | | end_repeat | | | | |
| 114 | +
|
| 115 | + | choices | |
| 116 | + | | list_name | name | label | |
| 117 | + | | l1 | c1 | C1 | |
| 118 | + | | l1 | c2 | C2 | |
| 119 | + """ |
| 120 | + self.assertPyxformXform( |
| 121 | + md=md, |
| 122 | + xml__xpath_match=[ |
| 123 | + xpq.model_instance_item("r1_count"), |
| 124 | + xpq.model_instance_bind("r1_count", "string"), |
| 125 | + xpq.model_instance_bind_attr( |
| 126 | + "r1_count", "calculate", "count-selected( /test_name/q1 )" |
| 127 | + ), |
| 128 | + xpq.model_instance_bind_attr("r1_count", "readonly", "true()"), |
| 129 | + # repeat references generated repeat_count element. |
| 130 | + """ |
| 131 | + /h:html/h:body/x:group[@ref='/test_name/r1']/x:repeat[ |
| 132 | + @nodeset='/test_name/r1' |
| 133 | + and @jr:count=' /test_name/r1_count ' |
| 134 | + ] |
| 135 | + """, |
| 136 | + ], |
| 137 | + ) |
| 138 | + |
| 139 | + def test_manual_xpath__generated_element_same_name__error(self): |
| 140 | + """Should find that a duplicate {repeat_name}_count element raises an error.""" |
| 141 | + md = """ |
| 142 | + | survey | |
| 143 | + | | type | name | label | repeat_count | |
| 144 | + | | select_multiple l1 | r1_count | Q1 | | |
| 145 | + | | begin_repeat | r1 | R1 | count-selected( /test_name/r1_count ) | |
| 146 | + | | text | q2 | Q2 | | |
| 147 | + | | end_repeat | | | | |
| 148 | +
|
| 149 | + | choices | |
| 150 | + | | list_name | name | label | |
| 151 | + | | l1 | c1 | C1 | |
| 152 | + | | l1 | c2 | C2 | |
| 153 | + """ |
| 154 | + self.assertPyxformXform( |
| 155 | + md=md, |
| 156 | + errored=True, |
| 157 | + error__contains=[ |
| 158 | + "There are more than one survey elements named 'r1_count' " |
| 159 | + "(case-insensitive) in the section named 'test_name'." |
| 160 | + ], |
| 161 | + ) |
| 162 | + |
| 163 | + def test_manual_xpath__generated_element_different_name__ok(self): |
| 164 | + """Should find that a {repeat_name}_count element is generated for the calculation.""" |
| 165 | + md = """ |
| 166 | + | survey | |
| 167 | + | | type | name | label | repeat_count | |
| 168 | + | | select_multiple l1 | q1 | Q1 | | |
| 169 | + | | begin_repeat | r1 | R1 | count-selected( /test_name/q1 ) | |
| 170 | + | | text | q2 | Q2 | | |
| 171 | + | | end_repeat | | | | |
| 172 | +
|
| 173 | + | choices | |
| 174 | + | | list_name | name | label | |
| 175 | + | | l1 | c1 | C1 | |
| 176 | + | | l1 | c2 | C2 | |
| 177 | + """ |
| 178 | + self.assertPyxformXform( |
| 179 | + md=md, |
| 180 | + xml__xpath_match=[ |
| 181 | + xpq.model_instance_item("r1_count"), |
| 182 | + xpq.model_instance_bind("r1_count", "string"), |
| 183 | + xpq.model_instance_bind_attr( |
| 184 | + "r1_count", "calculate", "count-selected( /test_name/q1 )" |
| 185 | + ), |
| 186 | + xpq.model_instance_bind_attr("r1_count", "readonly", "true()"), |
| 187 | + # repeat references generated repeat_count element. |
| 188 | + """ |
| 189 | + /h:html/h:body/x:group[@ref='/test_name/r1']/x:repeat[ |
| 190 | + @nodeset='/test_name/r1' |
| 191 | + and @jr:count=' /test_name/r1_count ' |
| 192 | + ] |
| 193 | + """, |
| 194 | + ], |
| 195 | + ) |
| 196 | + |
| 197 | + def test_constant_integer__generated_element_same_name__error(self): |
| 198 | + """Should find that a duplicate {repeat_name}_count element raises an error.""" |
| 199 | + # Seems strange, but according to pyxform 435 it's a javarosa limitation. |
| 200 | + md = """ |
| 201 | + | survey | |
| 202 | + | | type | name | label | repeat_count | |
| 203 | + | | integer | r1_count | Q1 | | |
| 204 | + | | begin_repeat | r1 | R1 | 2 | |
| 205 | + | | text | q2 | Q2 | | |
| 206 | + | | end_repeat | | | | |
| 207 | + """ |
| 208 | + self.assertPyxformXform( |
| 209 | + md=md, |
| 210 | + errored=True, |
| 211 | + error__contains=[ |
| 212 | + "There are more than one survey elements named 'r1_count' " |
| 213 | + "(case-insensitive) in the section named 'test_name'." |
| 214 | + ], |
| 215 | + ) |
| 216 | + |
| 217 | + def test_constant_integer__generated_element_different_name__ok(self): |
| 218 | + """Should find that a {repeat_name}_count element is generated for the calculation.""" |
| 219 | + # Seems strange, but according to pyxform 435 it's a javarosa limitation. |
| 220 | + md = """ |
| 221 | + | survey | |
| 222 | + | | type | name | label | repeat_count | |
| 223 | + | | integer | q1 | Q1 | | |
| 224 | + | | begin_repeat | r1 | R1 | 2 | |
| 225 | + | | text | q2 | Q2 | | |
| 226 | + | | end_repeat | | | | |
| 227 | + """ |
| 228 | + self.assertPyxformXform( |
| 229 | + md=md, |
| 230 | + xml__xpath_match=[ |
| 231 | + xpq.model_instance_item("r1_count"), |
| 232 | + xpq.model_instance_bind("r1_count", "string"), |
| 233 | + xpq.model_instance_bind_attr("r1_count", "calculate", "2"), |
| 234 | + xpq.model_instance_bind_attr("r1_count", "readonly", "true()"), |
| 235 | + # repeat references generated repeat_count element. |
| 236 | + """ |
| 237 | + /h:html/h:body/x:group[@ref='/test_name/r1']/x:repeat[ |
| 238 | + @nodeset='/test_name/r1' |
| 239 | + and @jr:count=' /test_name/r1_count ' |
| 240 | + ] |
| 241 | + """, |
| 242 | + ], |
| 243 | + ) |
0 commit comments