def test_pretty_table_printer(): # Functional equivalence to # https://pypi.org/project/pretty-table-printer # https://github.com/cfmeyers/pretty_table_printer # (tests/test_pretty_table_printer.py # test_it_knows_how_to_print_itself) # # delta: # * KVITable doesn't have explicit field widths/truncation (implicit in pretty_table_printer?) # * KVITable default alignment is right instead of left # * KVITable doesn't show a bottom border kvit = KVITable(valuecol_name='name') kvit.add('Sam', id=1) kvit.add('Layla', id=2) kvit.add('Jack Gabriel', id=3) rows = kvit.get_rows() assert [ [ 1, 'Sam' ], [ 2, 'Layla' ], [ 3, 'Jack Gabriel' ], ] == rows show = kvit.render() assert '\n'.join([ '| id | name |', '+----+--------------+', '| 1 | Sam |', '| 2 | Layla |', '| 3 | Jack Gabriel |', ]) == show
def test_non_leaf_kvitable_add_uses_default_key_val(): kvit = KVITable() kvit.add("hi", ('moo', "cow"), foo='bar') kvit.add("oops", ('moo', "dog")) rows = kvit.get_rows() assert [ ['cow', 'bar', 'hi'], ['dog', '', 'oops'], ] == rows
def test_non_frozen_kvitable_add_key(): kvit = KVITable({'foo':['bar','baz']}, kv_frozen=False) kvit.add("hi", foo='bar') kvit.add("yo", foo='baz', dog='woof') rows = kvit.get_rows() assert [ [ 'bar', '', 'hi' ], [ 'baz', 'woof', 'yo'], ] == rows show = kvit.render() assert '\n'.join([ '| foo | dog | Value |', '+-----+------+-------+', '| bar | | hi |', '| baz | woof | yo |', ]) == show
def test_non_frozen_kvitable_add_deep_key(): kvit = KVITable({'foo':['bar','baz'], 'moon':['beam', 'pie'], }, kv_frozen=False, valuecol_name='says', keyval_factory=lambda key: '?') kvit.add("hi", foo='bar', moon='pie') kvit.add("yo", foo='baz', moon='beam', dog='woof') kvit.add("Excellent!", foo='Bill', moon='Ted', dog='arf arf') rows = kvit.get_rows() assert [ [ 'Bill', 'Ted', 'arf arf', 'Excellent!'], [ 'bar', 'pie', '?', 'hi' ], [ 'baz', 'beam', 'woof', 'yo'], ] == rows show = kvit.render(sort_vals=True) assert '\n'.join([ '| foo | moon | dog | says |', '+------+------+---------+------------+', '| Bill | Ted | arf arf | Excellent! |', '| bar | pie | ? | hi |', '| baz | beam | woof | yo |', ]) == show