Update of /cvsroot/webware/Webware/MiscUtils/Testing
In directory usw-pr-cvs1:/tmp/cvs-serv1324/Testing
Modified Files:
TestDataTable.py
Added Files:
TestCSVParser.py
Log Message:
rewrote the parsing of CSV records so that DataTable now supports the full range of what are considered normal CSV files
--- NEW FILE: TestCSVParser.py ---
import FixPath
import unittest
from CSVParser import CSVParser, ParseError
class CSVParserTests(unittest.TestCase):
"""
TO DO
* Test the different options for parser. See CVSParser.__init__
"""
def setUp(self):
self.parse = CSVParser().parse
def testNegatives(self):
inputs = [
'""a',
'"a"b',
'a\n,b'
]
for input in inputs:
try:
results = self.parse(input)
except ParseError:
pass
else:
print
print 'results:', repr(results)
raise Exception, 'Did not get an exception for: %r' % input
def testPositives(self):
tests = [
# basics
('', []),
(',', ['','']),
(',,', ['', '', '']),
('a', ['a']),
('a,b', ['a', 'b']),
('a,b,c,d,e,f', 'a b c d e f'.split()),
# surrounding whitespace
(' a', ['a']),
('a ', ['a']),
(' a ', ['a']),
('a, b', ['a', 'b']),
(' a , b ', ['a', 'b']),
# commas in fields
('","', [',']),
('",",","', [',', ',']),
('"a , b",b', ['a , b', 'b']),
# quotes in fields
('""""', ['"']),
('""""""', ['""']),
('"""a""",b,"""c"""', ['"a"', 'b', '"c"']),
# single line combos
(' "a", "b"', ['a', 'b']),
(' """"', ['"']),
('"""" ', ['"']),
(' """" ', ['"']),
(' """a""", """b"""', ['"a"', '"b"']),
(' """", ",", ""","""', ['"', ',', '","']),
# comments
('#a,b', []),
# multiple line records
('"a\nb"', ['a\nb']),
('a,"b\nc"', ['a', 'b\nc']),
('a,"b\nc\n\n\n"', ['a', 'b\nc']),
# MiddleKit enums
('a,Enums="b"', ['a', 'Enums="b"']),
("a,Enums='b'", ['a', "Enums='b'"]),
('a,"Enums=""b, c"""', ['a', 'Enums="b, c"']),
('''a,"Enums='b, c'"''', ['a', "Enums='b, c'"]),
]
for input, output in tests:
if input.find('\n')==-1:
# single line
result = self.parse(input)
assert result==output, '\ninput=%r\nresult=%r\noutput=%r' % (input, result, output)
result = self.parse(input+'\n')
assert result==output, '\ninput=%r\nresult=%r\noutput=%r' % (input, result, output)
else:
# multiple lines
gotFields = 0
for line in input.split('\n'):
assert not gotFields
result = self.parse(line)
if result is not None:
gotFields = 1
assert gotFields
assert result==output, '\ninput=%r\nresult=%r\noutput=%r' % (input, result, output)
def main():
suite = unittest.makeSuite(CSVParserTests, 'test')
runner = unittest.TextTestRunner()
runner.run(suite)
if __name__=='__main__':
main()
Index: TestDataTable.py
===================================================================
RCS file: /cvsroot/webware/Webware/MiscUtils/Testing/TestDataTable.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** TestDataTable.py 2001/12/20 20:01:11 1.6
--- TestDataTable.py 2001/12/20 21:23:05 1.7
***************
*** 12,16 ****
def testSource(name, src, headings, data):
! heading(name+':')
dt = DataTable()
lines = split(src, '\n')
--- 12,16 ----
def testSource(name, src, headings, data):
! heading(name)
dt = DataTable()
lines = split(src, '\n')
***************
*** 22,28 ****
if dt[i].asList()!=match:
print 'mismatch'
! print 'i :', i
! print 'match :', match
! print 'dt[i] :', dt[i]
raise AssertionError
i = i + 1
--- 22,28 ----
if dt[i].asList()!=match:
print 'mismatch'
! print 'i :', i
! print 'expected :', match
! print 'got :', dt[i]
raise AssertionError
i = i + 1
***************
*** 32,47 ****
print 'Simple tests...'
! heading('Create table:')
t = DataTable()
! heading('Headings 1:')
t = DataTable()
t.setHeadings([TableColumn('name'), TableColumn('age:int'), TableColumn('rating:float')])
! heading('Headings 2:')
t = DataTable()
t.setHeadings(['name', 'age:int', 'rating:float'])
! heading('Adding and accessing data:')
a = ['John', '26', '7.2']
b = ['Mary', 32, 8.3]
--- 32,47 ----
print 'Simple tests...'
! heading('Create table')
t = DataTable()
! heading('Headings 1')
t = DataTable()
t.setHeadings([TableColumn('name'), TableColumn('age:int'), TableColumn('rating:float')])
! heading('Headings 2')
t = DataTable()
t.setHeadings(['name', 'age:int', 'rating:float'])
! heading('Adding and accessing data')
a = ['John', '26', '7.2']
b = ['Mary', 32, 8.3]
***************
*** 53,60 ****
assert t[-2]['name']=='John'
! heading('Printing:')
print t
! heading('Writing file (CSV):')
answer = '''\
name,age,rating
--- 53,60 ----
assert t[-2]['name']=='John'
! heading('Printing')
print t
! heading('Writing file (CSV)')
answer = '''\
name,age,rating
***************
*** 67,71 ****
assert results==answer, '\n%r\n%r\n' % (results, answer)
! heading('Accessing rows:')
for row in t:
assert row['name']==row[0]
--- 67,71 ----
assert results==answer, '\n%r\n%r\n' % (results, answer)
! heading('Accessing rows')
for row in t:
assert row['name']==row[0]
***************
*** 75,79 ****
pass
! heading('Default type:')
t = DataTable(defaultType='int')
t.setHeadings(list('xyz'))
--- 75,79 ----
pass
! heading('Default type')
t = DataTable(defaultType='int')
t.setHeadings(list('xyz'))
***************
*** 125,138 ****
src = '''\
a
! """Hi""",
'''
headings = ['a']
! data = ['"Hi"']
! #testSource('Multiline records', src, headings, data)
! print
! def test():
print 'Testing DataTable.py'
test01()
--- 125,162 ----
src = '''\
a
! """Hi
! there"""
'''
headings = ['a']
! data = [
! ['"Hi\nthere"'],
! ]
! testSource('Multiline records', src, headings, data)
! # MiddleKit enums
! src = '''\
! Class,Attribute,Type,Extras
! #Foo,
! ,what,enum,"Enums=""foo, bar"""
! ,what,enum,"Enums='foo, bar'"
! '''
! headings = 'Class,Attribute,Type,Extras'.split(',')
! data = [
! ['', 'what', 'enum', 'Enums="foo, bar"'],
! ['', 'what', 'enum', "Enums='foo, bar'"],
! ]
! testSource('MK enums', src, headings, data)
! heading('Unfinished multiline record')
! try:
! DataTable().readString('a\n"1\n')
! except DataTableError:
! pass # just what we were expecting
! else:
! raise Exception, 'Failed to raise exception for unfinished multiline record'
!
!
! def main():
print 'Testing DataTable.py'
test01()
***************
*** 141,143 ****
if __name__=='__main__':
! test()
--- 165,167 ----
if __name__=='__main__':
! main()
|