コード例 #1
0
    def test_stream_queryset_as_xlsx(self):
        template = gen_xlsx_template(with_header=True)

        cells = ([1, 'bo€>é', 1.1], [2, 'oki', 1.2])
        qs = [cells[i % 2] for i in range(27)]

        stream = streaming.stream_queryset_as_xlsx(qs, xlsx_template=template, batch_size=10)

        f = tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False)
        for chunk in stream:
            f.write(chunk)
        f.close()

        new_wb = openpyxl.load_workbook(filename=f.name)

        # check header:
        self.assertEqual(new_wb.active.cell(row=1, column=1).value, 'Id')
        self.assertEqual(new_wb.active.cell(row=1, column=2).value, 'Description')
        self.assertEqual(new_wb.active.cell(row=1, column=3).value, 'Date')

        self.assertEqual(new_wb.active.cell(row=2, column=1).value, 1)
        self.assertEqual(new_wb.active.cell(row=3, column=1).value, 2)
        self.assertEqual(new_wb.active.cell(row=28, column=1).value, 1)

        self.assertEqual(new_wb.active.cell(row=2, column=2).value, 'bo€>é')
        self.assertEqual(new_wb.active.cell(row=3, column=2).value, 'oki')
        self.assertEqual(new_wb.active.cell(row=28, column=2).value, 'bo€>é')

        self.assertEqual(new_wb.active.cell(row=2, column=3).value, datetime.datetime(1900, 1, 1, 2, 24))
        self.assertEqual(new_wb.active.cell(row=3, column=3).value, datetime.datetime(1900, 1, 1, 4, 48))
        self.assertEqual(new_wb.active.cell(row=28, column=3).value, datetime.datetime(1900, 1, 1, 2, 24))
        os.remove(f.name)
コード例 #2
0
    def test_stream_queryset_as_xlsx(self):
        template = gen_xlsx_template(with_header=True)

        cells = ([1, 'bo€>é', 1.1], [2, 'oki', 1.2])
        qs = [cells[i % 2] for i in range(27)]

        stream = streaming.stream_queryset_as_xlsx(qs, xlsx_template=template, batch_size=10)

        f = tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False)
        for chunk in stream:
            f.write(chunk)
        f.close()

        new_wb = openpyxl.load_workbook(filename=f.name)

        # check header:
        self.assertEqual(new_wb.active.cell(row=1, column=1).value, 'Id')
        self.assertEqual(new_wb.active.cell(row=1, column=2).value, 'Description')
        self.assertEqual(new_wb.active.cell(row=1, column=3).value, 'Date')

        self.assertEqual(new_wb.active.cell(row=2, column=1).value, 1)
        self.assertEqual(new_wb.active.cell(row=3, column=1).value, 2)
        self.assertEqual(new_wb.active.cell(row=28, column=1).value, 1)

        self.assertEqual(new_wb.active.cell(row=2, column=2).value, 'bo€>é')
        self.assertEqual(new_wb.active.cell(row=3, column=2).value, 'oki')
        self.assertEqual(new_wb.active.cell(row=28, column=2).value, 'bo€>é')

        self.assertEqual(new_wb.active.cell(row=2, column=3).value, datetime.datetime(1900, 1, 1, 2, 24))
        self.assertEqual(new_wb.active.cell(row=3, column=3).value, datetime.datetime(1900, 1, 1, 4, 48))
        self.assertEqual(new_wb.active.cell(row=28, column=3).value, datetime.datetime(1900, 1, 1, 2, 24))
        os.remove(f.name)
コード例 #3
0
    def test_wrong_template(self):
        template = io.BytesIO()
        queryset = [list(range(10)) for i in range(8)]
        stream = streaming.stream_queryset_as_xlsx(queryset, xlsx_template=template, batch_size=10)

        f = tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False)
        for chunk in stream:
            f.write(chunk)
        f.close()

        new_wb = openpyxl.load_workbook(filename=f.name)
        for row in new_wb.active.rows:
            self.assertEqual([c.value for c in row], [str(i) for i in range(10)])
コード例 #4
0
    def test_wrong_template(self):
        template = io.BytesIO()
        queryset = [list(range(10)) for i in range(8)]
        stream = streaming.stream_queryset_as_xlsx(queryset, xlsx_template=template, batch_size=10)

        f = tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False)
        for chunk in stream:
            f.write(chunk)
        f.close()

        new_wb = openpyxl.load_workbook(filename=f.name)
        for row in new_wb.active.rows:
            self.assertEqual([c.value for c in row], [str(i) for i in range(10)])