Exemplo n.º 1
0
    def test_init(self):
        key_map = {'some_header': 'some_key'}
        loader = Json2Csv(key_map)
        self.assertEquals(loader.key_map, key_map)

        self.assertRaises(ValueError, Json2Csv, None)

        self.assertRaises(ValueError, Json2Csv, {})
Exemplo n.º 2
0
    def test_init(self):
        outline = {'map': [['some_header', 'some_key']]}
        loader = Json2Csv(outline)
        self.assertIn('some_header', loader.key_map)

        self.assertRaises(ValueError, Json2Csv, None)

        self.assertRaises(ValueError, Json2Csv, {})
Exemplo n.º 3
0
    def test_load_json(self):
        outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
        loader = Json2Csv(outline)
        with open('fixtures/data.json') as f:
            loader.load(f)

        first_row = loader.rows[0]
        self.assertEqual(first_row['author'], 'Someone')
        second_row = loader.rows[1]
        self.assertEqual(second_row['author'], 'Another')
        third_row = loader.rows[2]
        self.assertEqual(third_row['author'], 'Me too')
Exemplo n.º 4
0
    def test_process_row(self):
        """Given a valid key-map and data, it should return a valid row"""
        outline = {'map': [['id', '_id'], ['count', 'count']]}
        loader = Json2Csv(outline)
        test_data = json.loads('{"_id" : "Someone","count" : 1}')
        row = loader.process_row(test_data)

        self.assertIs(type(row), dict)
        self.assertIn('id', row.keys())
        self.assertIn('count', row.keys())

        self.assertEquals(row['id'], 'Someone')
        self.assertEquals(row['count'], 1)
Exemplo n.º 5
0
    def test_process_each(self):
        outline = {'map': [['id', '_id'], ['count', 'count']], 'collection': 'result'}
        loader = Json2Csv(outline)

        test_data = json.loads('{"result":[{"_id" : "Someone","count" : 1}]}')
        loader.process_each(test_data)

        self.assertEquals(len(loader.rows), 1)
        row = loader.rows[0]
        self.assertIs(type(row), dict)
        self.assertIn('id', row.keys())
        self.assertIn('count', row.keys())

        self.assertEquals(row['id'], 'Someone')
        self.assertEquals(row['count'], 1)
Exemplo n.º 6
0
    def test_process_row_nested_data(self):
        """Ensure that nested keys (with . notation) are processed"""
        key_map = {"map": [['author', 'source.author'], ['message', 'message.original']]}
        loader = Json2Csv(key_map)
        test_data = json.loads(
            '{"source": {"author": "Someone"}, "message": {"original": "Hey!", "Revised": "Hey yo!"}}'
        )
        row = loader.process_row(test_data)

        self.assertIs(type(row), dict)
        self.assertIn('author', row.keys())
        self.assertIn('message', row.keys())

        self.assertEquals(row['author'], 'Someone')
        self.assertEquals(row['message'], 'Hey!')
Exemplo n.º 7
0
    def test_process_each_optional_key(self):
        """Ensure a key that is not always present won't prevent data extraction
        Where the data is missing, None is returned
        """
        key_map = {'id': '_id', 'count': 'count'}
        loader = Json2Csv(key_map)

        test_data = json.loads(
            '[{"_id" : "Someone","count" : 1}, {"_id": "Another"}]')
        self.assertEquals(len(test_data), 2)
        loader.process_each(test_data)

        self.assertEquals(len(loader.rows), 2)
        second_row = loader.rows[1]
        self.assertEquals(second_row['id'], 'Another')
        self.assertIsNone(second_row['count'])
Exemplo n.º 8
0
    def test_default_delimiter(self):
        outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
        loader = Json2Csv(outline)
        with open('fixtures/data.json') as f:
            loader.load(f)

        filename = "test.csv"
        loader.write_csv(filename=filename)
        output = open(filename, "r")
        csvString = output.read()

        author = 'author'
        delimiterSubstring = csvString[len(author):len(author)+1]

        self.assertEqual(delimiterSubstring, ',')

        os.remove(filename)
Exemplo n.º 9
0
    def test_default_delimiter(self):
        ## Credits to James Hartt: https://github.com/jameshartt/json2csv/commit/a032b6862b04f59498b39eb7c9dc494be6c233b0
        outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
        loader = Json2Csv(outline)
        with open('fixtures/data.json') as f:
            loader.load(f)

        filename = "test.csv"
        loader.write_csv(filename=filename)
        output = open(filename, "r")
        csvString = output.read()

        author = 'author'
        delimiterSubstring = csvString[len(author):len(author)+1]

        self.assertEqual(delimiterSubstring, ',')

        os.remove(filename)
Exemplo n.º 10
0
    def test_process_each(self):
        outline = {'map': [['id', '_id'], ['count', 'count']], 'collection': 'result'}
        loader = Json2Csv(outline)
        print(f"loader.key_map: {loader.key_map}")

        test_data = json.loads('{"result":[{"_id" : "Someone","count" : 1}]}')
        print(f"test_data: {test_data}")
        loader.process_each(test_data)

        print(f"loader.rows: {loader.rows}")
        self.assertEqual(len(loader.rows), 1)
        row = loader.rows[0]
        self.assertIs(type(row), dict)
        self.assertIn('id', list(row.keys()))
        self.assertIn('count', list(row.keys()))

        print(f"row: {row}")
        self.assertEqual(row['id'], 'Someone')
        self.assertEqual(row['count'], 1)
Exemplo n.º 11
0
    def test_process_each_optional_key(self):
        """Ensure a key that is not always present won't prevent data extraction
        Where the data is missing, None is returned
        """
        outline = {'map': [['id', '_id'], ['count', 'count'], ['tags_0', 'tags.0']]}
        loader = Json2Csv(outline)

        test_data = json.loads('''[
          {"_id": "Someone","count": 1, "tags": ["super"]},
          {"_id": "Another", "tags": []}]''')
        self.assertEquals(len(test_data), 2)
        loader.process_each(test_data)

        self.assertEquals(len(loader.rows), 2)
        second_row = loader.rows[1]
        self.assertEquals(second_row['id'], 'Another')
        # works for missing dict keys
        self.assertIsNone(second_row['count'])
        # and missing list indices
        self.assertIsNone(second_row['tags_0'])