def test_expand_constraint_message(): s1 = {'survey': [{'type': 'integer', 'constraint': '. > 3', 'label::XX': 'X number', 'label::YY': 'Y number', 'constraint_message::XX': 'X: . > 3', 'constraint_message::YY': 'Y: . > 3', }], 'translated': ['constraint_message', 'label'], 'translations': ['XX', 'YY']} s1_copy = copy.deepcopy(s1) x1 = {'survey': [{'type': 'integer', 'constraint': '. > 3', 'label': ['X number', 'Y number'], 'constraint_message': ['X: . > 3', 'Y: . > 3'], }], 'schema': SCHEMA_VERSION, 'translated': ['constraint_message', 'label'], 'translations': ['XX', 'YY'], } expand_content(s1, in_place=True) assert s1 == x1 flatten_content(x1, in_place=True) s1_copy.pop('translated') s1_copy.pop('translations') assert x1 == s1_copy
def test_flatten_select_or_other(): s1 = {'survey': [{'type': 'select_one_or_other', 'select_from_list_name': 'xyz'}]} flatten_content(s1, in_place=True) row0 = s1['survey'][0] assert row0['type'] == 'select_one xyz or_other' assert 'select_from_list_name' not in row0
def test_flatten_select(): s1 = {'survey': [{'type': 'select_one', 'select_from_list_name': 'aaa'}]} flatten_content(s1, in_place=True) row0 = s1['survey'][0] assert row0['type'] == 'select_one aaa' assert 'select_from_list_name' not in row0
def test_flatten_select_type(): s1 = {'survey': [{'type': 'select_multiple', 'select_from_list_name': 'xyz'}]} flatten_content(s1, in_place=True) row0 = s1['survey'][0] assert row0['type'] == 'select_multiple xyz' assert 'select_from_list_name' not in row0
def test_flatten_fails_with_not_enough_translations_listed(): with pytest.raises(ValueError) as err: flatten_content({'survey': [ {'type': 'text', 'name': 'q1', 'label': ['lang1', 'lang2', 'lang3'], }, ], 'translated': ['label'], 'translations': ['lang1', 'lang2'], }) assert 'Incorrect translation count: "label"' in str(err.value)
def test_expand_translations(): s1 = {'survey': [{'type': 'text', 'label::English': 'OK?', 'label::Français': 'OK!'}]} x1 = {'survey': [{'type': 'text', 'label': ['OK?', 'OK!']}], 'schema': SCHEMA_VERSION, 'translated': ['label'], 'translations': ['English', 'Français']} expand_content(s1, in_place=True) assert s1 == x1 flatten_content(s1, in_place=True) assert s1 == {'survey': [{'type': 'text', 'label::English': 'OK?', 'label::Français': 'OK!'}], }
def test_flatten_label_with_xpath(): _c = flatten_content({'survey': [ {'type': 'acknowledge', 'name': 'verifnote', 'label': [ 'You answered ', {'@lookup': 'foo'}, ' to the foo question' ]} ]}) assert _c['survey'][0]['label'] == 'You answered ${foo} to the foo question'
def test_expand_translated_media(): s1 = {'survey': [{'type': 'note', 'media::image::English': 'eng.jpg' }]} expand_content(s1, in_place=True) assert s1 == {'survey': [ {'type': 'note', 'media::image': ['eng.jpg'] } ], 'translated': ['media::image'], 'schema': SCHEMA_VERSION, 'translations': ['English']} flatten_content(s1, in_place=True) assert s1 == {'survey': [{ 'type': 'note', 'media::image::English': 'eng.jpg', }], }
def test_expand_media(): s1 = {'survey': [{'type': 'note', 'media::image': 'ugh.jpg'}]} expand_content(s1, in_place=True) assert s1 == { 'survey': [{ 'type': 'note', 'media::image': ['ugh.jpg'] }], 'translated': ['media::image'], 'translations': [UNTRANSLATED], 'schema': SCHEMA_VERSION, } flatten_content(s1, in_place=True) assert s1 == { 'survey': [{ 'type': 'note', 'media::image': 'ugh.jpg', }], }
def test_flatten_can_remove_column(): c = flatten_content({'survey': [ {'type': 'text', 'name': 'q1', 'label': 'lang1' }, ], }, remove_columns={'survey':['label']}, ) assert 'label' not in c['survey'][0].keys()
def test_flatten_can_remove_sheet(): c = flatten_content({'survey': [ {'type': 'text', 'name': 'q1', 'label': 'lang1' }, ], }, remove_sheets=['survey'], ) assert 'survey' not in c
def test_expand_media(): s1 = {'survey': [{'type': 'note', 'media::image': 'ugh.jpg'}]} expand_content(s1, in_place=True) assert s1 == {'survey': [ { 'type': 'note', 'media::image': ['ugh.jpg'] } ], 'translated': ['media::image'], 'translations': [UNTRANSLATED], 'schema': SCHEMA_VERSION, } flatten_content(s1, in_place=True) assert s1 == {'survey': [{ 'type': 'note', 'media::image': 'ugh.jpg', }], }
def test_expand_translations_null_lang(): s1 = {'survey': [{'type': 'text', 'label': 'NoLang', 'label::English': 'EnglishLang'}], 'translated': ['label'], 'translations': [UNTRANSLATED, 'English']} x1 = {'survey': [{'type': 'text', 'label': ['NoLang', 'EnglishLang']}], 'schema': SCHEMA_VERSION, 'translated': ['label'], 'translations': [UNTRANSLATED, 'English']} s1_copy = copy.deepcopy(s1) expand_content(s1, in_place=True) assert s1.get('translations') == x1.get('translations') assert s1.get('translated') == ['label'] assert s1.get('survey')[0] == x1.get('survey')[0] assert s1 == x1 flatten_content(s1, in_place=True) s1_copy.pop('translated') s1_copy.pop('translations') assert s1 == s1_copy
def test_expand_translated_media_mangled_format(): """ An unfortunate bug seen in formpack#115 has resulted in needing to account for this behaviour if surveys used image::lang rather than media::image::lang """ s1 = { 'survey': [ { 'type': 'note', 'media::image': ['eng.jpg'], }, ], 'translated': ['image'], # Bug 🐛: not coming through as media::image 'schema': SCHEMA_VERSION, 'translations': ['English (en)'], } expand_content(s1, in_place=True) assert s1 == { 'survey': [ { 'type': 'note', 'media::image': ['eng.jpg'], }, ], 'translated': ['media::image'], 'schema': SCHEMA_VERSION, 'translations': ['English (en)'], } flatten_content(s1, in_place=True) assert s1 == { 'survey': [ { 'type': 'note', 'media::image::English (en)': 'eng.jpg', }, ], }
def test_flatten_translated_label_with_xpath(): _c = flatten_content({'survey': [ {'type': 'acknowledge', 'name': 'verifnote', 'label': [ [ 'You answered ', {'@lookup': 'foo'}, ' to the foo question' ], [ 'U ansrd ', {'@lookup': 'foo'}, ' 2da foo q' ], ]}, ], 'translated': ['label'], 'translations': ['en', 'txtspk'], }) row = _c['survey'][0] assert 'label' not in row assert row['label::en'] == 'You answered ${foo} to the foo question' assert row['label::txtspk'] == 'U ansrd ${foo} 2da foo q'
def _flatten_type(_r): return flatten_content({'survey': [_r]})['survey'][0]['type']
def test_flatten_constraints(): a1 = _wrap_field('constraint', ['.', '>', {'@lookup': 'x'}]) flatten_content(a1, in_place=True) ss_struct = a1['survey'] assert ss_struct[1]['constraint'] == '. > ${x}'
def test_flatten_select_one_type_deprecated_format(): a1 = _wrap_type({'select_one': 'yn'}) flatten_content(a1, in_place=True) ss_struct = a1['survey'] assert ss_struct[0]['type'] == 'select_one yn'
def test_flatten_rank_type(): a1 = _wrap_type({'rank': 'yn'}) flatten_content(a1, in_place=True) ss_struct = a1['survey'] assert ss_struct[0]['type'] == 'rank yn'
def test_flatten_select_multiple_from_file_type(): a1 = _wrap_type({'select_multiple_from_file': 'yn.csv'}) flatten_content(a1, in_place=True) ss_struct = a1['survey'] assert ss_struct[0]['type'] == 'select_multiple_from_file yn.csv'
def valid_xlsform_content(self): _flattened_content = flatten_content(self.content) return to_xlsform_structure(_flattened_content)
def test_flatten_relevant(): a1 = _wrap_field('relevant', [{'@lookup': 'x'}]) flatten_content(a1, in_place=True) ss_struct = a1['survey'] assert ss_struct[1]['relevant'] == '${x}'
def test_flatten_empty_relevant(): a1 = _wrap_field('relevant', []) flatten_content(a1, in_place=True) ss_struct = a1['survey'] assert ss_struct[1]['relevant'] == ''
def test_flatten_select_multiple_type(): a1 = _wrap_type({'select_multiple': 'yn'}) flatten_content(a1, in_place=True) ss_struct = a1['survey'] assert ss_struct[0]['type'] == 'select_multiple yn'
def flattened_content_copy(self): _c = self.standardized_content_copy() flatten_content(_c, in_place=True) return to_xlsform_structure(_c, move_autonames=True)
def to_ss_structure(self): return flatten_content(self.content, in_place=False)
def to_ss_structure(self): return flatten_content(copy.deepcopy(self.content))