Debian Codegraph @

petl/test/util/test_materialise.py

raw
1 from __future__ import absolute_import, print_function, division
2
3 import pytest
4
5 from petl.errors import FieldSelectionError
6 from petl.test.helpers import eq_
7 from petl.util.materialise import columns, facetcolumns
8
9
10 def test_columns():
11
12 table = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]]
13 cols = columns(table)
14 eq_(['a', 'b', 'b'], cols['foo'])
15 eq_([1, 2, 3], cols['bar'])
16
17
18 def test_columns_empty():
19 table = [('foo', 'bar')]
20 cols = columns(table)
21 eq_([], cols['foo'])
22 eq_([], cols['bar'])
23
24
25 def test_columns_headerless():
26 table = []
27 cols = columns(table)
28 eq_({}, cols)
29
30
31 def test_facetcolumns():
32
33 table = [['foo', 'bar', 'baz'],
34 ['a', 1, True],
35 ['b', 2, True],
36 ['b', 3]]
37
38 fc = facetcolumns(table, 'foo')
39 eq_(['a'], fc['a']['foo'])
40 eq_([1], fc['a']['bar'])
41 eq_([True], fc['a']['baz'])
42 eq_(['b', 'b'], fc['b']['foo'])
43 eq_([2, 3], fc['b']['bar'])
44 eq_([True, None], fc['b']['baz'])
45
46
47 def test_facetcolumns_headerless():
48 table = []
49 with pytest.raises(FieldSelectionError):
50 facetcolumns(table, 'foo')