示例#1
0
    def test_writing_excel(self):
        """ This is a pretty crap test. It at least exercises the code.
            If anyone wants to go through the brain damage of actually building
            an 'expected' xlsx output and comparing it
            (see https://github.com/jmcnamara/XlsxWriter/blob/master/xlsxwriter/test/helperfunctions.py for reference)
            , by all means submit a pull request!
        """
        res = QueryResult(
            SimpleQueryFactory(
                sql='select 1 as "a", 2 as ""',
                title='\\/*[]:?this title is longer than 32 characters').sql,
            connections[CONN])

        res.execute_query()
        res.process()

        d = datetime.now()
        d = timezone.make_aware(d, timezone.get_current_timezone())

        res._data = [[1, None], [u"Jenét", d]]

        res = ExcelExporter(
            query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)
def _build_zip_excel(queries):
    temp = tempfile.TemporaryFile()
    zip_file = ZipFile(temp, 'w')
    for r in queries:
        zip_file.writestr('%s.xlsx' % r.title,
                          ExcelExporter(r).get_output() or "Error!")
    zip_file.close()
    ret = FileWrapper(temp)
    ret.size = temp.tell()
    temp.seek(0)
    return ret
def _package_excel(queries):
    ret = {}
    is_one = len(queries) == 1
    name_root = lambda n: "attachment; filename=%s" % n
    ret["content_type"] = (is_one
                           and 'application/vnd.ms-excel') or 'application/zip'
    ret["filename"] = (is_one and name_root(
        '%s.xlsx' % queries[0].title.replace(',', ''))) or name_root(
            "Report_%s.zip" % date.today())
    ret["data"] = (is_one and ExcelExporter(
        queries[0]).get_output()) or _build_zip_excel(queries)
    ret["length"] = (is_one and len(ret["data"]) or ret["data"].size)
    return ret
    def test_writing_dict_fields(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""',
                                             title='\\/*[]:?this title is longer than 32 characters').sql, connections[CONN])

        res.execute_query()
        res.process()

        res._data = [[1, ['foo', 'bar']], [2, {'foo': 'bar'}]]

        res = ExcelExporter(query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)
    def test_writing_excel_with_invalid_characters_in_worksheet_name(self):
        res = QueryResult(
            SimpleQueryFactory(
                sql='select 1 as "a", 2 as ""',
                title='[]:*?/\\this title is longer than 32 characters').sql,
            connections[CONN])
        res.execute_query()
        res.process()

        d = datetime.now()
        d = timezone.make_aware(d, timezone.get_current_timezone())

        res._data = [[1, None], [u"Jenét", d]]

        res = ExcelExporter(
            query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)