def test_rowreduce_empty(): table = (('foo', 'bar'),) expect = (('foo', 'bar'),) reducer = lambda key, rows: (key, [r[0] for r in rows]) actual = rowreduce(table, key='foo', reducer=reducer, header=('foo', 'bar')) ieq(expect, actual)
def test_rowreduce(): table1 = (('foo', 'bar'), ('a', 3), ('a', 7), ('b', 2), ('b', 1), ('b', 9), ('c', 4)) def sumbar(key, rows): return [key, sum(row[1] for row in rows)] table2 = rowreduce(table1, key='foo', reducer=sumbar, fields=['foo', 'barsum']) expect2 = (('foo', 'barsum'), ('a', 10), ('b', 12), ('c', 4)) ieq(expect2, table2)
def test_rowreduce_more(): table1 = (('foo', 'bar'), ('aa', 3), ('aa', 7), ('bb', 2), ('bb', 1), ('bb', 9), ('cc', 4)) def sumbar(key, records): return [key, sum(rec['bar'] for rec in records)] table2 = rowreduce(table1, key='foo', reducer=sumbar, header=['foo', 'barsum']) expect2 = (('foo', 'barsum'), ('aa', 10), ('bb', 12), ('cc', 4)) ieq(expect2, table2)
def test_rowreduce(): table1 = (('foo', 'bar'), ('a', 3), ('a', 7), ('b', 2), ('b', 1), ('b', 9), ('c', 4)) def sumbar(key, rows): return [key, sum(row[1] for row in rows)] table2 = rowreduce(table1, key='foo', reducer=sumbar, header=['foo', 'barsum']) expect2 = (('foo', 'barsum'), ('a', 10), ('b', 12), ('c', 4)) ieq(expect2, table2)
def test_rowreduce_fieldnameaccess(): table1 = (('foo', 'bar'), ('a', 3), ('a', 7), ('b', 2), ('b', 1), ('b', 9), ('c', 4)) def sumbar(key, records): return [key, sum([rec['bar'] for rec in records])] table2 = rowreduce(table1, key='foo', reducer=sumbar, fields=['foo', 'barsum']) expect2 = (('foo', 'barsum'), ('a', 10), ('b', 12), ('c', 4)) ieq(expect2, table2)