def test_unflatten(): table1 = (('lines',), ('A',), (1,), (True,), ('C',), (7,), (False,), ('B',), (2,), (False,), ('C',), (9,)) expect1 = (('f0', 'f1', 'f2'), ('A', 1, True), ('C', 7, False), ('B', 2, False), ('C', 9, None)) actual1 = unflatten(table1, 'lines', 3) ieq(expect1, actual1) ieq(expect1, actual1)
def test_fromxml_2(): # initial data f = NamedTemporaryFile(delete=False) data = """<table> <tr> <td v='foo'/><td v='bar'/> </tr> <tr> <td v='a'/><td v='1'/> </tr> <tr> <td v='b'/><td v='2'/> </tr> <tr> <td v='c'/><td v='2'/> </tr> </table>""" f.write(data) f.close() print open(f.name).read() actual = fromxml(f.name, 'tr', 'td', 'v') print actual expect = (('foo', 'bar'), ('a', '1'), ('b', '2'), ('c', '2')) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
def test_fold(): t1 = (('id', 'count'), (1, 3), (1, 5), (2, 4), (2, 8)) t2 = fold(t1, 'id', operator.add, 'count', presorted=True) expect = (('key', 'value'), (1, 8), (2, 12)) ieq(expect, t2) ieq(expect, t2)
def test_addfieldusingcontext(): table1 = (('foo', 'bar'), ('A', 1), ('B', 4), ('C', 5), ('D', 9)) expect = (('foo', 'bar', 'baz', 'quux'), ('A', 1, None, 3), ('B', 4, 3, 1), ('C', 5, 1, 4), ('D', 9, 4, None)) def upstream(prv, cur, nxt): if prv is None: return None else: return cur.bar - prv.bar def downstream(prv, cur, nxt): if nxt is None: return None else: return nxt.bar - cur.bar table2 = addfieldusingcontext(table1, 'baz', upstream) table3 = addfieldusingcontext(table2, 'quux', downstream) ieq(expect, table3) ieq(expect, table3)
def test_fromxml_5(): # initial data f = NamedTemporaryFile(delete=False) data = """<table> <row> <foo>a</foo><baz><bar v='1'/><bar v='3'/></baz> </row> <row> <foo>b</foo><baz><bar v='2'/></baz> </row> <row> <foo>c</foo><baz><bar v='2'/></baz> </row> </table>""" f.write(data) f.close() actual = fromxml(f.name, 'row', {'foo': 'foo', 'bar': ('baz/bar', 'v')}) expect = (('foo', 'bar'), ('a', ('1', '3')), ('b', '2'), ('c', '2')) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
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, fields=('foo', 'bar')) ieq(expect, actual)
def test_tocsv_appendcsv_gz(): """Test the tocsv and appendcsv function.""" # exercise function table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2)) f = NamedTemporaryFile(delete=False) fn = f.name + ".gz" f.close() tocsv(table, fn, delimiter="\t") # check what it did with gzip.open(fn, "rb") as o: actual = csv.reader(o, delimiter="\t") expect = [["foo", "bar"], ["a", "1"], ["b", "2"], ["c", "2"]] ieq(expect, actual) # check appending table2 = (("foo", "bar"), ("d", 7), ("e", 9), ("f", 1)) appendcsv(table2, fn, delimiter="\t") # check what it did with gzip.open(fn, "rb") as o: actual = csv.reader(o, delimiter="\t") expect = [["foo", "bar"], ["a", "1"], ["b", "2"], ["c", "2"], ["d", "7"], ["e", "9"], ["f", "1"]] ieq(expect, actual)
def test_todb_appenddb_cursor(): f = NamedTemporaryFile(delete=False) conn = sqlite3.connect(f.name) conn.execute("create table foobar (foo, bar)") conn.commit() # exercise function table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2)) cursor = conn.cursor() todb(table, cursor, "foobar") # check what it did actual = conn.execute("select * from foobar") expect = (("a", 1), ("b", 2), ("c", 2)) ieq(expect, actual) # try appending table2 = (("foo", "bar"), ("d", 7), ("e", 9), ("f", 1)) appenddb(table2, cursor, "foobar") # check what it did actual = conn.execute("select * from foobar") expect = (("a", 1), ("b", 2), ("c", 2), ("d", 7), ("e", 9), ("f", 1)) ieq(expect, actual)
def test_fromxml(): # initial data f = NamedTemporaryFile(delete=False) data = """<table> <tr> <td>foo</td><td>bar</td> </tr> <tr> <td>a</td><td>1</td> </tr> <tr> <td>b</td><td>2</td> </tr> <tr> <td>c</td><td>2</td> </tr> </table>""" f.write(data) f.close() actual = fromxml(f.name, 'tr', 'td') expect = (('foo', 'bar'), ('a', '1'), ('b', '2'), ('c', '2')) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
def test_sort_4(): table = (("foo", "bar"), ("C", 2), ("A", 9), ("A", 6), ("F", 1), ("D", 10)) result = sort(table, "bar") expectation = (("foo", "bar"), ("F", 1), ("C", 2), ("A", 6), ("A", 9), ("D", 10)) ieq(expectation, result)
def test_records_shortrows(): """Test the records function on a table with short rows.""" table = (('foo', 'bar'), ('a', 1), ('b',)) actual = records(table) expect = ({'foo': 'a', 'bar': 1}, {'foo': 'b', 'bar': None}) ieq(expect, actual)
def test_records(): """Test the records function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = records(table) expect = ({'foo': 'a', 'bar': 1}, {'foo': 'b', 'bar': 2}) ieq(expect, actual)
def test_topickle_appendpickle(): """Test the topickle and appendpickle functions.""" # exercise function table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2)) f = NamedTemporaryFile(delete=False) topickle(table, f.name) def picklereader(file): try: while True: yield pickle.load(file) except EOFError: pass # check what it did with open(f.name, "rb") as o: actual = picklereader(o) ieq(table, actual) # check appending table2 = (("foo", "bar"), ("d", 7), ("e", 9), ("f", 1)) appendpickle(table2, f.name) # check what it did with open(f.name, "rb") as o: actual = picklereader(o) expect = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2), ("d", 7), ("e", 9), ("f", 1)) ieq(expect, actual)
def test_fromsqlite3(): """Test the fromsqlite3 function.""" # initial data f = NamedTemporaryFile(delete=False) data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(f.name) c = connection.cursor() c.execute('create table foobar (foo, bar)') for row in data: c.execute('insert into foobar values (?, ?)', row) connection.commit() c.close() connection.close() # test the function actual = fromsqlite3(f.name, 'select * from foobar') expect = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2.0)) print list(actual) ieq(expect, actual, cast=tuple) ieq(expect, actual, cast=tuple) # verify can iterate twice
def test_sort_2(): table = (('foo', 'bar'), ('C', '2'), ('A', '9'), ('A', '6'), ('F', '1'), ('D', '10')) result = sort(table, key=('foo', 'bar')) expectation = (('foo', 'bar'), ('A', '6'), ('A', '9'), ('C', '2'), ('D', '10'), ('F', '1')) ieq(expectation, result) result = sort(table) # default is lexical sort expectation = (('foo', 'bar'), ('A', '6'), ('A', '9'), ('C', '2'), ('D', '10'), ('F', '1')) ieq(expectation, result)
def test_unique(): table = ( ("foo", "bar", "baz"), ("A", 1, 2), ("B", "2", "3.4"), ("D", "xyz", 9.0), ("B", u"3", u"7.8", True), ("B", "2", 42), ("E", None), ("D", 4, 12.3), ) f1 = NamedTemporaryFile(delete=False) f2 = NamedTemporaryFile(delete=False) p = sort("foo") q = p.pipe(unique("foo")) q.pipe(topickle(f1.name)) q.pipe("remainder", topickle(f2.name)) p.push(table) expectation = (("foo", "bar", "baz"), ("A", 1, 2), ("E", None)) ieq(expectation, frompickle(f1.name)) exremainder = ( ("foo", "bar", "baz"), ("B", "2", "3.4"), ("B", u"3", u"7.8", True), ("B", "2", 42), ("D", "xyz", 9.0), ("D", 4, 12.3), ) ieq(exremainder, frompickle(f2.name))
def test_fromdb_mkcursor(): # initial data data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(':memory:') c = connection.cursor() c.execute('create table foobar (foo, bar)') for row in data: c.execute('insert into foobar values (?, ?)', row) connection.commit() c.close() # test the function mkcursor = lambda: connection.cursor() actual = fromdb(mkcursor, 'select * from foobar') expect = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice # test iterators are isolated i1 = iter(actual) i2 = iter(actual) eq_(('foo', 'bar'), i1.next()) eq_(('a', 1), i1.next()) eq_(('foo', 'bar'), i2.next()) eq_(('b', 2), i1.next())
def test_sort_none(): table = (('foo', 'bar'), ('C', 2), ('A', 9), ('A', None), ('F', 1), ('D', 10)) result = sort(table, 'bar') expectation = (('foo', 'bar'), ('A', None), ('F', 1), ('C', 2), ('A', 9), ('D', 10)) ieq(expectation, result) dt = datetime.now().replace table = (('foo', 'bar'), ('C', dt(hour=5)), ('A', dt(hour=1)), ('A', None), ('F', dt(hour=9)), ('D', dt(hour=17))) result = sort(table, 'bar') expectation = (('foo', 'bar'), ('A', None), ('A', dt(hour=1)), ('C', dt(hour=5)), ('F', dt(hour=9)), ('D', dt(hour=17))) ieq(expectation, result)
def test_skip(): table1 = (("#aaa", "bbb", "ccc"), ("#mmm",), ("foo", "bar"), ("a", 1), ("b", 2)) table2 = skip(table1, 2) expect2 = (("foo", "bar"), ("a", 1), ("b", 2)) ieq(expect2, table2) ieq(expect2, table2) # can iterate twice?
def test_fromdb_withargs(): # initial data data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(':memory:') c = connection.cursor() c.execute('create table foobar (foo, bar)') for row in data: c.execute('insert into foobar values (?, ?)', row) connection.commit() c.close() # test the function actual = fromdb( connection, 'select * from foobar where bar > ? and bar < ?', (1, 3) ) expect = (('foo', 'bar'), ('b', 2), ('c', 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
def test_data(): """Test the data function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = data(table) expect = (('a', 1), ('b', 2)) ieq(expect, actual)
def test_sort_3(): table = (("foo", "bar"), ("C", "2"), ("A", "9"), ("A", "6"), ("F", "1"), ("D", "10")) result = sort(table, "bar") expectation = (("foo", "bar"), ("F", "1"), ("D", "10"), ("C", "2"), ("A", "6"), ("A", "9")) ieq(expectation, result)
def test_fromcsv_gz(): """Test the fromcsv function on a gzipped file.""" f = NamedTemporaryFile(delete=False) f.close() fn = f.name + '.gz' os.rename(f.name, fn) fz = gzip.open(fn, 'wb') writer = csv.writer(fz, delimiter='\t') table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) for row in table: writer.writerow(row) fz.close() actual = fromcsv(fn, delimiter='\t') expect = (('foo', 'bar'), ('a', '1'), ('b', '2'), ('c', '2')) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
def test_tosqlite3_appendsqlite3_connection(): conn = sqlite3.connect(':memory:') # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) tosqlite3(table, conn, 'foobar', create=True) # check what it did actual = conn.execute('select * from foobar') expect = (('a', 1), ('b', 2), ('c', 2)) ieq(expect, actual) # check appending table2 = (('foo', 'bar'), ('d', 7), ('e', 9), ('f', 1)) appendsqlite3(table2, conn, 'foobar') # check what it did actual = conn.execute('select * from foobar') expect = (('a', 1), ('b', 2), ('c', 2), ('d', 7), ('e', 9), ('f', 1)) ieq(expect, actual)
def test_recordmapmany(): table = (('id', 'sex', 'age', 'height', 'weight'), (1, 'male', 16, 1.45, 62.0), (2, 'female', 19, 1.34, 55.4), (3, '-', 17, 1.78, 74.4), (4, 'male', 21, 1.33)) def rowgenerator(rec): transmf = {'male': 'M', 'female': 'F'} yield [rec['id'], 'gender', transmf[rec['sex']] if rec['sex'] in transmf else rec['sex']] yield [rec['id'], 'age_months', rec['age'] * 12] yield [rec['id'], 'bmi', rec['weight'] / rec['height'] ** 2] actual = rowmapmany(table, rowgenerator, fields=['subject_id', 'variable', 'value']) expect = (('subject_id', 'variable', 'value'), (1, 'gender', 'M'), (1, 'age_months', 16*12), (1, 'bmi', 62.0/1.45**2), (2, 'gender', 'F'), (2, 'age_months', 19*12), (2, 'bmi', 55.4/1.34**2), (3, 'gender', '-'), (3, 'age_months', 17*12), (3, 'bmi', 74.4/1.78**2), (4, 'gender', 'M'), (4, 'age_months', 21*12)) ieq(expect, actual) ieq(expect, actual) # can iteratate twice?
def test_pushheader(): table1 = (("a", 1), ("b", 2)) table2 = pushheader(table1, ["foo", "bar"]) expect2 = (("foo", "bar"), ("a", 1), ("b", 2)) ieq(expect2, table2) ieq(expect2, table2) # can iterate twice?
def test_extendheader(): table1 = (("foo",), ("a", 1, True), ("b", 2, False)) table2 = extendheader(table1, ["bar", "baz"]) expect2 = (("foo", "bar", "baz"), ("a", 1, True), ("b", 2, False)) ieq(expect2, table2) ieq(expect2, table2) # can iterate twice?
def test_operator_overload(): table = ( ("foo", "bar", "baz"), ("A", 1, 2), ("B", "2", "3.4"), ("D", "xyz", 9.0), ("B", u"3", u"7.8", True), ("B", "2", 42), ("E", None), ("D", 4, 12.3), ) f1 = NamedTemporaryFile(delete=False) p = sort("foo") p | duplicates("foo") | topickle(f1.name) p.push(table) expectation = ( ("foo", "bar", "baz"), ("B", "2", "3.4"), ("B", u"3", u"7.8", True), ("B", "2", 42), ("D", "xyz", 9.0), ("D", 4, 12.3), ) ieq(expectation, frompickle(f1.name))
def test_duplicates(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), ('D', 'xyz', 9.0), ('B', u'3', u'7.8', True), ('B', '2', 42), ('E', None), ('D', 4, 12.3)) result = duplicates(table, 'foo') expectation = (('foo', 'bar', 'baz'), ('B', '2', '3.4'), ('B', u'3', u'7.8', True), ('B', '2', 42), ('D', 'xyz', 9.0), ('D', 4, 12.3)) ieq(expectation, result) # test with compound key result = duplicates(table, key=('foo', 'bar')) expectation = (('foo', 'bar', 'baz'), ('B', '2', '3.4'), ('B', '2', 42)) ieq(expectation, result)
def test_unflatten_empty(): table1 = (('lines',),) expect1 = (('f0', 'f1', 'f2'),) actual1 = unflatten(table1, 'lines', 3) print list(actual1) ieq(expect1, actual1)
def test_convert_with_row(): table = (('foo', 'bar'), ('a', 1), ('b', 2)) expect = (('foo', 'bar'), ('a', 'A'), ('b', 'B')) actual = convert(table, 'bar', lambda v, row: row.foo.upper()) ieq(expect, actual)
def test_integration_fluent(): import petl.fluent as etl import petlx.xlsx tbl = etl.fromxlsx('fixture/test.xlsx', 'Sheet1') expect = (('foo', 'bar'), ('A', 1), ('B', 2), ('C', 2), (u'é', datetime(2012, 1, 1))) ieq(expect, tbl) ieq(expect, tbl)
def test_parsecounts(): table = (('foo', 'bar', 'baz'), ('A', 'aaa', 2), ('B', u'2', '3.4'), (u'B', u'3', u'7.8', True), ('D', '3.7', 9.0), ('E', 42)) actual = parsecounts(table, 'bar') expect = (('type', 'count', 'errors'), ('float', 3, 1), ('int', 2, 2)) ieq(expect, actual)
def _test_antijoin_empty(antijoin_impl): table1 = (('id', 'colour'), (0, 'black'), (1, 'blue'), (2, 'red'), (4, 'yellow'), (5, 'white')) table2 = (('id', 'shape'), ) actual = antijoin_impl(table1, table2, key='id') expect = table1 ieq(expect, actual)
def test_convert_translate(): table = (('foo', 'bar'), ('M', 12), ('F', 34), ('-', 56)) trans = {'M': 'male', 'F': 'female'} result = convert(table, 'foo', trans) expectation = (('foo', 'bar'), ('male', 12), ('female', 34), ('-', 56)) ieq(expectation, result)
def test_sort_4(): table = (('foo', 'bar'), ('C', 2), ('A', 9), ('A', 6), ('F', 1), ('D', 10)) result = sort(table, 'bar') expectation = (('foo', 'bar'), ('F', 1), ('C', 2), ('A', 6), ('A', 9), ('D', 10)) ieq(expectation, result)
def test_convert_with_row_backwards_compat(): table = (('foo', 'bar'), (' a ', 1), (' b ', 2)) expect = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = convert(table, 'foo', string.strip) ieq(expect, actual)
def test_skip(): table1 = (('#aaa', 'bbb', 'ccc'), ('#mmm', ), ('foo', 'bar'), ('a', 1), ('b', 2)) table2 = skip(table1, 2) expect2 = (('foo', 'bar'), ('a', 1), ('b', 2)) ieq(expect2, table2) ieq(expect2, table2) # can iterate twice?
def test_skip_empty(): table1 = (('#aaa', 'bbb', 'ccc'), ('#mmm',), ('foo', 'bar')) table2 = skip(table1, 2) expect2 = (('foo', 'bar'),) ieq(expect2, table2)
def test_transpose(): table1 = (('id', 'colour'), (1, 'blue'), (2, 'red'), (3, 'purple'), (5, 'yellow'), (7, 'orange')) table2 = transpose(table1) expect2 = (('id', 1, 2, 3, 5, 7), ('colour', 'blue', 'red', 'purple', 'yellow', 'orange')) ieq(expect2, table2) ieq(expect2, table2)
def test_PopenSource(): expect = (('foo', 'bar'), ('a', '1')) actual = fromcsv(PopenSource(r'echo -e foo bar\\na 1', shell=True, executable='/bin/bash'), delimiter=' ') ieq(expect, actual)
def test_duplicates_wholerow(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), ('B', '2', '3.4'), ('D', 4, 12.3)) result = duplicates(table) expectation = (('foo', 'bar', 'baz'), ('B', '2', '3.4'), ('B', '2', '3.4')) ieq(expectation, result)
def test_unique_wholerow(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), ('B', '2', '3.4'), ('D', 4, 12.3)) result = unique(table) expectation = (('foo', 'bar', 'baz'), ('A', 1, 2), ('D', 4, 12.3)) ieq(expectation, result)
def test_valuecounts(): """Test the valuecounts function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 7)) actual = valuecounts(table, 'foo') expect = (('foo', 'count', 'frequency'), ('b', 2, 2./3), ('a', 1, 1./3)) ieq(expect, actual) ieq(expect, actual)
def test_valuecounts_shortrows(): table = (('foo', 'bar'), ('a', True), ('x', True), ('b', ), ('b', True), ('c', False), ('z', False)) actual = valuecounts(table, 'bar') expect = (('bar', 'count', 'frequency'), (True, 3, 3. / 6), (False, 2, 2. / 6), (None, 1, 1. / 6)) ieq(expect, actual) ieq(expect, actual)
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, fields=('foo', 'bar')) ieq(expect, actual)
def test_crossjoin_prefix(): table1 = (('id', 'colour'), (1, 'blue'), (2, 'red')) table2 = (('id', 'shape'), (1, 'circle'), (3, 'square')) table3 = crossjoin(table1, table2, prefix=True) expect3 = (('1_id', '1_colour', '2_id', '2_shape'), (1, 'blue', 1, 'circle'), (1, 'blue', 3, 'square'), (2, 'red', 1, 'circle'), (2, 'red', 3, 'square')) ieq(expect3, table3)
def test_fieldmap_empty(): table = (('foo', 'bar'),) expect = (('foo', 'baz'),) mappings = OrderedDict() mappings['foo'] = 'foo' mappings['baz'] = 'bar', lambda v: v*2 actual = fieldmap(table, mappings) ieq(expect, actual)
def test_recordselect(): table = (('foo', 'bar', 'baz'), ('a', 4, 9.3), ('a', 2, 88.2), ('b', 1, 23.3), ('c', 8, 42.0), ('d', 7, 100.9), ('c', 2)) actual = rowselect(table, lambda rec: rec['foo'] == 'a') expect = (('foo', 'bar', 'baz'), ('a', 4, 9.3), ('a', 2, 88.2)) ieq(expect, actual) ieq(expect, actual) # check can iterate twice
def test_selectin(): table = (('foo', 'bar', 'baz'), ('a', 4, 9.3), ('a', 2, 88.2), ('b', 1, 23.3), ('c', 8, 42.0), ('d', 7, 100.9), ('c', 2)) actual = selectin(table, 'foo', ['a', 'x', 'y']) expect = (('foo', 'bar', 'baz'), ('a', 4, 9.3), ('a', 2, 88.2)) ieq(expect, actual) ieq(expect, actual) # check can iterate twice
def test_selectre(): table = (('foo', 'bar', 'baz'), ('aa', 4, 9.3), ('aaa', 2, 88.2), ('b', 1, 23.3), ('ccc', 8, 42.0), ('bb', 7, 100.9), ('c', 2)) actual = selectre(table, 'foo', '[ab]{2}') expect = (('foo', 'bar', 'baz'), ('aa', 4, 9.3), ('aaa', 2, 88.2), ('bb', 7, 100.9)) ieq(expect, actual) ieq(expect, actual)
def _test_rightjoin_lrkey(rightjoin_impl): table1 = (('id', 'colour'), (1, 'blue'), (2, 'red'), (3, 'purple')) table2 = (('identifier', 'shape'), (0, 'triangle'), (1, 'circle'), (3, 'square'), (4, 'ellipse'), (5, 'pentagon')) table3 = rightjoin_impl(table1, table2, lkey='id', rkey='identifier') expect3 = (('id', 'colour', 'shape'), (0, None, 'triangle'), (1, 'blue', 'circle'), (3, 'purple', 'square'), (4, None, 'ellipse'), (5, None, 'pentagon')) ieq(expect3, table3)
def test_outerjoin_fieldorder(): table1 = (('colour', 'id'), ('blue', 1), ('red', 2), ('purple', 3)) table2 = (('id', 'shape'), (1, 'circle'), (3, 'square'), (4, 'ellipse')) table3 = outerjoin(table1, table2, key='id') expect3 = (('colour', 'id', 'shape'), ('blue', 1, 'circle'), ('red', 2, None), ('purple', 3, 'square'), (None, 4, 'ellipse')) ieq(expect3, table3) ieq(expect3, table3) # check twice
def test_crossjoin(): table1 = (('id', 'colour'), (1, 'blue'), (2, 'red')) table2 = (('id', 'shape'), (1, 'circle'), (3, 'square')) table3 = crossjoin(table1, table2) expect3 = (('id', 'colour', 'id', 'shape'), (1, 'blue', 1, 'circle'), (1, 'blue', 3, 'square'), (2, 'red', 1, 'circle'), (2, 'red', 3, 'square')) ieq(expect3, table3)
def test_valuecounts_multifields(): table = (('foo', 'bar', 'baz'), ('a', True, .12), ('a', True, .17), ('b', False, .34), ('b', False, .44), ('b', ), ('b', False, .56)) actual = valuecounts(table, 'foo', 'bar') expect = (('foo', 'bar', 'count', 'frequency'), ('b', False, 3, 3. / 6), ('a', True, 2, 2. / 6), ('b', None, 1, 1. / 6)) ieq(expect, actual) ieq(expect, actual)
def test_distinct(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), ('B', '2', '3.4'), ('D', 4, 12.3)) result = distinct(table) expect = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), ('D', 4, 12.3)) ieq(expect, result)
def test_rowlengths(): """Test the rowlengths function.""" table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), (u'B', u'3', u'7.8', True), ('D', 'xyz', 9.0), ('E', None), ('F', 9)) actual = rowlengths(table) expect = (('length', 'count'), (3, 3), (2, 2), (4, 1)) ieq(expect, actual)
def test_distinct_count(): table = (('foo', 'bar', 'baz'), ('A', 1, 2), ('B', '2', '3.4'), ('B', '2', '3.4'), ('D', 4, 12.3)) result = distinct(table, count='count') expect = (('foo', 'bar', 'baz', 'count'), ('A', 1, 2, 1), ('B', '2', '3.4', 2), ('D', 4, 12.3, 1)) ieq(expect, result)
def test_skipcomments_empty(): table1 = (('##aaa', 'bbb', 'ccc'), ('##mmm',), ('#foo', 'bar'), ('##nnn', 1)) table2 = skipcomments(table1, '##') expect2 = (('#foo', 'bar'),) ieq(expect2, table2)
def _test_antijoin_lrkey(antijoin_impl): table1 = (('id', 'colour'), (0, 'black'), (1, 'blue'), (2, 'red'), (4, 'yellow'), (5, 'white')) table2 = (('identifier', 'shape'), (1, 'circle'), (3, 'square')) table3 = antijoin_impl(table1, table2, lkey='id', rkey='identifier') expect3 = (('id', 'colour'), (0, 'black'), (2, 'red'), (4, 'yellow'), (5, 'white')) ieq(expect3, table3)