def test_should_handle_empty_worksheet(self, mock_csv): ws = Worksheet() result = worksheet_to_csv(ws, encoding='ascii') mock_writer = mock_csv.writer.return_value self.assertFalse(mock_writer.writerow.called)
def test_raises_on_attempting_to_encode_nonwestern_chars_to_excel_format( self): some_kanji = u'\u30bc\u30ed\u30a6\u30a3\u30f3\u30b0' ws = Worksheet() ws.a1.value = some_kanji self.assertRaises( UnicodeEncodeError, lambda: worksheet_to_csv(ws, encoding='windows-1252'))
def test_raises_on_attempting_to_encode_nonwestern_chars_to_excel_format(self): some_kanji = u'\u30bc\u30ed\u30a6\u30a3\u30f3\u30b0' ws = Worksheet() ws.a1.value = some_kanji self.assertRaises( UnicodeEncodeError, lambda : worksheet_to_csv(ws, encoding='windows-1252') )
def test_can_convert_unicode_to_utf8(self): some_kanji = u'\u30bc\u30ed\u30a6\u30a3\u30f3\u30b0' ws = Worksheet() ws.a1.value = some_kanji result = worksheet_to_csv(ws, encoding='utf-8') self.assertEquals( result.split('\r\n')[:-1], [ ws.a1.value.encode('utf-8'), ])
def test_should_include_everything_from_A1_outwards(self, mock_csv): ws = Worksheet() ws.B3.value = 5 result = worksheet_to_csv(ws, encoding='windows-1252') mock_writer = mock_csv.writer.return_value self.assertEquals(mock_writer.writerow.call_args_list, [((["", ""], ), {}), ((["", ""], ), {}), ((["", 5], ), {})])
def test_handles_cell_values_set_to_non_ascii_bytes(self): a_large_number = 25700000000.0 ws = Worksheet() ws.a1.value = a_large_number result = worksheet_to_csv(ws, encoding='windows-1252') stream = StringIO() stream.write(result) stream.seek(0) decoder = codecs.getreader('windows-1252')(stream) decoder.read()
def test_can_convert_unicode_to_utf8(self): some_kanji = u'\u30bc\u30ed\u30a6\u30a3\u30f3\u30b0' ws = Worksheet() ws.a1.value = some_kanji result = worksheet_to_csv(ws, encoding='utf-8') self.assertEquals( result.split('\r\n')[:-1], [ ws.a1.value.encode('utf-8'), ] )
def test_should_convert_unicode_to_windows_1252(self): ws = Worksheet() ws.a1.value = u'Sacr\xe9 bleu!' ws.a2.value = u'\xa312.95' ws.a3.value = u'\u20ac9.99' result = worksheet_to_csv(ws, encoding='windows-1252') self.assertEquals( result.split('\r\n')[:-1], [ ws.a1.value.encode('windows-1252'), ws.a2.value.encode('windows-1252'), ws.a3.value.encode('windows-1252'), ])
def test_should_process_contents_in_raster_order(self, mock_csv): ws = Worksheet() ws.A1.value = 1 ws.B1.value = 2 ws.C1.value = 3 ws.A2.value = 4 ws.B2.value = 5 ws.C2.value = 6 result = worksheet_to_csv(ws, encoding='windows-1252') mock_writer = mock_csv.writer.return_value self.assertEquals(mock_writer.writerow.call_args_list, [(([1, 2, 3], ), {}), (([4, 5, 6], ), {})])
def test_should_convert_unicode_to_windows_1252(self): ws = Worksheet() ws.a1.value = u'Sacr\xe9 bleu!' ws.a2.value = u'\xa312.95' ws.a3.value = u'\u20ac9.99' result = worksheet_to_csv(ws, encoding='windows-1252') self.assertEquals( result.split('\r\n')[:-1], [ ws.a1.value.encode('windows-1252'), ws.a2.value.encode('windows-1252'), ws.a3.value.encode('windows-1252'), ] )
def test_should_use_stringio_and_return_result(self, mock_stringio_class, mock_csv): mock_stringio_object = mock_stringio_class.return_value def check_getvalue_has_been_called(): self.assertCalledOnce(mock_stringio_object.getvalue) mock_stringio_object.close.side_effect = check_getvalue_has_been_called ws = Worksheet() ws.A1.value = "test data" result = worksheet_to_csv(ws, encoding='ascii') self.assertCalledOnce(mock_csv.writer, mock_stringio_object) self.assertEquals(result, mock_stringio_object.getvalue.return_value) self.assertCalledOnce(mock_stringio_object.close)
def test_should_include_everything_from_A1_outwards(self, mock_csv): ws = Worksheet() ws.B3.value = 5 result = worksheet_to_csv(ws, encoding='windows-1252') mock_writer = mock_csv.writer.return_value self.assertEquals( mock_writer.writerow.call_args_list, [ ((["", ""],), {}), ((["", ""],), {}), ((["", 5],), {}) ] )
def test_should_process_contents_in_raster_order(self, mock_csv): ws = Worksheet() ws.A1.value = 1 ws.B1.value = 2 ws.C1.value = 3 ws.A2.value = 4 ws.B2.value = 5 ws.C2.value = 6 result = worksheet_to_csv(ws, encoding='windows-1252') mock_writer = mock_csv.writer.return_value self.assertEquals( mock_writer.writerow.call_args_list, [ (([1, 2, 3],), {}), (([4, 5, 6],), {}) ] )
def export_csv(request, sheet, csv_format): if csv_format == 'unicode': encoding = 'utf-8' else: encoding = 'windows-1252' try: content = worksheet_to_csv( sheet.unjsonify_worksheet(), encoding=encoding ) except UnicodeEncodeError: return render_to_response( 'export_csv_error.html', {'sheet': sheet}, context_instance=RequestContext(request) ) response = HttpResponse(content, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename=%s.csv' % (sheet.name,) response['Content-Length'] = len(content) return response