view tests/test_json2csv.py @ 18:56596902e9ae default tip

add some setup + tests
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:57:03 -0800
parents
children
line wrap: on
line source

#!/usr/bin/env python

"""
test JSON to CSV conversion
"""

import unittest
from lemuriformes import json2csv


class TestJSON2CSV(unittest.TestCase):

    def test_flatten(self):
        data = [{'a': 'B', 'c': 1, 'd': 'd'},
                {'a': 'a', 'c': 2, 'd': 'e'},
                {'a': 'H', 'c': 3, 'd': 'I'},
                {'a': 'K', 'c': 5, 'd': 'L'},
        ]
        header = ['a', 'c', 'd']
        expected = [['B', 1, 'd'],
                    ['a', 2, 'e'],
                    ['H', 3, 'I'],
                    ['K', 5, 'L'],
        ]

        received_header, flattened = json2csv.flatten_list_of_dicts(data, header=header)
        assert flattened == expected
        assert received_header == header

if __name__ == '__main__':
    unittest.main()