def test_generate_anchors(): def get_row0s(surv): row0 = surv['survey'][0] if isinstance(surv['choices'], list): return (row0, surv['choices'][0]) return (row0, surv['choices']['xlistnamex'][0]) for _gen in [True, False]: # generate_anchors=True/False for content in [CONTENT_1, CONTENT_2]: # content.export(schema='1'/'2') for cs in ['1', '2']: akey = '$anchor' if (cs == '2') else '$kuid' result = Content(content, generate_anchors=_gen).export(schema=cs) if _gen: (row0, choice0) = get_row0s(result) assert akey in row0 assert akey in choice0 asval1 = row0[akey] acval1 = choice0[akey] for cs2 in ['1', '2']: # ensure anchor is preserved on second pass result_x = Content( result, generate_anchors=True).export(schema=cs2) akey = '$anchor' if (cs2 == '2') else '$kuid' (row0, choice0) = get_row0s(result_x) # survey[n]['$anchor'] does not get changed on pass 2 asval2 = row0[akey] assert asval1 == asval2 # choices[...]['$anchor'] does not get changed on pass 2 acval2 = choice0[akey] assert acval1 == acval2
def test_1_with_no_default_tx(): content = Content({ 'survey': [], 'translations': [ 'English', ], 'settings': {}, 'schema': '1', }) result = content.export(schema='2') defaults = [tx.get('default') for tx in result['translations']] assert defaults == [True]
def test_colons_forward(): result = Content({ 'schema': '1', 'survey': [ {'type': 'text', 'name': 'q1', 'label': ['t1lab', 't2lab']}, {'type': 'select_one', 'name': 'q2', 'select_from_list_name': 'xyz', 'label': ['s1lab', 's2lab'], 'hint': ['q2hintt1', 'q2hintt2'], } ], 'choices': [ {'list_name': 'xyz', 'value':'val1', 'label': [ 'c1lab', 'c2lab', ]} ], 'translations': [ 'T1', 'T2', ], 'settings': {}, 'translated': [ 'label', 'hint', ] }).export(schema='xlsform') row0 = result['survey'][0] choice0 = result['choices'][0] assert 'label::T1' in row0 assert 'label::T1' in choice0
def test_1_with_tx_locales(): content = Content({ 'survey': [], 'translations': [ 'English (en)', 'French (fr)', ], 'settings': { 'default_language': 'French (fr)', }, 'schema': '1', }) result = content.export(schema='2') result2 = Content(result).export(schema='1') defaults = [tx.get('default') for tx in result['translations']] assert defaults == [None, True]
def test_renames_column_on_import(): cc1 = BAREBONES_1.copy(translated=('media::image', )) cc2 = Content(cc1) row = Row(content=cc2, row={'type': 'text', 'media::image': ['abc']}) _keys = [val.key for val in row] assert 'image' in _keys assert 'media::image' not in _keys
def test_two2one(): result = Content(CONTENT_2).export(schema='1') assert 'translated' in result label = result['survey'][0]['label'] assert isinstance(label, list) assert len(label) == 1 assert label[0] == 'mylabel' tx1 = result['translations'][0] assert tx1 == 'eng (en)'
def test_rename_kuid_to_anchor(): cc = Content({ 'schema': '1', 'survey': [{ 'type': 'text', 'label': ['asdf'], '$kuid': 'asdfasdf' }], 'translations': [None], 'translated': ['label'], 'settings': {}, }) exp = cc.export(schema='2') assert '$anchor' in exp['survey'][0] # and rename it back to '$kuid' when saved as schema='1' exp2 = Content(exp).export(schema='1') assert '$kuid' in exp2['survey'][0]
def test_1_plus_colons(): content = Content({ # '1+xx' equivalent to 'xlsform' 'schema': '1+::', 'survey': [ {'type': 'text', 'name': 'book', 'label::English': 'The patient', 'label::French': 'Le patient', }, ], 'translated': [ 'label' ], 'settings': {}, }) result = content.export(schema='1') row0 = result['survey'][0] assert row0['label'] == ['The patient', 'Le patient']
def test_one2one(): result = Content(CONTENT_1).export(schema='1') assert 'translated' in result label = result['survey'][0]['label'] assert isinstance(label, list) assert len(label) == 1 tx1 = result['translations'][0] assert tx1 == None assert isinstance(result['choices'], list) assert len(result['choices']) > 0
def test_reverts_certain_names_on_export(): cc1 = BAREBONES_1.copy(translated=('media::image', )) cc2 = Content(cc1) row = Row(content=cc2, row={'type': 'text', 'media::image': ['abc']}) schema = '1' assert 'image' not in row.to_export(schema=schema) assert 'media::image' in row.to_export(schema=schema) schema = '2' assert 'image' in row.to_export(schema=schema) assert 'media::image' not in row.to_export(schema=schema)
def test_aliases(): cc = Content({ 'schema': '2', 'settings': {}, 'survey': [], 'translations': [{ 'name': 't1', 'code': 't1' }] }) assert cc.export(schema='2')['translations'][0] == { 'name': 't1', 'default': True, '$anchor': 't1' } cc = Content({ 'schema': '2', 'settings': {}, 'survey': [], 'translations': [{ 'name': 't1', 'uicode': 'en', 'code': 't1' }] }) assert cc.export(schema='2')['translations'][0] == { 'name': 't1', 'default': True, '$anchor': 't1', 'locale': 'en' }
def test_extract_metas_properly(): # just converts back and forth and back and forth # verifies settings.metas is correct result_2 = Content(CONTENT).export(schema='2') assert len(result_2['survey']) == 0 assert 'settings' in result_2 assert 'metas' in result_2['settings'] result_21 = Content(result_2).export(schema='1') assert len(result_21['survey']) == 2 assert 'metas' not in result_21['settings'] result_22 = Content(result_2).export(schema='2') result_221 = Content(result_22).export(schema='1') assert len(result_221['survey']) == 2 assert 'metas' not in result_221['settings'] result_2212 = Content(result_221).export(schema='2') assert len(result_2212['survey']) == 0 assert result_2212['settings']['metas'] == {'start': True, 'end': True}
def test_import_constraint_with_message_from_1(): result = Content({ **CONTENT_1, 'survey': [{ 'type': 'integer', 'name': 'age', '$anchor': 'age', 'label': ['label'], 'constraint': '${age} > 0 and ${age} < 120', 'constraint_message': ['that is not a valid age'] }] }).export(schema='2') row0 = result['survey'][0] constraint1 = row0['constraint'] assert constraint1 == { 'string': '${age} > 0 and ${age} < 120', 'message': { 'mytx': { 'string': 'that is not a valid age' } }, } result2 = Content(result).export(schema='1') row0 = result2['survey'][0] # {'constraint': '${age} > 0 and ${age} < 120', # 'constraint_message': ['that is not a valid age'], # 'label': ['label'], # 'name': 'age', # 'type': 'integer'} assert row0['constraint'] == '${age} > 0 and ${age} < 120' assert row0['constraint_message'][0] == 'that is not a valid age'
def test_one2two(): result = Content(CONTENT_1).export(schema='2') assert 'translated' not in result label = result['survey'][0]['label'] assert isinstance(label, dict) assert set(label.keys()) == set(['tx0']) assert isinstance(label['tx0'], dict) assert set(label['tx0'].keys()) == set(['string']) tx1 = result['translations'][0] assert isinstance(tx1, dict) assert tx1 == {'$anchor': 'tx0', 'name': '', 'default': True} assert isinstance(result['choices'], dict) assert len(result['choices'].keys()) > 0
def test_two2two(): result = Content(CONTENT_2).export(schema='2') assert 'translated' not in result label = result['survey'][0]['label'] assert isinstance(label, dict) assert set(label.keys()) == set(['tx0']) tx1 = result['translations'][0] assert isinstance(tx1, dict) assert tx1 == { '$anchor': 'tx0', 'name': 'eng', 'locale': 'en', 'default': True } row2 = result['survey'][1] assert 'name' in row2
def test_reverts_select_from_on_export(): cc1 = BAREBONES_1 cc2 = Content(cc1) row = Row(content=cc2, row={ 'type': 'select_one', 'select_from_list_name': 'xyz', }) schema = '1' assert 'select_from' not in row.to_export(schema=schema) assert 'select_from_list_name' in row.to_export(schema=schema) schema = '2' assert 'select_from' in row.to_export(schema=schema) assert 'select_from_list_name' not in row.to_export(schema=schema)
def test_reorder_translations(): cc = Content({ 'survey': [{ 'type': 'text', 'name': 'q1', 'label': { 'en': { 'string': 'en q1' }, 'fr': { 'string': 'fr q1' } } }, { 'type': 'text', 'name': 'q2', 'label': { 'en': { 'string': 'en q2' }, 'fr': { 'string': 'fr q2' } } }], 'translations': [{ 'name': 'eng', '$anchor': 'en', 'locale': 'en' }, { 'name': 'fr', '$anchor': 'fr', 'default': True, 'locale': 'fr' }], 'settings': {}, 'schema': '2', }) anchors_0 = [c.anchor for c in cc.txs] assert anchors_0 == ['en', 'fr'] cc.txs.reorder() anchors_1 = [c.anchor for c in cc.txs] assert anchors_1 == ['fr', 'en']
def test_additional(): cc = {'schema': '1::', 'settings': [{'default_language': None}], 'survey': [{'name': 'start', 'type': 'start'}, {'name': 'end', 'type': 'end'}, {'$kuid': 'ty7yd67', 'label': 'q1', 'required': False, 'type': 'text'}, {'$kuid': 'pm4jk80', 'label': 'q2', 'required': False, 'type': 'integer'}]} content = Content(cc) result = content.export(schema='1') assert len(result['translations']) == 1 assert result['translations'] == [None] content = Content(cc) result = content.export(schema='2') assert len(result['translations']) == 1 assert result['translations'] == [{'$anchor': 'tx0', 'default': True, 'name': ''}]
def test_one2two(): for cc in CONTENT_1S: content = Content(cc, exports_include_defaults=True) rr = content.export(schema='2') assert 'settings' in rr content = Content(cc, exports_include_defaults=False) rr = content.export(schema='2') assert 'settings' not in rr for cc in CONTENT_1S_WITH_SETTINGS: # don't delete valid settings content = Content(cc, exports_include_defaults=True) rr = content.export(schema='2') assert 'settings' in rr content = Content(cc, exports_include_defaults=False) rr = content.export(schema='2') assert 'settings' in rr
def content2tx(content): kk = Content(content) return (kk.txs._tuple[0], kk)