示例#1
0
 def test_fails_on_excel_from_csv(self):
     csv_path = dev_test.CUSTOMERS_CSV_PATH
     try:
         list(rowio.excel_rows(csv_path))
         self.fail()
     except errors.DataFormatError as anticipated_error:
         dev_test.assert_fnmatches(self, str(anticipated_error), '* (R1C1): cannot read Excel file: *')
示例#2
0
 def test_fails_on_excel_from_ods(self):
     ods_path = dev_test.path_to_test_data('valid_customers.ods')
     try:
         list(rowio.excel_rows(ods_path))
         self.fail()
     except errors.DataFormatError as anticipated_error:
         dev_test.assert_fnmatches(self, str(anticipated_error), '* (R1C1): cannot read Excel file: *')
示例#3
0
 def test_fails_on_ods_from_excel(self):
     excel_path = dev_test.path_to_test_data('valid_customers.xls')
     try:
         list(rowio.ods_rows(excel_path))
         self.fail()
     except errors.DataFormatError as anticipated_error:
         dev_test.assert_fnmatches(self, str(anticipated_error), '*: cannot uncompress ODS spreadsheet: *')
示例#4
0
 def test_fails_on_writing_broken_field(self):
     with io.StringIO() as delimited_stream:
         with validio.Writer(self._standard_delimited_cid, delimited_stream) as delimited_writer:
             delimited_writer.write_row(['Miller', '173', '1967-05-23'])
             try:
                 delimited_writer.write_row(['Webster', 'not_a_number', '1983-11-02'])
             except errors.FieldValueError as anticipated_error:
                 dev_test.assert_fnmatches(
                     self, str(anticipated_error),
                     "* (R2C2): cannot accept field 'height': value must be an integer number: *'not_a_number'")
示例#5
0
 def test_fails_on_broken_data_before_skipping_validation(self):
     data_with_row_3_broken = '1\n2\na\n'
     with io.StringIO(data_with_row_3_broken) as partially_broken_data:
         with validio.Reader(_DIGIT_CID, partially_broken_data, validate_until=3) as reader:
             try:
                 list(reader.rows())
                 self.fail()
             except errors.FieldValueError as anticipated_error:
                 dev_test.assert_fnmatches(
                     self, str(anticipated_error),
                     "* (R3C1): cannot accept field 'digit': value must be an integer number: 'a'")
示例#6
0
 def test_fails_on_unicode_error_during_fixed_write(self):
     fixed_data_format = data.DataFormat(data.FORMAT_FIXED)
     fixed_data_format.set_property(data.KEY_ENCODING, 'ascii')
     fixed_data_format.validate()
     fixed_path = dev_test.path_to_test_result('test_fails_on_unicode_error_during_fixed_write.txt')
     with rowio.FixedRowWriter(fixed_path, fixed_data_format, [('x', 1)]) as fixed_writer:
         fixed_writer.write_row(['a'])
         try:
             fixed_writer.write_row([_EURO_SIGN])
             self.fail()
         except errors.DataError as anticipated_error:
             anticipated_error_message = str(anticipated_error)
             dev_test.assert_fnmatches(
                 self, anticipated_error_message, "*.txt (R2C1): cannot write data row: *; row=*")
示例#7
0
 def test_fails_on_writing_fixed_row_with_too_few_fields(self):
     with io.StringIO() as fixed_stream:
         with validio.Writer(self._standard_fixed_cid, fixed_stream) as fixed_writer:
             broken_rows_to_write = [
                 ['Miller    ', '173', '1967-05-23'],
                 ['Webster   ', 'abc'],
             ]
             try:
                 fixed_writer.write_rows(broken_rows_to_write)
                 self.fail()
             except errors.DataError as anticipated_error:
                 dev_test.assert_fnmatches(
                     self, str(anticipated_error),
                     "* row must contain 3 fields but only has 2: *'Webster   ', *'abc'?")
示例#8
0
 def test_fails_on_writing_fixed_integer_field_with_non_numeric_value(self):
     with io.StringIO() as fixed_stream:
         with validio.Writer(self._standard_fixed_cid, fixed_stream) as fixed_writer:
             broken_rows_to_write = [
                 ['Miller    ', '173', '1967-05-23'],
                 ['Webster   ', 'abc', '1983-11-02'],
             ]
             try:
                 fixed_writer.write_rows(broken_rows_to_write)
                 self.fail()
             except errors.DataError as anticipated_error:
                 dev_test.assert_fnmatches(
                     self, str(anticipated_error),
                     "* (R2C2): cannot accept field 'height': value must be an integer number: 'abc'")
示例#9
0
 def test_fails_on_unicode_error_during_delimited_write(self):
     delimited_data_format = data.DataFormat(data.FORMAT_DELIMITED)
     delimited_data_format.set_property(data.KEY_ENCODING, 'ascii')
     delimited_data_format.validate()
     delimited_path = dev_test.path_to_test_result('test_fails_on_unicode_error_during_delimited_write.csv')
     with io.open(delimited_path, 'w', newline='', encoding=delimited_data_format.encoding) as delimited_target_stream:
         with rowio.DelimitedRowWriter(delimited_target_stream, delimited_data_format) as delimited_writer:
             try:
                 delimited_writer.write_row(['a'])
                 delimited_writer.write_row(['b', _EURO_SIGN])
                 self.fail()
             except errors.DataError as anticipated_error:
                 anticipated_error_message = str(anticipated_error)
                 dev_test.assert_fnmatches(
                     self, anticipated_error_message, "*.csv (R2C1): cannot write data row: *; row=*'b', *")
示例#10
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)
示例#11
0
 def test_fails_on_writing_too_long_fixed_field(self):
     with io.StringIO() as fixed_stream:
         with validio.Writer(self._standard_fixed_cid, fixed_stream) as fixed_writer:
             broken_rows_to_write = [
                 ['Miller    ', '173', '1967-05-23'],
                 ['Webster   ', '16789', '1983-11-02'],
             ]
             try:
                 fixed_writer.write_rows(broken_rows_to_write)
                 self.fail()
             except errors.DataError as anticipated_error:
                 dev_test.assert_fnmatches(
                     self, str(anticipated_error),
                     "* (R2C2): cannot accept field 'height': fixed format field must have at most 3 characters "
                     + "instead of 5: '16789'")
示例#12
0
 def test_fails_on_error_in_first_non_header_row(self):
     cid_text = '\n'.join([
         'd,format,delimited',
         'd,header,1',
         'f,some_number,,,,Integer',
     ])
     cid = interface.create_cid_from_string(cid_text)
     with io.StringIO('some_number\nabc\n') as broken_data:
         with validio.Reader(cid, broken_data) as reader:
             try:
                 list(reader.rows())
                 self.fail()
             except errors.FieldValueError as anticipated_error:
                 dev_test.assert_fnmatches(
                     self, str(anticipated_error),
                     "* (R2C1): cannot accept field 'some_number': value must be an integer number: 'abc'")
示例#13
0
 def test_can_create_caller_location(self):
     location = errors.create_caller_location()
     dev_test.assert_fnmatches(self, str(location), "test_errors.py ([1-9]*)")