def test_format_batch_and_load_file(self):
        # given:
        options = {
            'options': {
                'fields': ['key1', 'key2']
            }
        }
        formatter = CSVExportFormatter(options)

        # when:
        formatted_batch = [formatter.format(item) for item in self.batch]

        # then:
        memfile = self._create_memfile((it for it in formatted_batch), header=['"key1","key2"'])
        self.assertEqual(self.batch, list(csv.DictReader(memfile)))
示例#2
0
    def test_custom_writer_with_csv_formatter(self):
        # given:

        options = {
            'name':
            'exporters.export_formatter.csv_export_formatter.CSVExportFormatter',
            'options': {
                'show_titles': False,
                'fields': ['key1', 'key2']
            }
        }
        formatter = CSVExportFormatter(options)
        writer = FakeWriter({}, {}, export_formatter=formatter)

        # when:
        try:
            writer.write_batch(self.batch)
            writer.flush()
        finally:
            writer.close()

        # then:
        output = writer.custom_output[()].splitlines()
        self.assertEquals([
            ['value11', 'value21'],
            ['value12', 'value22'],
            ['value13', 'value23'],
        ], [l for l in csv.reader(output)])

        self.assertEquals('csv',
                          writer.write_buffer.items_group_files.file_extension)
    def test_format_batch_no_show_titles(self):
        # given:
        fields = ['key1', 'key2']
        options = {
            'options': {
                'show_titles': False,
                'fields': fields,
            }
        }
        formatter = CSVExportFormatter(options)

        # when:
        formatted_batch = [formatter.format(item) for item in self.batch]

        # then:
        memfile = self._create_memfile(it for it in formatted_batch)
        self.assertEqual(self.batch, list(csv.DictReader(memfile, fieldnames=fields)))
    def test_format_batch_with_custom_delimiter(self):
        # given:
        options = {
            'options': {
                'fields': ['key1', 'key2'],
                'delimiter': '|',
                'show_titles': True
            }
        }
        formatter = CSVExportFormatter(options)

        # when:
        formatted_batch = [formatter.format(item) for item in self.batch]

        # then:
        memfile = self._create_memfile((it for it in formatted_batch), header=['"key1"|"key2"'])

        self.assertEqual(self.batch, list(csv.DictReader(memfile, delimiter='|')))
    def test_format_from_schema(self):
        # given:
        options = {
            'options': {
                'show_titles': True,
                'schema': {
                    '$schema': 'http://json-schema.org/draft-04/schema',
                    'properties': {
                        'key1': {'type': 'string'},
                        'key2': {'type': 'string'}
                    },
                    'required': ['key1'],
                    'type': 'object'
                }
            }
        }
        formatter = CSVExportFormatter(options)

        # when:
        formatted_batch = [formatter.format(item) for item in self.batch]

        # then:
        memfile = self._create_memfile((it for it in formatted_batch), header=['"key1","key2"'])
        self.assertEqual(self.batch, list(csv.DictReader(memfile)))