Ignore:
Timestamp:
6 Jun 2012, 00:45:24 (12 years ago)
Author:
uli
Message:

Add a helper function to append one CSV file to another.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py

    r8466 r8631  
    372372        return
    373373
     374class MergeCSVFileTestCase(unittest.TestCase):
     375
     376    def setUp(self):
     377        self.workdir = tempfile.mkdtemp()
     378        self.path1 = os.path.join(self.workdir, 'myfile1')
     379        self.path2 = os.path.join(self.workdir, 'myfile2')
     380        self.result_path = None
     381        return
     382
     383    def tearDown(self):
     384        shutil.rmtree(self.workdir)
     385        if self.result_path is not None and os.path.exists(self.result_path):
     386            os.unlink(self.result_path)
     387        return
     388
     389    def test_basic(self):
     390        # we can merge very basic CSV files
     391        open(self.path1, 'wb').write('name,age\nManfred,32\n')
     392        open(self.path2, 'wb').write('name,age\nBarney,28\n')
     393        self.result_path = helpers.merge_csv_files(self.path1, self.path2)
     394        contents = open(self.result_path, 'r').read()
     395        self.assertEqual(
     396            contents,
     397            'age,name\r\n'
     398            '32,Manfred\r\n'
     399            '28,Barney\r\n')
     400        return
     401
     402    def test_different_col_order(self):
     403        # if cols of both files have different order, that won't stop us
     404        open(self.path1, 'wb').write('name,age\nManfred,32\n')
     405        open(self.path2, 'wb').write('age,name\n28,Barney\n')
     406        self.result_path = helpers.merge_csv_files(self.path1, self.path2)
     407        contents = open(self.result_path, 'r').read()
     408        self.assertEqual(
     409            contents,
     410            'age,name\r\n'
     411            '32,Manfred\r\n'
     412            '28,Barney\r\n')
     413        return
     414
     415    def test_different_cols_at_all(self):
     416        # also cols available only in one file will work.
     417        open(self.path1, 'wb').write('name,age\nManfred,32\n')
     418        open(self.path2, 'wb').write('name,age,buddy\nBarney,28,Manfred\n')
     419        self.result_path = helpers.merge_csv_files(self.path1, self.path2)
     420        contents = open(self.result_path, 'r').read()
     421        self.assertEqual(
     422            contents,
     423            'age,buddy,name\r\n'
     424            '32,,Manfred\r\n'
     425            '28,Manfred,Barney\r\n')
     426        return
     427
     428
    374429def test_suite():
    375430    suite = unittest.TestSuite()
     
    386441        DateTimeHelpersTestCase,
    387442        GetFileFormatTestCase,
     443        MergeCSVFileTestCase,
    388444        ]:
    389445        suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.