[Python-checkins] python/nondist/sandbox/csv/test __init__.py,NONE,1.1 test_csv.py,NONE,1.1

andrewmcnamara@users.sourceforge.net andrewmcnamara@users.sourceforge.net
Thu, 30 Jan 2003 20:08:20 -0800


Update of /cvsroot/python/python/nondist/sandbox/csv/test
In directory sc8-pr-cvs1:/tmp/cvs-serv14214/test

Added Files:
	__init__.py test_csv.py 
Log Message:
Added a test framework, and some tests for the "excel" dialect (reader only,
so far).


--- NEW FILE: __init__.py ---
# Copyright (C) 2001,2002 Python Software Foundation
# email package unit tests

--- NEW FILE: test_csv.py ---
# Copyright (C) 2001,2002 Python Software Foundation
# csv package unit tests

import sys
import unittest
from StringIO import StringIO
import csv

class TestCsvBase(unittest.TestCase):
    def readerAssertEqual(self, input, expected_result):
        reader = csv.reader(StringIO(input), dialect = self.dialect)
        fields = list(reader)
        self.assertEqual(fields, expected_result)

class TestDialectExcel(TestCsvBase):
    dialect = 'excel'

    def test_single(self):
        self.readerAssertEqual('abc', [['abc']])

    def test_simple(self):
        self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']])

    def test_blankline(self):
        self.readerAssertEqual('', [])

    def test_empty_fields(self):
        self.readerAssertEqual(',', [['', '']])

    def test_singlequoted(self):
        self.readerAssertEqual('""', [['']])

    def test_singlequoted_left_empty(self):
        self.readerAssertEqual('"",', [['','']])

    def test_singlequoted_right_empty(self):
        self.readerAssertEqual(',""', [['','']])

    def test_single_quoted_quote(self):
        self.readerAssertEqual('""""', [['"']])

    def test_quoted_quotes(self):
        self.readerAssertEqual('""""""', [['""']])

    def test_inline_quote(self):
        self.readerAssertEqual('a""b', [['a""b']])

    def test_inline_quotes(self):
        self.readerAssertEqual('a"b"c', [['a"b"c']])

    def test_quotes_and_more(self):
        self.readerAssertEqual('"a"b', [['ab']])

    def test_lone_quote(self):
        self.readerAssertEqual('a"b', [['a"b']])

    def test_quote_and_quote(self):
        self.readerAssertEqual('"a" "b"', [['a "b"']])

    def test_space_and_quote(self):
        self.readerAssertEqual(' "a"', [[' "a"']])

    def test_quoted(self):
        self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', 
                          [['1', '2', '3', 
                            'I think, therefore I am', 
                            '5', '6']])

    def test_quoted_quote(self):
        self.readerAssertEqual('1,2,3,"""I see,"" said the blind man","as he picked up his hammer and saw"',
                          [['1', '2', '3', 
                            '"I see," said the blind man', 
                            'as he picked up his hammer and saw']])

    def test_quoted_nl(self):
        input = '''\
1,2,3,"""I see,""
said the blind man","as he picked up his
hammer and saw"
9,8,7,6'''
        self.readerAssertEqual(input,
                          [['1', '2', '3', 
                            '"I see,"\nsaid the blind man', 
                            'as he picked up his\nhammer and saw'],
                           ['9','8','7','6']])

    def test_dubious_quote(self):
        self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']])

def _testclasses():
    mod = sys.modules[__name__]
    return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]

def suite():
    suite = unittest.TestSuite()
    for testclass in _testclasses():
        suite.addTest(unittest.makeSuite(testclass))
    return suite

if __name__ == '__main__':
    unittest.main(defaultTest='suite')