예제 #1
0
    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_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)))
예제 #3
0
    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='|')))
예제 #4
0
    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)))