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'] 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)
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' ] 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)
def test_zipped_csv_export_works(self): survey = self._create_childrens_survey() 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) 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}.csv".format(survey.name)))) with open(os.path.join(temp_dir, "{0}.csv".format(survey.name))) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open(_logger_fixture_path( 'csvs', 'childrens_survey.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) self.assertTrue(os.path.exists(os.path.join(temp_dir, "children.csv"))) with open(os.path.join(temp_dir, "children.csv")) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open(_logger_fixture_path('csvs', 'children.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) self.assertTrue( os.path.exists(os.path.join(temp_dir, "children_cartoons.csv"))) with open(os.path.join(temp_dir, "children_cartoons.csv")) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open(_logger_fixture_path( 'csvs', 'children_cartoons.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) self.assertTrue( os.path.exists( os.path.join(temp_dir, "children_cartoons_characters.csv"))) with open(os.path.join( temp_dir, "children_cartoons_characters.csv")) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open( _logger_fixture_path( 'csvs', 'children_cartoons_characters.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) shutil.rmtree(temp_dir)
def test_zipped_csv_export_works(self): survey = self._create_childrens_survey() 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) 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}.csv".format(survey.name)))) with open( os.path.join( temp_dir, "{0}.csv".format(survey.name))) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open(_logger_fixture_path( 'csvs', 'childrens_survey.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) self.assertTrue( os.path.exists( os.path.join(temp_dir, "children.csv"))) with open(os.path.join(temp_dir, "children.csv")) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open(_logger_fixture_path( 'csvs', 'children.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) self.assertTrue( os.path.exists( os.path.join(temp_dir, "children_cartoons.csv"))) with open(os.path.join(temp_dir, "children_cartoons.csv")) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open(_logger_fixture_path( 'csvs', 'children_cartoons.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) self.assertTrue( os.path.exists( os.path.join(temp_dir, "children_cartoons_characters.csv"))) with open(os.path.join( temp_dir, "children_cartoons_characters.csv")) as csv_file: reader = csv.reader(csv_file) rows = [r for r in reader] # open comparison file with open(_logger_fixture_path( 'csvs', 'children_cartoons_characters.csv')) as fixture_csv: fixture_reader = csv.reader(fixture_csv) expected_rows = [r for r in fixture_reader] self.assertEqual(rows, expected_rows) shutil.rmtree(temp_dir)