本文整理汇总了Python中onadata.libs.utils.export_tools.ExportBuilder类的典型用法代码示例。如果您正苦于以下问题:Python ExportBuilder类的具体用法?Python ExportBuilder怎么用?Python ExportBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ExportBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_generation_of_multi_selects_works
def test_generation_of_multi_selects_works(self):
survey = self._create_childrens_survey()
export_builder = ExportBuilder()
export_builder.set_survey(survey)
expected_select_multiples =\
{
'children':
{
'children/fav_colors':
[
'children/fav_colors/red', 'children/fav_colors/blue',
'children/fav_colors/pink'
],
'children/ice.creams':
[
'children/ice.creams/vanilla',
'children/ice.creams/strawberry',
'children/ice.creams/chocolate'
]
}
}
select_multiples = export_builder.select_multiples
self.assertTrue('children' in select_multiples)
self.assertTrue('children/fav_colors' in select_multiples['children'])
self.assertTrue('children/ice.creams' in select_multiples['children'])
self.assertEqual(
sorted(select_multiples['children']['children/fav_colors']),
sorted(
expected_select_multiples['children']['children/fav_colors']))
self.assertEqual(
sorted(select_multiples['children']['children/ice.creams']),
sorted(
expected_select_multiples['children']['children/ice.creams']))
示例2: test_split_select_multiples_works
def test_split_select_multiples_works(self):
select_multiples = {
"children/fav_colors": ["children/fav_colors/red", "children/fav_colors/blue", "children/fav_colors/pink"]
}
row = {"children/name": "Mike", "children/age": 5, "children/fav_colors": "red blue"}
new_row = ExportBuilder.split_select_multiples(row, select_multiples)
expected_row = {
"children/name": "Mike",
"children/age": 5,
"children/fav_colors": "red blue",
"children/fav_colors/red": True,
"children/fav_colors/blue": True,
"children/fav_colors/pink": False,
}
self.assertEqual(new_row, expected_row)
row = {"children/name": "Mike", "children/age": 5}
new_row = ExportBuilder.split_select_multiples(row, select_multiples)
expected_row = {
"children/name": "Mike",
"children/age": 5,
"children/fav_colors/red": None,
"children/fav_colors/blue": None,
"children/fav_colors/pink": None,
}
self.assertEqual(new_row, expected_row)
示例3: test_generation_of_multi_selects_works
def test_generation_of_multi_selects_works(self):
survey = self._create_childrens_survey()
export_builder = ExportBuilder()
export_builder.set_survey(survey)
expected_select_multiples = {
"children": {
"children/fav_colors": [
"children/fav_colors/red",
"children/fav_colors/blue",
"children/fav_colors/pink",
],
"children/ice.creams": [
"children/ice.creams/vanilla",
"children/ice.creams/strawberry",
"children/ice.creams/chocolate",
],
}
}
select_multiples = export_builder.select_multiples
self.assertTrue("children" in select_multiples)
self.assertTrue("children/fav_colors" in select_multiples["children"])
self.assertTrue("children/ice.creams" in select_multiples["children"])
self.assertEqual(
sorted(select_multiples["children"]["children/fav_colors"]),
sorted(expected_select_multiples["children"]["children/fav_colors"]),
)
self.assertEqual(
sorted(select_multiples["children"]["children/ice.creams"]),
sorted(expected_select_multiples["children"]["children/ice.creams"]),
)
示例4: test_child_record_parent_table_is_updated_when_sheet_is_renamed
def test_child_record_parent_table_is_updated_when_sheet_is_renamed(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_with_a_very_long_name.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
xls_file = NamedTemporaryFile(suffix='.xlsx')
filename = xls_file.name
export_builder.to_xls_export(filename, self.long_survey_data)
xls_file.seek(0)
wb = load_workbook(filename)
# get the children's sheet
ws1 = wb.get_sheet_by_name('childrens_survey_with_a_very_l1')
# parent_table is in cell K2
parent_table_name = ws1.cell('K2').value
expected_parent_table_name = 'childrens_survey_with_a_very_lo'
self.assertEqual(parent_table_name, expected_parent_table_name)
# get cartoons sheet
ws2 = wb.get_sheet_by_name('childrens_survey_with_a_very_l2')
parent_table_name = ws2.cell('G2').value
expected_parent_table_name = 'childrens_survey_with_a_very_l1'
self.assertEqual(parent_table_name, expected_parent_table_name)
xls_file.close()
示例5: test_delimiter_replacement_works_existing_fields
def test_delimiter_replacement_works_existing_fields(self):
survey = self._create_childrens_survey()
export_builder = ExportBuilder()
export_builder.GROUP_DELIMITER = "."
export_builder.set_survey(survey)
expected_sections = [{"name": "children", "elements": [{"title": "children.name", "xpath": "children/name"}]}]
children_section = export_builder.section_by_name("children")
self.assertEqual(children_section["elements"][0]["title"], expected_sections[0]["elements"][0]["title"])
示例6: test_type_conversion
def test_type_conversion(self):
submission_1 = {
"_id": 579827,
"geolocation": "-1.2625482 36.7924794 0.0 21.0",
"_bamboo_dataset_id": "",
"meta/instanceID": "uuid:2a8129f5-3091-44e1-a579-bed2b07a12cf",
"name": "Smith",
"formhub/uuid": "633ec390e024411ba5ce634db7807e62",
"_submission_time": "2013-07-03T08:25:30",
"age": "107",
"_uuid": "2a8129f5-3091-44e1-a579-bed2b07a12cf",
"when": "2013-07-03",
"amount": "250.0",
"_geolocation": [
"-1.2625482",
"36.7924794"
],
"_xform_id_string": "test_data_types",
"_userform_id": "larryweya_test_data_types",
"_status": "submitted_via_web",
"precisely": "2013-07-03T15:24:00.000+03",
"really": "15:24:00.000+03"
}
submission_2 = {
"_id": 579828,
"_submission_time": "2013-07-03T08:26:10",
"_uuid": "5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
"_bamboo_dataset_id": "",
"_xform_id_string": "test_data_types",
"_userform_id": "larryweya_test_data_types",
"_status": "submitted_via_web",
"meta/instanceID": "uuid:5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
"formhub/uuid": "633ec390e024411ba5ce634db7807e62",
"amount": "",
}
survey = create_survey_from_xls(viewer_fixture_path(
'test_data_types/test_data_types.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
# format submission 1 for export
survey_name = survey.name
indices = {survey_name: 0}
data = dict_to_joined_export(submission_1, 1, indices, survey_name)
new_row = export_builder.pre_process_row(data[survey_name],
export_builder.sections[0])
self.assertIsInstance(new_row['age'], int)
self.assertIsInstance(new_row['when'], datetime.date)
self.assertIsInstance(new_row['amount'], float)
# check missing values dont break and empty values return blank strings
indices = {survey_name: 0}
data = dict_to_joined_export(submission_2, 1, indices, survey_name)
new_row = export_builder.pre_process_row(data[survey_name],
export_builder.sections[0])
self.assertIsInstance(new_row['amount'], basestring)
self.assertEqual(new_row['amount'], '')
示例7: test_xls_convert_dates_before_1900
def test_xls_convert_dates_before_1900(self):
survey = create_survey_from_xls(viewer_fixture_path("test_data_types/test_data_types.xls"))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
data = [{"name": "Abe", "when": "1899-07-03"}]
# create export file
temp_xls_file = NamedTemporaryFile(suffix=".xlsx")
export_builder.to_xls_export(temp_xls_file.name, data)
temp_xls_file.close()
示例8: test_delimiter_replacement_works_for_generated_gps_fields
def test_delimiter_replacement_works_for_generated_gps_fields(self):
survey = self._create_childrens_survey()
export_builder = ExportBuilder()
export_builder.GROUP_DELIMITER = "."
export_builder.set_survey(survey)
expected_section = {
"name": "childrens_survey",
"elements": [{"title": "geo._geolocation_latitude", "xpath": "geo/_geolocation_latitude"}],
}
main_section = export_builder.section_by_name("childrens_survey")
match = filter(lambda x: (expected_section["elements"][0]["xpath"] == x["xpath"]), main_section["elements"])[0]
self.assertEqual(expected_section["elements"][0]["title"], match["title"])
示例9: test_zipped_csv_export_works_with_unicode
def test_zipped_csv_export_works_with_unicode(self):
"""
cvs writer doesnt handle unicode we we have to encode to ascii
"""
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_zip_file = NamedTemporaryFile(suffix='.zip')
export_builder.to_zipped_csv(temp_zip_file.name, self.data_utf8)
temp_zip_file.seek(0)
temp_dir = tempfile.mkdtemp()
zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
zip_file.extractall(temp_dir)
zip_file.close()
temp_zip_file.close()
# check that the children's file (which has the unicode header) exists
self.assertTrue(
os.path.exists(
os.path.join(temp_dir, "children.info.csv")))
# check file's contents
with open(os.path.join(temp_dir, "children.info.csv")) as csv_file:
reader = csv.reader(csv_file)
expected_headers = ['children.info/name.first',
'children.info/age',
'children.info/fav_colors',
u'children.info/fav_colors/red\u2019s',
u'children.info/fav_colors/blue\u2019s',
u'children.info/fav_colors/pink\u2019s',
'children.info/ice_creams',
'children.info/ice_creams/vanilla',
'children.info/ice_creams/strawberry',
'children.info/ice_creams/chocolate', '_id',
'_uuid', '_submission_time', '_index',
'_parent_table_name', '_parent_index',
u'_tags', '_notes', '_version',
'_duration', '_submitted_by']
rows = [row for row in reader]
actual_headers = [h.decode('utf-8') for h in rows[0]]
self.assertEqual(sorted(actual_headers), sorted(expected_headers))
data = dict(zip(rows[0], rows[1]))
self.assertEqual(
data[u'children.info/fav_colors/red\u2019s'.encode('utf-8')],
'True')
self.assertEqual(
data[u'children.info/fav_colors/blue\u2019s'.encode('utf-8')],
'True')
self.assertEqual(
data[u'children.info/fav_colors/pink\u2019s'.encode('utf-8')],
'False')
# check that red and blue are set to true
shutil.rmtree(temp_dir)
示例10: test_generation_of_mongo_encoded_fields_works
def test_generation_of_mongo_encoded_fields_works(self):
survey = self._create_childrens_survey()
export_builder = ExportBuilder()
export_builder.set_survey(survey)
expected_encoded_fields = {
"childrens_survey": {
"tel/tel.office": "tel/{0}".format(_encode_for_mongo("tel.office")),
"tel/tel.mobile": "tel/{0}".format(_encode_for_mongo("tel.mobile")),
}
}
encoded_fields = export_builder.encoded_fields
self.assertTrue("childrens_survey" in encoded_fields)
self.assertEqual(encoded_fields["childrens_survey"], expected_encoded_fields["childrens_survey"])
示例11: test_delimiter_replacement_works_generated_multi_select_fields
def test_delimiter_replacement_works_generated_multi_select_fields(self):
survey = self._create_childrens_survey()
export_builder = ExportBuilder()
export_builder.GROUP_DELIMITER = "."
export_builder.set_survey(survey)
expected_section = {
"name": "children",
"elements": [{"title": "children.fav_colors.red", "xpath": "children/fav_colors/red"}],
}
childrens_section = export_builder.section_by_name("children")
match = filter(lambda x: expected_section["elements"][0]["xpath"] == x["xpath"], childrens_section["elements"])[
0
]
self.assertEqual(expected_section["elements"][0]["title"], match["title"])
示例12: test_to_sav_export
def test_to_sav_export(self):
survey = self._create_childrens_survey()
export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_zip_file = NamedTemporaryFile(suffix='.zip')
filename = temp_zip_file.name
export_builder.to_zipped_sav(filename, self.data)
temp_zip_file.seek(0)
temp_dir = tempfile.mkdtemp()
zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
zip_file.extractall(temp_dir)
zip_file.close()
temp_zip_file.close()
# generate data to compare with
index = 1
indices = {}
survey_name = survey.name
outputs = []
for d in self.data:
outputs.append(
dict_to_joined_export(d, index, indices, survey_name))
index += 1
# check that each file exists
self.assertTrue(
os.path.exists(
os.path.join(temp_dir, "{0}.sav".format(survey.name))))
def _test_sav_file(section):
with SavReader(
os.path.join(
temp_dir, "{0}.sav".format(section)),
returnHeader=True) as reader:
header = next(reader)
rows = [r for r in reader]
# open comparison file
with SavReader(_logger_fixture_path(
'spss', "{0}.sav".format(section)),
returnHeader=True) as fixture_reader:
fixture_header = next(fixture_reader)
self.assertEqual(header, fixture_header)
expected_rows = [r for r in fixture_reader]
self.assertEqual(rows, expected_rows)
for section in export_builder.sections:
section_name = section['name'].replace('/', '_')
_test_sav_file(section_name)
示例13: test_xls_convert_dates_before_1900
def test_xls_convert_dates_before_1900(self):
survey = create_survey_from_xls(viewer_fixture_path(
'test_data_types/test_data_types.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
data = [
{
'name': 'Abe',
'when': '1899-07-03',
}
]
# create export file
temp_xls_file = NamedTemporaryFile(suffix='.xlsx')
export_builder.to_xls_export(temp_xls_file.name, data)
temp_xls_file.close()
示例14: test_xls_export_works_with_unicode
def test_xls_export_works_with_unicode(self):
survey = create_survey_from_xls(_logger_fixture_path("childrens_survey_unicode.xls"))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_xls_file = NamedTemporaryFile(suffix=".xlsx")
export_builder.to_xls_export(temp_xls_file.name, self.data_utf8)
temp_xls_file.seek(0)
# check that values for red\u2019s and blue\u2019s are set to true
wb = load_workbook(temp_xls_file.name)
children_sheet = wb.get_sheet_by_name("children.info")
data = dict([(r[0].value, r[1].value) for r in children_sheet.columns])
self.assertTrue(data[u"children.info/fav_colors/red\u2019s"])
self.assertTrue(data[u"children.info/fav_colors/blue\u2019s"])
self.assertFalse(data[u"children.info/fav_colors/pink\u2019s"])
temp_xls_file.close()
示例15: test_split_select_multiples_works_when_data_is_blank
def test_split_select_multiples_works_when_data_is_blank(self):
select_multiples =\
{
'children/fav_colors': [
'children/fav_colors/red', 'children/fav_colors/blue',
'children/fav_colors/pink']
}
row = \
{
'children/name': 'Mike',
'children/age': 5,
'children/fav_colors': ''
}
new_row = ExportBuilder.split_select_multiples(
row, select_multiples)
expected_row = \
{
'children/name': 'Mike',
'children/age': 5,
'children/fav_colors': '',
'children/fav_colors/red': None,
'children/fav_colors/blue': None,
'children/fav_colors/pink': None
}
self.assertEqual(new_row, expected_row)