<s> import StringIO <EOL> from nose . tools import assert_equal <EOL> from openpyxl import load_workbook <EOL> from desktop . lib . export_csvxls import create_generator , make_response <EOL> def content_generator ( header , data ) : <EOL> yield header , data <EOL> def test_export_csv ( ) : <EOL> headers = [ "<STR_LIT:x>" , "<STR_LIT:y>" ] <EOL> data = [ [ "<STR_LIT:1>" , "<STR_LIT:2>" ] , [ "<STR_LIT:3>" , "<STR_LIT:4>" ] , [ "<STR_LIT>" , "<STR_LIT>" ] , [ None , None ] ] <EOL> generator = create_generator ( content_generator ( headers , data ) , "<STR_LIT>" ) <EOL> response = make_response ( generator , "<STR_LIT>" , "<STR_LIT:foo>" ) <EOL> assert_equal ( "<STR_LIT>" , response [ "<STR_LIT>" ] ) <EOL> content = '<STR_LIT>' . join ( response . streaming_content ) <EOL> assert_equal ( '<STR_LIT>' , content ) <EOL> assert_equal ( "<STR_LIT>" , response [ "<STR_LIT>" ] ) <EOL> def test_export_xls ( ) : <EOL> headers = [ "<STR_LIT:x>" , "<STR_LIT:y>" ] <EOL> data = [ [ "<STR_LIT:1>" , "<STR_LIT:2>" ] , [ "<STR_LIT:3>" , "<STR_LIT:4>" ] , [ "<STR_LIT>" , "<STR_LIT>" ] , [ None , None ] ] <EOL> sheet = [ headers ] + data <EOL> generator = create_generator ( content_generator ( headers , data ) , "<STR_LIT>" ) <EOL> response = make_response ( generator , "<STR_LIT>" , "<STR_LIT:foo>" ) <EOL> assert_equal ( "<STR_LIT>" , response [ "<STR_LIT>" ] ) <EOL> expected_data = [ [ cell is not None and cell or "<STR_LIT>" for cell in row ] for row in sheet ] <EOL> sheet_data = _read_xls_sheet_data ( response ) <EOL> assert_equal ( expected_data , sheet_data ) <EOL> assert_equal ( "<STR_LIT>" , response [ "<STR_LIT>" ] ) <EOL> def _read_xls_sheet_data ( response ) : <EOL> content = '<STR_LIT>' . join ( response . streaming_content ) <EOL> data = StringIO . StringIO ( ) <EOL> data . write ( content ) <EOL> wb = load_workbook ( filename = data , read_only = True ) <EOL> ws = wb . active <EOL> return [ [ cell . value if cell else cell for cell in row ] for row in ws . rows ] </s>
