def test_calculated_columns(): col = NamedCollection(*BASIC_DATA) col.add_calculated_column("d", "{a} + {b}") assert col[0]['d'] == 3 col = NamedCollection(*BASIC_DATA, calculated_columns=(('d', '{a} + {b}',),)) assert col[1]['d'] == 9
def test_filter(): col = NamedCollection(*BASIC_DATA) col.filter(lambda x: x['a'] < 2) assert len(col) == 1 col = NamedCollection(*BASIC_DATA, filter=(lambda x: x['a'] < 2,)) assert len(col) == 1
def test_formatted_columns(): names, data = STRING_DATA col = NamedCollection(names, data) col.add_formatted_column('d', '{c} {a} {b}') assert list(col)[0]['d'] == 'baz foo bar' assert len(col.names) == 4 col = NamedCollection(names, data, formatted_columns=(('d', '{c} {b} {a}'),)) assert list(col)[0]['d'] == 'baz bar foo'
def test_group(): col_ref = NamedCollection(*GROUP_DATA) col1 = NamedCollection(*GROUP_DATA) col1.group(['a']) col2 = NamedCollection(*GROUP_DATA, group=['a']) for col in [col1, col2]: assert len(col) == 2 assert col[0]['a'] == 1 assert col[1]['a'] == 2 assert len(col[0].children) == 3 assert type(col) == type(col[0].children) assert set(col[0].children) <= set(col_ref) for row in col: assert len(row) == 2