Beispiel #1
0
 def test_errors(self) -> None:
     with CsvWriter(DATA_FILE, fieldnames=['a', 'b', 'c', 'd']) as writer:
         writer.write(['One', 2, 3.0, True])
     with CsvReader(DATA_FILE, types=[int, int, float]) as reader:
         with self.assertRaisesRegex(ValueError, r'invalid literal for int\(\) with base 10: \'One\''):
             self.assertListEqual(reader.read_list(), ['One', 2, 3.0, 'True'])
     os.remove(DATA_FILE)
Beispiel #2
0
 def test_lists(self) -> None:
     with CsvWriter(DATA_FILE, fieldnames=['a', 'b', 'c']) as writer:
         self.__write_lists_writer(writer)
     with CsvReader(DATA_FILE) as reader:
         self.check_lists_reader(reader)
     with CsvReader(DATA_FILE) as reader:
         self.check_dicts_reader(reader)
     with CsvReader(DATA_FILE) as reader:
         self.check_objects_reader(reader)
     os.remove(DATA_FILE)
Beispiel #3
0
 def test_dict_2_object(self) -> None:
     d = {'1': 1, 'G&S': 2}
     with CsvWriter(DATA_FILE, fieldnames=d) as writer:
         writer.write_dict(d)
         writer.write_list(list(d.values()))
     with CsvReader(DATA_FILE) as reader:
         for obj in reader:
             pass
     self.assertEqual(obj.n1, '1')
     self.assertEqual(obj.G_S, '2')
     os.remove(DATA_FILE)
Beispiel #4
0
 def test_write_csv_dict(self) -> None:
     with CsvWriter(DATA_FILE, fieldnames=['a', 'b', 'c']) as writer:
         self.assertEqual(len(writer), 0)
         write_registers(writer)
         self.assertEqual(len(writer), 4)
     self.assertTrue(os.path.exists(DATA_FILE))
     with CsvReader(DATA_FILE) as reader:
         for obj in tqdm(reader):
             pass
     self.assertEqual(obj.b, '8')
     os.remove(DATA_FILE)
Beispiel #5
0
 def test_import(self) -> None:
     with CsvWriter(DATA_FILE, fieldnames=['a', 'b', 'c']) as writer:
         self.assertEqual(len(writer), 0)
         write_registers(writer)
         self.assertEqual(len(writer), 4)
     self.assertTrue(os.path.exists(DATA_FILE))
     with CsvReader(DATA_FILE) as reader:
         with ExcelWriter(EXCEL_FILE, fieldnames=reader.fieldnames) as writer:
             writer.import_reader(reader)
     self.assertTrue(os.path.exists(EXCEL_FILE))
     os.remove(DATA_FILE)
     os.remove(EXCEL_FILE)
     with ExcelWriter(EXCEL_FILE, fieldnames=['a', 'b', 'c']) as writer:
         self.assertEqual(len(writer), 0)
         write_registers(writer)
         self.assertEqual(len(writer), 4)
     self.assertTrue(os.path.exists(EXCEL_FILE))
     with ExcelReader(EXCEL_FILE) as reader:
         with CsvWriter(DATA_FILE, fieldnames=reader.fieldnames) as writer:
             writer.import_reader(reader)
     self.assertTrue(os.path.exists(DATA_FILE))
     os.remove(DATA_FILE)
     os.remove(EXCEL_FILE)
Beispiel #6
0
 def test_write_csv_obj(self) -> None:
     with CsvWriter(COMPRESSED_FILE, fieldnames=Employee) as writer:
         writer.write(Employee('John', 'Smith'))
         writer.write(Employee('Maria', 'Ortega'))
     with CsvReader(COMPRESSED_FILE) as reader:
         obj = next(reader)
         self.assertEqual(obj.name, 'John')
         self.assertEqual(obj.surname, 'Smith')
         obj = next(reader)
         self.assertEqual(obj.name, 'Maria')
         self.assertEqual(obj.surname, 'Ortega')
         with self.assertRaises(StopIteration):
             reader.read()
     os.remove(COMPRESSED_FILE)
