# coding: utf-8
from cStringIO import StringIO
from codecs import BOM_UTF8
from couchexport.writers import ZippedExportWriter, CsvFileWriter
from django.test import SimpleTestCase
from mock import patch, Mock


class ZippedExportWriterTests(SimpleTestCase):

    def setUp(self):
        self.zip_file_patch = patch('zipfile.ZipFile')
        self.MockZipFile = self.zip_file_patch.start()

        self.path_mock = Mock()
        self.path_mock.get_path.return_value = 'tmp'

        self.writer = ZippedExportWriter()
        self.writer.tables = [self.path_mock]
        self.writer.file = Mock()

    def tearDown(self):
        self.zip_file_patch.stop()
        del self.writer

    def test_zipped_export_writer_unicode(self):
        mock_zip_file = self.MockZipFile.return_value
        self.writer.table_names = {0: u'ひらがな'}
        self.writer._write_final_result()
        mock_zip_file.write.assert_called_with('tmp', 'ひらがな.csv')

    def test_zipped_export_writer_utf8(self):
        mock_zip_file = self.MockZipFile.return_value
        self.writer.table_names = {0: '\xe3\x81\xb2\xe3\x82\x89\xe3\x81\x8c\xe3\x81\xaa'}
        self.writer._write_final_result()
        mock_zip_file.write.assert_called_with('tmp', 'ひらがな.csv')


class CsvFileWriterTests(SimpleTestCase):

    def setUp(self):
        self.csv_file = StringIO()
        self.fdopen_patch = patch('os.fdopen')
        fdopen_mock = self.fdopen_patch.start()
        fdopen_mock.return_value = self.csv_file
        self.writer = CsvFileWriter()

    def tearDown(self):
        self.writer.close()
        self.fdopen_patch.stop()

    def test_csv_file_writer_bom(self):
        """
        CsvFileWriter should prepend a byte-order mark to the start of the CSV file for Excel
        """
        headers = ['ham', 'spam', 'eggs']
        self.writer.open('Spam')
        self.writer.write_row(headers)

        self.csv_file.seek(0)
        file_start = self.csv_file.read(6)
        self.assertEqual(file_start, BOM_UTF8 + 'ham')

@@ -13,39 +13,8 @@
 f-8%0A
-from cStringIO import StringIO%0A
 from
@@ -1253,333 +1253,8 @@
 ):%0A%0A
-    def setUp(self):%0A        self.csv_file = StringIO()%0A        self.fdopen_patch = patch('os.fdopen')%0A        fdopen_mock = self.fdopen_patch.start()%0A        fdopen_mock.return_value = self.csv_file%0A        self.writer = CsvFileWriter()%0A%0A    def tearDown(self):%0A        self.writer.close()%0A        self.fdopen_patch.stop()%0A%0A
     
@@ -1281,32 +1281,32 @@
 iter_bom(self):%0A
+
         %22%22%22%0A    
@@ -1399,32 +1399,65 @@
 cel%0A        %22%22%22%0A
+        writer = CsvFileWriter()%0A
         headers 
@@ -1486,29 +1486,24 @@
 s'%5D%0A        
-self.
 writer.open(
@@ -1514,29 +1514,24 @@
 m')%0A        
-self.
 writer.write
@@ -1544,25 +1544,24 @@
 eaders)%0A
-%0A
         
 self.csv
@@ -1556,28 +1556,22 @@
     
-self.csv_file.seek(0
+writer.finish(
 )%0A  
@@ -1593,21 +1593,25 @@
 t = 
-self.csv
+writer.get
 _file
+()
 .rea

simplify test