def test_writing_to_multiple_sheets_in_same_book(context): "Writer should write to multiple sheets in the same book" # Given a backend backend = XL() # And a writer writer = Writer(backend=backend, output=context.tmpfile) # When we write data to Awesome Sheet1 backend.use_sheet('Awesome Sheet1') data = [ [("Country", "Argentina"), ("Revenue", 14500025)] ] writer.write(data) # When we write data to Awesome Sheet2 backend.use_sheet('Awesome Sheet2') data = [ OrderedDict([("Country", "Puerto Rico"), ("Revenue", 2340982)]), OrderedDict([("Country", "Colombia"), ("Revenue", 23409822)]), OrderedDict([("Country", "Brazil"), ("Revenue", 19982793)]), ] writer.write(data) writer.save() # Then the written data should be under Awesome Sheet assert_first_sheets_are_the_same(context.tmpfile.name, LOCAL_FILE('awesome_sheet2.xls'))
def test_write_row_passing_no_style(): ("XL backend.write_row should write row using appropriate " "index, value and style") row = Mock() xl_backend = XL() xl_backend.write_row(row, ['Cell One', 'Cell Two']) row.write.assert_has_calls([ call(0, 'Cell One', default_style), call(1, 'Cell Two', default_style), ])
def test_get_sheets(): ("XL backend.get_sheets should return" " number of worksheets in the workbook") # Given a mocked workbook and XL backend workbook = Mock() xl_backend = XL(workbook=workbook) # When I get sheets sheets = xl_backend.get_sheets() # Then sheets should equal the sheets form the workbook worksheets = workbook._Workbook__worksheets sheets.should.equal(worksheets)
def test_get_header_style(): ("XL backend .get_header_style() returns " "bold and horizontal right alignment") xl_backend = XL() style = xl_backend.get_header_style() expect(style).to.be.a('xlwt.XFStyle') expect(style.alignment).to.be.an(Alignment) expect(style.alignment.horz).to.equal(Alignment.HORZ_RIGHT) expect(style.borders).to.be.a('xlwt.Borders') expect(style.font).to.be.a('xlwt.Font') expect(style.font.bold).to.be.true
def test_write_row_passing_style(): ("XL backend.write_row should write row using appropriate " "index, value and style") # Given a row row = Mock() # When .write_row is called xl_backend = XL() xl_backend.write_row(row, ['Cell One', 'Cell Two'], default_style) # Then row.write is called appropriately row.write.assert_has_calls([ call(0, 'Cell One', default_style), call(1, 'Cell Two', default_style), ])
def test_save(): "XL backend.save should save th workbook and close the output" # Given an XL backend with mocked workbook and a mocked output workbook = Mock() output = Mock() xl_backend = XL(workbook=workbook) # When I save the backend to output xl_backend.save(output) # Then xl_backend.workbook.save should have been called once with output workbook.save.assert_called_once_with(output) # And output.close should hae been called once output.close.assert_called_once_with()
def benchmark_excellent(): from collections import OrderedDict from excellent import Writer, XL output = open("benchmark_excellent.xls", "wb") sheet_manager = XL() excel = Writer(sheet_manager, output) sheet_manager.use_sheet("Sheet1") excel.write((OrderedDict(((str(column), value) for column in range(COLUMNS))) for row, value in zip(range(ROWS), VALUES))) excel.save()
def test_writing_with_none_format_string(): "Writer should not use a format string if None is passed" STYLE_CACHE.clear() backend = XL() output = Mock() mock_row = Mock() backend.write_row( mock_row, [['Chuck Norris', 'Power']], format_string=None, ) STYLE_CACHE.should.have.length_of(1) active_style = STYLE_CACHE.values()[0] active_style.num_format_str.should.equal("General")
def test_write_data_to_xl_specifying_sheet_name(context): "Writer should write data to the sheet specified" # Given a backend backend = XL() # And a writer writer = Writer(backend=backend, output=context.tmpfile) # When we use sheet `Awesome Sheet1` backend.use_sheet('Awesome Sheet1') # And we write some data data = [ [("Country", "Argentina"), ("Revenue", 14500025)] ] writer.write(data) writer.save() # Then the written data should be under Awesome Sheet assert_first_sheets_are_the_same(context.tmpfile.name, LOCAL_FILE('awesome_sheet1.xls'))
def benchmark_excellent(): from collections import OrderedDict from excellent import Writer, XL output = open("benchmark_excellent.xls", "wb") sheet_manager = XL() excel = Writer(sheet_manager, output) sheet_manager.use_sheet("Sheet1") excel.write(( OrderedDict(( (str(column), value) for column in xrange(COLUMNS) )) for row, value in izip(xrange(ROWS), VALUES) )) excel.save()
def test_writing_multiple_times_to_same_sheet_and_multiple_sheets(context): "Writer can switch between sheets and write multiple times to same sheet" # Given a backend backend = XL() # And a writer writer = Writer(backend=backend, output=context.tmpfile) # And then switch to Awesome Sheet1 and this just adds this sheet backend.use_sheet('Awesome Sheet1') # When we write data to Awesome Sheet2 backend.use_sheet('Awesome Sheet2') data = [ OrderedDict([("Country", "Puerto Rico"), ("Revenue", 2340982)]), ] writer.write(data) backend.use_sheet('Awesome Sheet1') data = [ OrderedDict([("Country", "Argentina"), ("Revenue", 14500025)]) ] writer.write(data) # And switch back to Awesome Sheet 2 to write more data backend.use_sheet('Awesome Sheet2') data = [ OrderedDict([("Country", "Colombia"), ("Revenue", 23409822)]), OrderedDict([("Country", "Brazil"), ("Revenue", 19982793)]), ] writer.write(data) writer.save() # Then the written data to Awesome Sheets 1 and 2 should match assert_first_sheets_are_the_same(context.tmpfile.name, LOCAL_FILE('awesome_sheet2.xls'))
def test_xl_style_cache_works(): ("The XL backend should cache styles that are the same instead of creating " "a new style each time") STYLE_CACHE.clear() backend = XL() output = Mock() mock_row = Mock() backend.write_row( mock_row, [['Chuck Norris', 'Power']], output=output, bold=True, ) STYLE_CACHE.should.have.length_of(1) bold_style = STYLE_CACHE.values()[0] backend.write_row( mock_row, [['Name', 'Some other guy']], output=output, ) STYLE_CACHE.should.have.length_of(2) non_bold_style = [style for style in STYLE_CACHE.values() if style != bold_style][0] backend.write_row( mock_row, [['Name', 'Another person']], output=output, bold=True, ) mock_row.write.assert_has_calls([ call(0, ['Chuck Norris', 'Power'], bold_style), call(0, ['Name', 'Some other guy'], non_bold_style), call(0, ['Name', 'Another person'], bold_style), ]) STYLE_CACHE.should.have.length_of(2)