Beispiel #7
0
 def test_csv_types(self) -> None:
     with CsvWriter(DATA_FILE, fieldnames=['a', 'b', 'c', 'd']) as writer:
         writer.write(['One', 2, 3.0, True])
     with CsvReader(DATA_FILE, types=[str, int, float, bool]) as reader:
         self.assertListEqual(reader.read_list(), ['One', 2, 3.0, True])
     with CsvReader(DATA_FILE, types={'a': str, 'b': int, 'c': float, 'd': bool}) as reader:
         self.assertListEqual(reader.read_list(), ['One', 2, 3.0, True])
     with CsvReader(DATA_FILE, types=[str, int, float]) as reader:
         self.assertListEqual(reader.read_list(), ['One', 2, 3.0, 'True'])
     l = csv2list(DATA_FILE, types=[str, int, float])
     self.assertListEqual(l[0], ['One', 2, 3.0, 'True'])
     d = csv2dict(DATA_FILE, types=[str, int, float])
     self.assertDictEqual(d[0], {'a': 'One', 'b': 2, 'c': 3.0, 'd': 'True'})
     o = csv2objects(DATA_FILE, types=[str, int, float])
     self.assertEqual(o[0].c, 3.0)
     os.remove(DATA_FILE)
Beispiel #8
0
 def test_read_modes(self) -> None:
     with CsvWriter(DATA_FILE, fieldnames=['a', 'b', 'c']) as writer:
         self.__write_lists_writer(writer)
     with CsvReader(DATA_FILE, mode=ReadMode.DICT) as reader:
         for obj in reader:
             pass
     self.assertDictEqual(obj, {'a': '22', 'b': '23', 'c': '24'})
     with CsvReader(DATA_FILE, mode=ReadMode.LIST) as reader:
         for obj in reader:
             pass
     self.assertListEqual(obj, ['22', '23', '24'])
     with CsvReader(DATA_FILE, mode=ReadMode.OBJECT) as reader:
         for obj in reader:
             pass
     self.assertEqual(obj.b, '23')
     self.assertEqual(obj.c, '24')
     os.remove(DATA_FILE)
Beispiel #9
0
    def test_strems(self) -> None:
        with open('data.csv', 'wt') as file:
            writer = CsvWriter(file, ['a', 'b', 'c'])
            self.assertEqual(len(writer), 0)
            writer.write_lists(lists)
            self.assertEqual(len(writer), 3)
            writer.write_dicts(dicts)
            self.assertEqual(len(writer), 5)
            writer.write_objects(objects)
            self.assertEqual(len(writer), 8)
        with open('data.csv', 'rt') as file:
            with self.assertRaisesRegex(DataSourceError, 'The length of the data source cannot be computed if it is '
                                                       'defined as a file stream instead of a file path.'):
                len(CsvReader(file))

        with open(DATA_FILE, 'at') as file:
            with self.assertRaisesRegex(ValueError, r'The reader is in mode Mode.WRITE but the file stream is in not '
                                                    r'in write mode \("at"\).'):
                CsvWriter(file, ['a', 'b', 'c'])
        with open_file(DATA_FILE, Mode.WRITE) as file:
            with self.assertRaisesRegex(ValueError, r'The reader is in mode Mode.APPEND but the file stream is in not '
                                                    r'in append mode \("wt"\).'):
                CsvWriter(file, ['a', 'b', 'c'], Mode.APPEND)
        with open_file(DATA_FILE, Mode.APPEND) as file:
            with self.assertRaisesRegex(DataSourceError,
                                        'The length of the data source cannot be computed if it is defined as a file '
                                        'stream, instead of a file path and this writer is opened in APPEND mode.'):
                len(CsvWriter(file, ['a', 'b', 'c'], Mode.APPEND))
        os.remove(DATA_FILE)