示例#1
0
 def test_simple(self):
     fieldnames = ['a.b.c', 'abc', 'a.d[0]', 'a.d[1][0]', 'a.d[2].a']
     w = NestedDictWriter(self.f, fieldnames)
     w.writerow({'a': {'b': {'c': 1}, 'd': [3, [4], {'a': 5}]}, 'abc': 2})
     w.writerow({'a': {'b': {'c': 6}, 'd': [8, [9], {'a': 10}]}, 'abc': 7})
     self.assertEqual(self.read_all(), [
         ['1', '2', '3', '4', '5'],
         ['6', '7', '8', '9', '10'],
     ])
示例#2
0
 def test_array_empty(self):
     fieldnames = ['a.a[]', 'a.b[].c[]', 'a.d']
     w = NestedDictWriter(self.f, fieldnames)
     w.writerow({'a': {'a': [], 'b': [], 'd': 1}})
     w.writerow({'a': {'a': [], 'b': [], 'd': 2}})
     self.assertEqual(self.read_all(), [
         ['', '', '1'],
         ['', '', '2'],
     ])
示例#3
0
 def test_array_id(self):
     fieldnames = ['a[].b', 'a[].c[]', 'a[id]', 'a[].c[id]']
     w = NestedDictWriter(self.f, fieldnames)
     w.writerow({'a': [{'b': 1, 'c': [3, 4, 5]}, {'b': 2, 'c': [6, 7]}]})
     w.writerow({'a': [{'b': 8, 'c': []}]})
     self.assertEqual(self.read_all(), [
         ['1', '3', '0', '0'],
         ['1', '4', '0', '1'],
         ['1', '5', '0', '2'],
         ['2', '6', '1', '0'],
         ['2', '7', '1', '1'],
         ['2', '', '1', '2'],
         ['8', '', '0', '0'],
     ])
示例#4
0
 def test_raise_on_missing(self):
     fieldnames = ['a.a', 'a.b[]', 'a.c[0]']
     w = NestedDictWriter(self.f, fieldnames, raise_on_missing=True)
     # empty array is not error
     w.writerow({'a': {'a': 1, 'b': [], 'c': [2]}})
     self.assertEqual(self.read_all(), [
         ['1', '', '2'],
     ])
     with self.assertRaises(KeyError):
         w.writerow({'a': {'b': [], 'c': [1]}})
     with self.assertRaises(KeyError):
         w.writerow({'a': {'a': 1, 'c': [1]}})
     with self.assertRaises(IndexError):
         w.writerow({'a': {'a': 1, 'b': [], 'c': []}})
示例#5
0
 def test_restval(self):
     fieldnames = ['a.a', 'a.b[]', 'a.c[0]']
     w = NestedDictWriter(self.f,
                          fieldnames,
                          raise_on_missing=False,
                          restval='none')
     # empty array is not error
     w.writerow({'a': {'a': 1, 'b': [], 'c': [2]}})
     w.writerow({'a': {'b': [1], 'c': [2]}})
     w.writerow({'a': {'a': 1, 'c': [2]}})
     w.writerow({'a': {'a': 1, 'b': [2], 'c': []}})
     self.assertEqual(self.read_all(), [
         ['1', 'none', '2'],
         ['none', '1', '2'],
         ['1', 'none', '2'],
         ['1', '2', 'none'],
     ])
示例#6
0
 def test_write_id(self):
     fieldnames = ['id', 'a[]']
     w = NestedDictWriter(self.f, fieldnames)
     w.writerow({'a': [1, 2]}, id=100)
     w.writerow({'a': [3, 4, 5], 'id': 200})
     # overwrite id by original data
     w.writerow({'a': [6, 7], 'id': 300}, id=400)
     self.assertEqual(self.read_all(), [
         ['100', '1'],
         ['100', '2'],
         ['200', '3'],
         ['200', '4'],
         ['200', '5'],
         ['300', '6'],
         ['300', '7'],
     ])
示例#7
0
 def test_array(self):
     fieldnames = ['a.a[]', 'a.b[].c', 'a.b[].d[]', 'b.c[]', 'b.d[]', 'a.c']
     w = NestedDictWriter(self.f, fieldnames)
     w.writerow({
         'a': {
             'a': [1, 2, 3, 4],
             'b': [{
                 'c': 5,
                 'd': [7, 8]
             }, {
                 'c': 6,
                 'd': [9, 10, 11]
             }],
             'c': 15
         },
         'b': {
             'c': [12, 13, 14],
             'd': []
         }
     })
     w.writerow({
         'a': {
             'a': [16, 17, 18, 19],
             'b': [{
                 'c': 20,
                 'd': [22, 23]
             }, {
                 'c': 21,
                 'd': [24, 25, 26]
             }],
             'c': 30
         },
         'b': {
             'c': [27, 28, 29],
             'd': []
         }
     })
     self.assertEqual(self.read_all(), [
         ['1', '5', '7', '12', '', '15'],
         ['1', '5', '8', '12', '', '15'],
         ['1', '5', '', '12', '', '15'],
         ['2', '6', '9', '13', '', '15'],
         ['2', '6', '10', '13', '', '15'],
         ['2', '6', '11', '13', '', '15'],
         ['3', '', '', '14', '', '15'],
         ['3', '', '', '14', '', '15'],
         ['3', '', '', '14', '', '15'],
         ['4', '', '', '', '', '15'],
         ['4', '', '', '', '', '15'],
         ['4', '', '', '', '', '15'],
         ['16', '20', '22', '27', '', '30'],
         ['16', '20', '23', '27', '', '30'],
         ['16', '20', '', '27', '', '30'],
         ['17', '21', '24', '28', '', '30'],
         ['17', '21', '25', '28', '', '30'],
         ['17', '21', '26', '28', '', '30'],
         ['18', '', '', '29', '', '30'],
         ['18', '', '', '29', '', '30'],
         ['18', '', '', '29', '', '30'],
         ['19', '', '', '', '', '30'],
         ['19', '', '', '', '', '30'],
         ['19', '', '', '', '', '30'],
     ])