Beispiel #1
0
 def test_can_read_fixed_rows_with_missing_terminating_line_delimiter(self):
     data_format, field_names_and_lengths = FixedRowsTest._create_fixed_data_format_and_fields_for_name_and_height()
     with io.StringIO('hugo172\nsepp163') as data_io:
         rows = list(rowio.fixed_rows(
             data_io, data_format.encoding, field_names_and_lengths, data_format.line_delimiter))
     self._assert_rows_contain_data(rows)
     self.assertEqual(2, len(rows))
Beispiel #2
0
 def test_can_read_empty_fixed_rows(self):
     data_format = data.DataFormat(data.FORMAT_FIXED)
     data_format.validate()
     with io.StringIO('') as data_io:
         rows = list(rowio.fixed_rows(
             data_io, data_format.encoding, (('dummy', 1),), data_format.line_delimiter))
     self.assertEqual([], rows)
Beispiel #3
0
 def test_can_read_fixed_rows_with_mixed_line_delimiters_terminated_by_carriage_return(
         self):
     data_format, field_names_and_lengths = FixedRowsTest._create_fixed_data_format_and_fields_for_name_and_height(
     )
     with io.StringIO('john172\r\nmary163\r') as data_io:
         rows = list(
             rowio.fixed_rows(data_io, data_format.encoding,
                              field_names_and_lengths,
                              data_format.line_delimiter))
     self.assertEqual([['john', '172'], ['mary', '163']], rows)
Beispiel #4
0
 def test_can_read_fixed_rows_with_mixed_line_delimiters(self):
     data_format, field_names_and_lengths = FixedRowsTest._create_fixed_data_format_and_fields_for_name_and_height(
     )
     with io.StringIO(
             'john172\rmary163\nbill167\r\njane184\r\n') as data_io:
         rows = list(
             rowio.fixed_rows(data_io, data_format.encoding,
                              field_names_and_lengths,
                              data_format.line_delimiter))
     self.assertEqual([['john', '172'], ['mary', '163'], ['bill', '167'],
                       ['jane', '184']], rows)
Beispiel #5
0
 def test_can_read_fixed_rows(self):
     cid_path = dev_test.path_to_test_cid('customers_fixed.ods')
     customer_cid = interface.Cid(cid_path)
     fixed_path = dev_test.path_to_test_data('valid_customers_fixed.txt')
     field_names_and_lengths = interface.field_names_and_lengths(customer_cid)
     rows = list(rowio.fixed_rows(fixed_path, customer_cid.data_format.encoding, field_names_and_lengths))
     self.assertNotEqual(0, len(rows))
     for row_index in range(len(rows) - 1):
         row = rows[row_index]
         next_row = rows[row_index + 1]
         self.assertNotEqual(0, len(row))
         self.assertEqual(len(row), len(next_row))
Beispiel #6
0
    def _test_can_read_fixed_rows_from_stringio(self, data_text, data_format=None):
        assert (data_format is None) or data_format.is_valid

        default_data_format, field_names_and_lengths = \
            FixedRowsTest._create_fixed_data_format_and_fields_for_name_and_height()
        actual_data_format = default_data_format if data_format is None else data_format
        with io.StringIO(data_text) as data_io:
            rows = list(rowio.fixed_rows(
                data_io, actual_data_format.encoding, field_names_and_lengths, actual_data_format.line_delimiter))
        self._assert_rows_contain_data(rows)
        for row in rows:
            for item in row:
                self.assertTrue(item in data_text, 'item %r must be part of data %r' % (item, data_text))
Beispiel #7
0
    def _fails_on_fixed_rows_from_stringio(self, data_text, expected_error_pattern='*', data_format=None):
        assert (data_format is None) or data_format.is_valid

        default_data_format, field_names_and_lengths = \
            FixedRowsTest._create_fixed_data_format_and_fields_for_name_and_height()
        actual_data_format = default_data_format if data_format is None else data_format
        with io.StringIO(data_text) as data_io:
            rows = rowio.fixed_rows(
                data_io, actual_data_format.encoding, field_names_and_lengths, actual_data_format.line_delimiter)
            try:
                for _ in rows:
                    pass
                self.fail()
            except errors.DataFormatError as anticipated_error:
                dev_test.assert_fnmatches(self, str(anticipated_error), expected_error_pattern)
Beispiel #8
0
 def _raw_rows(self):
     data_format = self.cid.data_format
     format = data_format.format
     if format == data.FORMAT_EXCEL:
         return rowio.excel_rows(self._source_data_stream_or_path, data_format.sheet)
     elif format == data.FORMAT_DELIMITED:
         return rowio.delimited_rows(self._source_data_stream_or_path, data_format)
     elif format == data.FORMAT_FIXED:
         return rowio.fixed_rows(
             self._source_data_stream_or_path, data_format.encoding, interface.field_names_and_lengths(self.cid),
             data_format.line_delimiter)
     elif format == data.FORMAT_ODS:
         return rowio.ods_rows(self._source_data_stream_or_path, data_format.sheet)
     else:
         assert False, 'format=%r' % format
Beispiel #9
0
 def _raw_rows(self):
     data_format = self.cid.data_format
     format = data_format.format
     if format == data.FORMAT_EXCEL:
         return rowio.excel_rows(self._source_data_stream_or_path, data_format.sheet)
     elif format == data.FORMAT_DELIMITED:
         return rowio.delimited_rows(self._source_data_stream_or_path, data_format)
     elif format == data.FORMAT_FIXED:
         return rowio.fixed_rows(
             self._source_data_stream_or_path, data_format.encoding, interface.field_names_and_lengths(self.cid),
             data_format.line_delimiter)
     elif format == data.FORMAT_ODS:
         return rowio.ods_rows(self._source_data_stream_or_path, data_format.sheet)
     else:
         assert False, 'format=%r' % format
Beispiel #10
0
 def test_can_read_fixed_rows_with_mixed_line_delimiters_terminated_by_carriage_return(self):
     data_format, field_names_and_lengths = FixedRowsTest._create_fixed_data_format_and_fields_for_name_and_height()
     with io.StringIO('john172\r\nmary163\r') as data_io:
         rows = list(rowio.fixed_rows(
             data_io, data_format.encoding, field_names_and_lengths, data_format.line_delimiter))
     self.assertEqual([['john', '172'], ['mary', '163']], rows)
Beispiel #11
0
 def test_can_read_fixed_rows_with_mixed_line_delimiters(self):
     data_format, field_names_and_lengths = FixedRowsTest._create_fixed_data_format_and_fields_for_name_and_height()
     with io.StringIO('john172\rmary163\nbill167\r\njane184\r\n') as data_io:
         rows = list(rowio.fixed_rows(
             data_io, data_format.encoding, field_names_and_lengths, data_format.line_delimiter))
     self.assertEqual([['john', '172'], ['mary', '163'], ['bill', '167'], ['jane', '184']], rows)