Beispiel #1
0
    def test_truncate(self):
        content = """Lôrèm ipsum dolor sit amet, consectetur adipiscing elit. Proin ac odio libero.
Praesent sollicitudin, mauris non sagittis tincidunt, magna libero malesuada lectus, sit amet dictum nulla mi ac justo.
Vivamus laoreet metus eu purus tincidunt, et consectetur justo mattis.
Phasellus egestas a lacus nec pulvinar.
Sed a lectus eleifend, hendrerit ligula nec, aliquet sem.
Quisque nec tortor nec ante pharetra cursus sed facilisis lorem.
Praesent blandit pharetra nulla, id ultrices diam molestie sed.
""" * 100
        self.assertGreater(len(content), 32767)

        file = NamedTemporaryFile(suffix='.xls')

        wt = XlwtWriter()
        wt.writerow([content])

        with self.assertNoException():
            wt.save(file.name)

        read_content = [*XlrdReader(filedata=file.name)]
        self.assertEqual(1, len(read_content))

        elt = read_content[0]
        self.assertEqual(1, len(elt))
        self.assertEqual(32767, len(elt[0]))
Beispiel #2
0
    def test_write_and_read(self):
        file = NamedTemporaryFile(suffix=".xls")

        wt = XlwtWriter()
        writerow = wt.writerow
        for element in self.data:
            writerow(element)
        wt.save(file.name)

        rd = XlrdReader(filedata=file.name)
        self.assertEqual([*rd], self.data)

        with open(file.name, mode='rb') as file_obj:
            file_content = file_obj.read()
            rd = XlrdReader(file_contents=file_content)
            self.assertEqual([*rd], self.data)
 def test_open_file(self):
     for filename in self.files:
         # with open(self.get_file_path(filename)) as file_obj:
         with open(self.get_file_path(filename), mode='rb') as file_obj:
             file_content = file_obj.read()
             rd = XlrdReader(file_contents=file_content)
             self.assertEqual(list(rd), self.data)
    def test_xls_export02(self):
        "Other CT, other type of fields"
        user = self.login()

        create_orga = partial(FakeOrganisation.objects.create, user=user)
        orga01 = create_orga(name='Bebop')
        orga02 = create_orga(name='Swordfish', subject_to_vat=False, creation_date=date(year=2016, month=7, day=5))

        build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation)
        cells = [build_cell(name='name'),
                 build_cell(name='subject_to_vat'),
                 build_cell(name='creation_date'),
                ]

        hf = HeaderFilter.create(pk='test-hf_orga', name='Organisation view',
                                 model=FakeOrganisation, cells_desc=cells,
                                )

        response = self.assertGET200(
            self._build_dl_url(FakeOrganisation,
                               doc_type='xls',
                               list_url=FakeOrganisation.get_lv_absolute_url(),
                               hfilter_id=hf.id,
                              ),
            follow=True,
        )

        it = iter(XlrdReader(None, file_contents=response.content))
        self.assertEqual(next(it), [hfi.title for hfi in cells])
        self.assertEqual(next(it), [orga01.name, _('Yes'), ''])
        self.assertEqual(next(it), [orga02.name, _('No'),  date_format(orga02.creation_date, 'DATE_FORMAT')])
        with self.assertRaises(StopIteration):
            next(it)
Beispiel #5
0
    def test_invalid_file(self):
        with self.assertRaises(XLRDError) as error:
            XlrdReader(filedata=self.get_file_path('data-invalid.xls'))

        self.assertEqual(
            str(error.exception),
            "Unsupported format, or corrupt file: Expected BOF record; found b'this is '"
        )
Beispiel #6
0
    def test_xls_export_header(self):
        self.login()
        # cells = self._build_hf_n_contacts()
        cells = self._build_hf_n_contacts().cells

        response = self.assertGET200(self._build_contact_dl_url(doc_type='xls',
                                                                header=True),
                                     follow=True)

        result = list(XlrdReader(None, file_contents=response.content))
        self.assertEqual(1, len(result))
        self.assertEqual(result[0], [hfi.title for hfi in cells])
