def test_totext(): # exercise function table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2)) f = NamedTemporaryFile(delete=False) prologue = """{| class="wikitable" |- ! foo ! bar """ template = """|- | {foo} | {bar} """ epilogue = "|}" totext(table, f.name, template, prologue, epilogue) # check what it did with open(f.name, "rb") as o: actual = o.read() expect = """{| class="wikitable" |- ! foo ! bar |- | a | 1 |- | b | 2 |- | c | 2 |}""" assertequal(expect, actual)
def test_totext(): # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) f = NamedTemporaryFile(delete=False) prologue = """{| class="wikitable" |- ! foo ! bar """ template = """|- | {foo} | {bar} """ epilogue = "|}" totext(table, f.name, template, prologue, epilogue) # check what it did with open(f.name, 'rb') as o: actual = o.read() expect = """{| class="wikitable" |- ! foo ! bar |- | a | 1 |- | b | 2 |- | c | 2 |}""" assertequal(expect, actual)
def test_valuecount(): """Test the valuecount function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 7)) n, f = valuecount(table, 'foo', 'b') assertequal(2, n) assertequal(2. / 3, f)
def test_valuecounter(): """Test the valuecounter function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 7)) actual = valuecounter(table, 'foo') expect = {'b': 2, 'a': 1} assertequal(expect, actual)
def test_valuecount(): """Test the valuecount function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 7)) n, f = valuecount(table, 'foo', 'b') assertequal(2, n) assertequal(2./3, f)
def test_rowcount(): """Test the rowcount function.""" table = (('foo', 'bar'), ('a', 1), ('b',)) actual = rowcount(table) expect = 2 assertequal(expect, actual)
def test_recordlookupone(): """Test the recordlookupone function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) try: recordlookupone(t1, 'foo', strict=True) except DuplicateKeyError: pass # expected else: assert False, 'expected error' # relax actual = recordlookupone(t1, 'foo', strict=False) expect = {'a': {'foo': 'a', 'bar': 1}, 'b': {'foo': 'b', 'bar': 2}} # first wins assertequal(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test compound key actual = recordlookupone(t2, ('foo', 'bar'), strict=False) expect = {('a', 1): {'foo': 'a', 'bar': 1, 'baz': True}, ('b', 2): {'foo': 'b', 'bar': 2, 'baz': False}, ('b', 3): {'foo': 'b', 'bar': 3, 'baz': True}} # first wins assertequal(expect, actual)
def test_header(): """Test the header function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = header(table) expect = ('foo', 'bar') assertequal(expect, actual)
def test_rowcount(): """Test the rowcount function.""" table = (('foo', 'bar'), ('a', 1), ('b', )) actual = rowcount(table) expect = 2 assertequal(expect, actual)
def test_typeset(): table = (('foo', 'bar', 'baz'), ('A', 1, '2'), ('B', u'2', '3.4'), (u'B', u'3', '7.8', True), ('D', u'xyz', 9.0), ('E', 42)) actual = typeset(table, 'foo') expect = {str, unicode} assertequal(expect, actual)
def test_diffheaders(): table1 = (('foo', 'bar', 'baz'), ('a', 1, .3)) table2 = (('baz', 'bar', 'quux'), ('a', 1, .3)) add, sub = diffheaders(table1, table2) assertequal({'quux'}, add) assertequal({'foo'}, sub)
def test_diffvalues(): table1 = (('foo', 'bar'), ('a', 1), ('b', 3)) table2 = (('bar', 'foo'), (1, 'a'), (3, 'c')) add, sub = diffvalues(table1, table2, 'foo') assertequal({'c'}, add) assertequal({'b'}, sub)
def test_see(): """Test the see function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = repr(see(table)) expect = """'foo': 'a', 'b' 'bar': 1, 2 """ assertequal(expect, actual)
def test_valueset(): """Test the valueset function.""" table = (('foo', 'bar'), ('a', True), ('x', True), ('b', ), ('b', True), ('c', False), ('z', False)) actual = valueset(table, 'foo') expect = {'a', 'b', 'c', 'x', 'z'} assertequal(expect, actual) actual = valueset(table, 'bar') expect = {True, False, None} assertequal(expect, actual)
def test_lookup(): """Test the lookup function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) # lookup one column on another actual = lookup(t1, 'foo', 'bar') expect = {'a': [1], 'b': [2, 3]} assertequal(expect, actual) # test default value - tuple of whole row actual = lookup(t1, 'foo') # no value selector expect = {'a': [('a', 1)], 'b': [('b', 2), ('b', 3)]} assertequal(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test value selection actual = lookup(t2, 'foo', ('bar', 'baz')) expect = {'a': [(1, True)], 'b': [(2, False), (3, True), (3, False)]} assertequal(expect, actual) # test compound key actual = lookup(t2, ('foo', 'bar'), 'baz') expect = {('a', 1): [True], ('b', 2): [False], ('b', 3): [True, False]} assertequal(expect, actual)
def test_look(): """Test the look function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = repr(look(table)) expect = """+-------+-------+ | 'foo' | 'bar' | +=======+=======+ | 'a' | 1 | +-------+-------+ | 'b' | 2 | +-------+-------+ """ assertequal(expect, actual)
def test_look_irregular_rows(): """Test the look function with a table where row lengths are irregular.""" table = (('foo', 'bar'), ('a',), ('b', 2, True)) actual = repr(look(table)) expect = """+-------+-------+------+ | 'foo' | 'bar' | | +=======+=======+======+ | 'a' | | | +-------+-------+------+ | 'b' | 2 | True | +-------+-------+------+ """ assertequal(expect, actual)
def test_look_irregular_rows(): """Test the look function with a table where row lengths are irregular.""" table = (('foo', 'bar'), ('a', ), ('b', 2, True)) actual = repr(look(table)) expect = """+-------+-------+------+ | 'foo' | 'bar' | | +=======+=======+======+ | 'a' | | | +-------+-------+------+ | 'b' | 2 | True | +-------+-------+------+ """ assertequal(expect, actual)
def test_lookupone(): """Test the lookupone function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) # lookup one column on another under strict mode try: lookupone(t1, 'foo', 'bar', strict=True) except DuplicateKeyError: pass # expected else: assert False, 'expected error' # lookup one column on another under, not strict actual = lookupone(t1, 'foo', 'bar', strict=False) expect = {'a': 1, 'b': 2} # first value wins assertequal(expect, actual) # test default value - tuple of whole row actual = lookupone(t1, 'foo', strict=False) # no value selector expect = {'a': ('a', 1), 'b': ('b', 2)} # first wins assertequal(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test value selection actual = lookupone(t2, 'foo', ('bar', 'baz'), strict=False) expect = {'a': (1, True), 'b': (2, False)} assertequal(expect, actual) # test compound key actual = lookupone(t2, ('foo', 'bar'), 'baz', strict=False) expect = {('a', 1): True, ('b', 2): False, ('b', 3): True} # first wins assertequal(expect, actual)
def test_recordlookupone(): """Test the recordlookupone function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) try: recordlookupone(t1, 'foo', strict=True) except DuplicateKeyError: pass # expected else: assert False, 'expected error' # relax actual = recordlookupone(t1, 'foo', strict=False) expect = { 'a': { 'foo': 'a', 'bar': 1 }, 'b': { 'foo': 'b', 'bar': 2 } } # first wins assertequal(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test compound key actual = recordlookupone(t2, ('foo', 'bar'), strict=False) expect = { ('a', 1): { 'foo': 'a', 'bar': 1, 'baz': True }, ('b', 2): { 'foo': 'b', 'bar': 2, 'baz': False }, ('b', 3): { 'foo': 'b', 'bar': 3, 'baz': True } } # first wins assertequal(expect, actual)
def test_recordlookup(): """Test the recordlookup function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) actual = recordlookup(t1, 'foo') expect = { 'a': [{ 'foo': 'a', 'bar': 1 }], 'b': [{ 'foo': 'b', 'bar': 2 }, { 'foo': 'b', 'bar': 3 }] } assertequal(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test compound key actual = recordlookup(t2, ('foo', 'bar')) expect = { ('a', 1): [{ 'foo': 'a', 'bar': 1, 'baz': True }], ('b', 2): [{ 'foo': 'b', 'bar': 2, 'baz': False }], ('b', 3): [{ 'foo': 'b', 'bar': 3, 'baz': True }, { 'foo': 'b', 'bar': 3, 'baz': False }] } assertequal(expect, actual)
def test_laxparsers(): p1 = datetimeparser('%Y-%m-%dT%H:%M:%S') try: v = p1('2002-12-25 00:00:00') except: pass else: assert False, 'expected exception' p2 = datetimeparser('%Y-%m-%dT%H:%M:%S', strict=False) try: v = p2('2002-12-25 00:00:00') except: assert False, 'did not expect exception' else: assertequal('2002-12-25 00:00:00', v)
def test_valueset(): """Test the valueset function.""" table = (('foo', 'bar'), ('a', True), ('x', True), ('b',), ('b', True), ('c', False), ('z', False)) actual = valueset(table, 'foo') expect = {'a', 'b', 'c', 'x', 'z'} assertequal(expect, actual) actual = valueset(table, 'bar') expect = {True, False, None} assertequal(expect, actual)
def test_totext_gz(): # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) f = NamedTemporaryFile(delete=False) fn = f.name + '.gz' f.close() os.rename(f.name, fn) prologue = """{| class="wikitable" |- ! foo ! bar """ template = """|- | {foo} | {bar} """ epilogue = "|}" totext(table, fn, template, prologue, epilogue) # check what it did with gzip.open(fn, 'rb') as o: actual = o.read() expect = """{| class="wikitable" |- ! foo ! bar |- | a | 1 |- | b | 2 |- | c | 2 |}""" assertequal(expect, actual)
def test_recordlookup(): """Test the recordlookup function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) actual = recordlookup(t1, 'foo') expect = {'a': [{'foo': 'a', 'bar': 1}], 'b': [{'foo': 'b', 'bar': 2}, {'foo': 'b', 'bar': 3}]} assertequal(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test compound key actual = recordlookup(t2, ('foo', 'bar')) expect = {('a', 1): [{'foo': 'a', 'bar': 1, 'baz': True}], ('b', 2): [{'foo': 'b', 'bar': 2, 'baz': False}], ('b', 3): [{'foo': 'b', 'bar': 3, 'baz': True}, {'foo': 'b', 'bar': 3, 'baz': False}]} assertequal(expect, actual)
def test_fieldnames(): """Test the fieldnames function.""" table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = fieldnames(table) expect = ['foo', 'bar'] assertequal(expect, actual) class CustomField(object): def __init__(self, key, description): self.key = key self.description = description def __str__(self): return self.key def __repr__(self): return 'CustomField(%r, %r)' % (self.key, self.description) table = ((CustomField('foo', 'Get some foo.'), CustomField('bar', 'A lot of bar.')), ('a', 1), ('b', 2)) actual = fieldnames(table) expect = ['foo', 'bar'] assertequal(expect, actual)
def test_intervallookupone(): table = (('start', 'stop', 'value'), (1, 4, 'foo'), (3, 7, 'bar'), (4, 9, 'baz')) lkp = intervallookupone(table, 'start', 'stop', value='value') actual = lkp.find(1, 2) expect = 'foo' assertequal(expect, actual) try: actual = lkp.find(2, 4) except DuplicateKeyError: pass else: assert False, 'expected error' try: actual = lkp.find(2, 5) except DuplicateKeyError: pass else: assert False, 'expected error' try: actual = lkp.find(4, 5) except DuplicateKeyError: pass else: assert False, 'expected error' try: actual = lkp.find(5, 7) except DuplicateKeyError: pass else: assert False, 'expected error' actual = lkp.find(8, 9) expect = 'baz' assertequal(expect, actual) actual = lkp.find(9, 14) expect = None assertequal(expect, actual) actual = lkp.find(19, 140) expect = None assertequal(expect, actual) actual = lkp.find(1) expect = None assertequal(expect, actual) actual = lkp.find(2) expect = 'foo' assertequal(expect, actual) actual = lkp.find(4) expect = 'bar' assertequal(expect, actual) try: actual = lkp.find(5) except DuplicateKeyError: pass else: assert False, 'expected error' actual = lkp.find(8) expect = 'baz' assertequal(expect, actual)
def test_facetintervallookupone(): table = (('type', 'start', 'stop', 'value'), ('apple', 1, 4, 'foo'), ('apple', 3, 7, 'bar'), ('orange', 4, 9, 'baz')) lkp = facetintervallookupone(table, facet='type', start='start', stop='stop', value='value') actual = lkp['apple'].find(1, 2) expect = 'foo' assertequal(expect, actual) try: actual = lkp['apple'].find(2, 4) except DuplicateKeyError: pass else: assert False, 'expected error' try: actual = lkp['apple'].find(2, 5) except DuplicateKeyError: pass else: assert False, 'expected error' actual = lkp['apple'].find(4, 5) expect = 'bar' assertequal(expect, actual) actual = lkp['orange'].find(4, 5) expect = 'baz' assertequal(expect, actual) actual = lkp['apple'].find(5, 7) expect = 'bar' assertequal(expect, actual) actual = lkp['orange'].find(5, 7) expect = 'baz' assertequal(expect, actual) actual = lkp['apple'].find(8, 9) expect = None assertequal(expect, actual) actual = lkp['orange'].find(8, 9) expect = 'baz' assertequal(expect, actual) actual = lkp['orange'].find(9, 14) expect = None assertequal(expect, actual) actual = lkp['orange'].find(19, 140) expect = None assertequal(expect, actual) actual = lkp['apple'].find(1) expect = None assertequal(expect, actual) actual = lkp['apple'].find(2) expect = 'foo' assertequal(expect, actual) actual = lkp['apple'].find(4) expect = 'bar' assertequal(expect, actual) actual = lkp['apple'].find(5) expect = 'bar' assertequal(expect, actual) actual = lkp['orange'].find(5) expect = 'baz' assertequal(expect, actual) actual = lkp['apple'].find(8) expect = None assertequal(expect, actual) actual = lkp['orange'].find(8) expect = 'baz' assertequal(expect, actual)
def test_facetcolumns(): table = [['foo', 'bar', 'baz'], ['a', 1, True], ['b', 2, True], ['b', 3]] fc = facetcolumns(table, 'foo') assertequal(['a'], fc['a']['foo']) assertequal([1], fc['a']['bar']) assertequal([True], fc['a']['baz']) assertequal(['b', 'b'], fc['b']['foo']) assertequal([2, 3], fc['b']['bar']) assertequal([True, None], fc['b']['baz'])
def test_columns(): table = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]] cols = columns(table) assertequal(['a', 'b', 'b'], cols['foo']) assertequal([1, 2, 3], cols['bar'])
def test_intervallookupone_2(): table = (('start', 'stop', 'value'), (1, 4, 'foo'), (3, 7, 'bar'), (4, 9, 'baz')) lkp = intervallookupone(table, 'start', 'stop', value='value', strict=False) actual = lkp.find(1, 2) expect = 'foo' assertequal(expect, actual) actual = lkp.find(2, 4) expect = 'foo' assertequal(expect, actual) actual = lkp.find(2, 5) expect = 'foo' assertequal(expect, actual) actual = lkp.find(4, 5) expect = 'bar' assertequal(expect, actual) actual = lkp.find(5, 7) expect = 'bar' assertequal(expect, actual) actual = lkp.find(8, 9) expect = 'baz' assertequal(expect, actual) actual = lkp.find(9, 14) expect = None assertequal(expect, actual) actual = lkp.find(19, 140) expect = None assertequal(expect, actual) actual = lkp.find(1) expect = None assertequal(expect, actual) actual = lkp.find(2) expect = 'foo' assertequal(expect, actual) actual = lkp.find(4) expect = 'bar' ieq(expect, actual) actual = lkp.find(5) expect = 'bar' ieq(expect, actual) actual = lkp.find(8) expect = 'baz' ieq(expect, actual)