def runner(): Log.logger.debug('Start application.... ') try: args = parser.parse_args() f_path, f_out, mode = (args.file, args.out, args.mode) except: Log.logger.error('Missing required parameters') Log.logger.debug('Stop application....') exit() if mode != 'lines' and mode != 'words': Log.logger.error('Invalid mode!') Log.logger.debug('Stop application....') exit() if not os.path.exists(f_path): Log.logger.error('File not found!') Log.logger.debug('Stop application....') exit() elif f_path == f_out: Log.logger.error('File must be different!') Log.logger.debug('Stop application....') exit() elif os.path.exists(f_out): Log.logger.debug('Output file {} exists'.format(f_out)) Log.logger.debug('Waiting for user confirmation...') choise = input( 'Data will be added to the existing file. Conitnue [y/n] ?\n') if choise.lower() == 'y': Log.logger.debug('Data will be added to the existing file') pass elif choise.lower() == 'n': Log.logger.debug( 'Negative answer is selected. The application will be closed') Log.logger.debug('Stop application....') exit() elif choise.lower() != 'n' and choise.lower() != 'y': Log.logger.error('Invalid choise') Log.logger.debug('Stop application....') exit() if mode == 'lines': Log.logger.debug( 'User choise: find lines in {} and put them in {}'.format( f_path, f_out)) obj = Analyzer(args.file, args.out) obj.lines_processing() Log.logger.debug('Stop application....') elif mode == 'words': Log.logger.debug( 'User choise: find unique words in {} and put them in {}'.format( f_path, f_out)) obj = Analyzer(args.file, args.out) obj.words_processing() Log.logger.debug('Stop application....')
class WordProcessingTest(unittest.TestCase): def setUp(self): self.textfile = 'in' self.emptyfile = 'out' self.sample = 'no_file_disk' self.data = 'qwerty \n !,.?:QweRty;[] \n q1W34erT78y \t soFTeq 9Softeq9 sofTEQ\n' with open(self.textfile, 'w') as f: f.write(self.data) with open(self.emptyfile, 'w') as f: f.write('') self.words_ext = Analyzer(self.textfile, self.emptyfile) self.words_ext_empty = Analyzer(self.emptyfile, self.textfile) self.words_sample = Analyzer(self.textfile, self.sample) def tearDown(self): os.unlink(self.textfile) os.unlink(self.emptyfile) try: os.unlink(self.sample) except FileNotFoundError: pass def test_WordsExtractorRun(self): self.words_ext_empty.words_processing() self.assertListEqual(self.words_ext_empty.words, []) def test_WordsWriterRun(self): self.words_sample.words_processing() assert os.path.exists(self.sample) is True def test_WordsExtractorReturn(self): self.words_ext.words_extractor() self.assertEqual(len(self.words_ext.words), 6) def test_EmptyFileProcessing(self): self.words_ext_empty.words_processing() self.assertEqual(len(self.words_ext_empty.words), 0) def test_EmptyFileProcessingList(self): self.words_ext_empty.words_processing() self.assertListEqual(self.words_ext_empty.words, []) def test_RemoveNonAlphaWords(self): self.words_ext.words_processing() self.assertEqual(len(self.words_ext.words), 2)
class WordsWriterTest(unittest.TestCase): def setUp(self): self.textfile = 'in' self.emptyfile = 'out' self.nonemptyfile = 'nonempty' self.sample = 'no_file_disk' self.data = 'qwerty \n !,.?:QweRty;[] \n q1W34erT78y \t soFTeq 9Softeq9 sofTEQ\n' self.len_data = len(self.data) with open(self.textfile, 'w') as f: f.write(self.data) with open(self.emptyfile, 'w') as f: f.write('') with open(self.nonemptyfile, 'w') as f: f.write('data') self.file_ext = Analyzer(self.textfile, self.emptyfile) self.file_ext_empty = Analyzer(self.emptyfile, self.textfile) self.file_nonempty = Analyzer(self.textfile,self.nonemptyfile) self.file_sample = Analyzer(self.textfile, self.sample) def tearDown(self): os.unlink(self.textfile) os.unlink(self.emptyfile) os.unlink(self.nonemptyfile) try: os.unlink(self.sample) except FileNotFoundError: pass def test_EmptyFileWriteOnDisk(self): self.file_sample.words = [] assert os.path.exists(self.sample) is False self.file_sample.words_writer() assert os.path.exists(self.sample) is True def test_NonEmptyFileWriteOnDisk(self): self.file_sample.words = [self.data] assert os.path.exists(self.sample) is False self.file_sample.words_writer() assert os.path.exists(self.sample) is True def test_ValidDataWrite(self): self.file_sample.words_processing() self.filesize = os.path.getsize(self.sample) self.lenght = 0 for item in self.file_sample.words: self.lenght +=len(item) self.lenght +=1 self.assertEqual(self.lenght,self.filesize) def test_AddDataToExistFile(self): self.file_ext.words_processing() self.filesize_before = os.path.getsize(self.emptyfile) self.file_ext.words_processing() self.filesize_after = int(os.path.getsize(self.emptyfile)) self.lenght = 0 for item in self.file_ext.words: self.lenght +=len(item) self.lenght +=1 self.assertEqual(self.filesize_before + self.lenght, self.filesize_after)