Beispiel #7
0
    def test_xls_export01(self):
        user = self.login()
        cells = self._build_hf_n_contacts().cells
        existing_fileref_ids = [*FileRef.objects.values_list('id', flat=True)]

        response = self.assertGET200(
            self._build_contact_dl_url(doc_type='xls'),
            follow=True,
        )

        it = iter(
            XlrdReader(None,
                       file_contents=b''.join(response.streaming_content)))
        self.assertEqual(next(it), [hfi.title for hfi in cells])
        self.assertEqual(next(it), ['', 'Black', 'Jet', 'Bebop', ''])
        self.assertIn(next(it), (
            ['', 'Spiegel', 'Spike', 'Bebop/Swordfish', ''],
            ['', 'Spiegel', 'Spike', 'Swordfish/Bebop', ''],
        ))
        self.assertIn(next(it), (
            ['', 'Valentine', 'Faye', '', 'is a girl/is beautiful'],
            ['', 'Valentine', 'Faye', '', 'is beautiful/is a girl'],
        ))
        self.assertEqual(next(it), ['', 'Wong', 'Edward', '', 'is a girl'])
        with self.assertRaises(StopIteration):
            next(it)

        # FileRef
        filerefs = FileRef.objects.exclude(id__in=existing_fileref_ids)
        self.assertEqual(1, len(filerefs))

        fileref = filerefs[0]
        self.assertTrue(fileref.temporary)
        self.assertEqual('fakecontact.xls', fileref.basename)
        self.assertEqual(user, fileref.user)

        fullpath = fileref.filedata.path
        self.assertTrue(exists(fullpath), f'<{fullpath}> does not exists ?!')
        self.assertEqual(join(settings.MEDIA_ROOT, 'upload', 'xls'),
                         dirname(fullpath))
Beispiel #8
0
    def test_xls_export01(self):
        self.login()
        # cells = self._build_hf_n_contacts()
        cells = self._build_hf_n_contacts().cells
        existing_fileref_ids = list(
            FileRef.objects.values_list('id', flat=True))

        response = self.assertGET200(
            self._build_contact_dl_url(doc_type='xls'),
            follow=True,
        )

        it = iter(XlrdReader(None, file_contents=response.content))
        self.assertEqual(next(it), [hfi.title for hfi in cells])
        self.assertEqual(next(it), ["", "Black", "Jet", "Bebop", ""])
        self.assertIn(next(it),
                      (["", "Spiegel", "Spike", "Bebop/Swordfish", ""
                        ], ["", "Spiegel", "Spike", "Swordfish/Bebop", ""]))
        self.assertIn(
            next(it),
            (["", "Valentine", "Faye", "", "is a girl/is beautiful"
              ], ["", "Valentine", "Faye", "", "is beautiful/is a girl"]))
        self.assertEqual(next(it), ["", "Wong", "Edward", "", "is a girl"])
        with self.assertRaises(StopIteration):
            next(it)

        # FileRef
        filerefs = FileRef.objects.exclude(id__in=existing_fileref_ids)
        self.assertEqual(1, len(filerefs))

        fileref = filerefs[0]
        self.assertTrue(fileref.temporary)
        self.assertEqual('fakecontact.xls', fileref.basename)
        # self.assertEqual(user, fileref.user) TODO

        fullpath = fileref.filedata.path
        self.assertTrue(exists(fullpath),
                        '<{}> does not exists ?!'.format(fullpath))
        self.assertEqual(join(settings.MEDIA_ROOT, 'upload', 'xls'),
                         dirname(fullpath))
Beispiel #9
0
 def test_as_list(self):
     for filename in self.files:
         rd = XlrdReader(filedata=self.get_file_path(filename))
         self.assertListEqual(self.data, [*rd])
Beispiel #10
0
 def test_read_next(self):
     for filename in self.files:
         rd = XlrdReader(filedata=self.get_file_path(filename))
         for element in self.data:
             self.assertEqual(element, next(rd))
Beispiel #11
0
 def test_sheet(self):
     rd = XlrdReader(filedata=self.get_file_path(self.files[0]))
     self.assertIsNotNone(rd.book)
     self.assertIsNotNone(rd.sheet)
     self.assertEqual(rd.sheet.nrows, len(self.data))
Beispiel #12
0
 def test_unknown_filename(self):
     with self.assertRaises(IOError):
         XlrdReader(filedata=self.get_file_path('unknown.xls'))