0c54755937c3349d4f2199d37c3180a8e267f0ae mmaddren Thu Mar 10 16:17:49 2011 -0800 Added testing suite for RaFile. Will have to edit slightly in the future once comment implementation is finished. diff --git python/ucscgenomics/rafile/RaFileTest python/ucscgenomics/rafile/RaFileTest new file mode 100644 index 0000000..14ade41 --- /dev/null +++ python/ucscgenomics/rafile/RaFileTest @@ -0,0 +1,110 @@ +import sys +import os +import difflib +import unittest +import raFile + +class DiffCheck(unittest.TestCase): + + def testBasicDiff(self): + """diffs basic input for core ra functionality""" + ra = raFile.RaFile(raFile.RaEntry) + ra.read(os.getcwd() + '/tests/BasicDiff.ra') + file = open(os.getcwd() + '/tests/BasicDiff.ra') + outfile = open(os.getcwd() + '/testoutput/BasicDiff.out', 'w') + expected = file.read() + + outfile.write('========Expected========\n') + outfile.write(expected) + outfile.write('=========Output=========\n') + outfile.write(ra.__str__()) + outfile.write('========================\n') + + diffstr = '' + for line in difflib.unified_diff(expected, ra.__str__(), 'expected', 'out'): + diffstr += line + outfile.write(line) + + outfile.close() + self.failIf(len(diffstr) > 0) + + + def testCommentsDiff(self): + """diff to ensure that comments are preserved""" + ra = raFile.RaFile(raFile.RaEntry) + ra.read(os.getcwd() + '/tests/CommentsDiff.ra') + file = open(os.getcwd() + '/tests/CommentsDiff.ra') + outfile = open(os.getcwd() + '/testoutput/CommentsDiff.out', 'w') + expected = file.read() + + outfile.write('========Expected========\n') + outfile.write(expected) + outfile.write('=========Output=========\n') + outfile.write(ra.__str__()) + outfile.write('========================\n') + + diffstr = '' + for line in difflib.unified_diff(expected, ra.__str__(), 'expected', 'out'): + diffstr += line + outfile.write(line) + + outfile.close() + self.failIf(len(diffstr) > 0) + + def testExtraneousWhitespaceDiff(self): + """diff to ensure that extraneous whitespace is truncated""" + ra = raFile.RaFile(raFile.RaEntry) + ra.read(os.getcwd() + '/tests/ExtraneousWhitespace.ra') + file = open(os.getcwd() + '/tests/ExtraneousWhitespaceExpected.ra') + outfile = open(os.getcwd() + '/testoutput/ExtraneousWhitespace.out', 'w') + expected = file.read() + + outfile.write('========Expected========\n') + outfile.write(expected) + outfile.write('=========Output=========\n') + outfile.write(ra.__str__()) + outfile.write('========================\n') + + diffstr = '' + for line in difflib.unified_diff(expected, ra.__str__(), 'expected', 'out'): + diffstr += line + outfile.write(line) + + outfile.close() + self.failIf(len(diffstr) > 0) + + +class InvalidFilesCheck(unittest.TestCase): + + def testDuplicateKeys(self): + """makes sure that duplicate keys are caught""" + ra = raFile.RaFile(raFile.RaEntry) + self.assertRaises(KeyError, ra.read, os.getcwd() + '/tests/DuplicateKeys.ra') + + + def testMisplacedKeys(self): + """checks if keys in the incorrect place are caught""" + ra = raFile.RaFile(raFile.RaEntry) + self.assertRaises(KeyError, ra.read, os.getcwd() + '/tests/MisplacedKeys.ra') + + + def testNonExistentFile(self): + """checks if invalid files are caught""" + ra = raFile.RaFile(raFile.RaEntry) + self.assertRaises(IOError, ra.read, os.getcwd() + '/tests/FileDoesNotExist.ra') + + + def testNonNewlineFile(self): + """ensures file ends in newline""" + ra = raFile.RaFile(raFile.RaEntry) + self.assertRaises(IOError, ra.read, os.getcwd() + '/tests/NonNewlineFile.ra') + + + def testInvalidComments(self): + """ensures file doesn't have comments in the middle of stanzas""" + ra = raFile.RaFile(raFile.RaEntry) + self.assertRaises(KeyError, ra.read, os.getcwd() + '/tests/InvalidComments.ra') + + +if __name__ == '__main__': + unittest.main()