def test_create_size_dictionary(self, mock_isdir, mock_walk, mock_getsize): ''' Test that create_size_dictionary() correctly reports files' sizes in bytes. ''' # Assign side effects mock_isdir.side_effect = fx.isdir_side_effect mock_walk.side_effect = fx.walk_side_effect mock_getsize.side_effect = fx.getsize_side_effect sizes = tools.create_size_dictionary('test_files') self.assertEqual(len(sizes), 3) # Check that test.txt and test.jpg are in the dictionary root_path = [path for path in sizes.keys() \ if re.search('root_file.txt$', path)] pdf_path = [path for path in sizes.keys() \ if re.search('test.pdf$', path)] self.assertTrue(bool(root_path)) self.assertTrue(bool(pdf_path)) # Check that the size dictionary reports these files' correct sizes in bytes self.assertEqual(sizes[root_path[0]], 100) self.assertEqual(sizes[pdf_path[0]], 1000) # Check that the function raises an error when its path argument # is not a directory. with self.assertRaises(ReleaseError), nostderrout(): sizes = tools.create_size_dictionary('nonexistent_directory') # The path argument must be a string with self.assertRaises(TypeError), nostderrout(): sizes = tools.create_size_dictionary(10)
def testIllegalSyntax(self): # missing arguments for ext in ['lyx', 'tex']: with nostderrout(): error = tablefill(input = '../../gslab_fill/tests/input/tables_appendix.txt ' + \ '../../gslab_fill/tests/input/tables_appendix_two.txt', template = '../../gslab_fill/tests/input/tablefill_template.%s' % ext) self.assertIn('KeyError', error) # non-existent input 1 for ext in ['lyx', 'tex']: with nostderrout(): error = tablefill(input = '../../gslab_fill/tests/input/fake_file.txt ' + \ '../../gslab_fill/tests/input/tables_appendix_two.txt', template = '../../gslab_fill/tests/input/tablefill_template_breaks.%s' % ext, output = './build/tablefill_template_filled.%s' % ext) self.assertIn('IOError', error) # non-existent input 2 for ext in ['lyx', 'tex']: with nostderrout(): error = tablefill(input = '../../gslab_fill/tests/input/tables_appendix.txt ' + \ '../../gslab_fill/tests/input/fake_file.txt', template = '../../gslab_fill/tests/input/tablefill_template_breaks.%s' % ext, output = './build/tablefill_template_filled.%s' % ext) self.assertIn('IOError', error)
def test_illegal_syntax(self): # missing arguments with nostderrout(): error = textfill( input='../../gslab_fill/tests/input/legal.log', template='../../gslab_fill/tests/input/textfill_template.lyx') self.assertIn('KeyError', error) # non-existent input 1 with nostderrout(): error = textfill( input='../../gslab_fill/tests/input/fake_file.log', template='../../gslab_fill/tests/input/textfill_template.lyx', output='./build/textfill_template_filled.lyx') self.assertIn('IOError', error) # non-existent input 2 with nostderrout(): error = textfill( input='./log/stata.log ./input/fake_file.log', template='../../gslab_fill/tests/input/textfill_template.lyx', output='./build/textfill_template_filled.lyx') self.assertIn('IOError', error)
def test_argument_order(self): with nostderrout(): message = textfill(input = '../../gslab_fill/tests/input/legal.log', output = '../../gslab_fill/tests/input/textfill_template_filled.lyx', template = '../../gslab_fill/tests/input/textfill_template.lyx') self.assertIn('filled successfully', message) with nostderrout(): message = textfill(template = '../../gslab_fill/tests/input/textfill_template.lyx', output = './build/textfill_template_filled.lyx', input = '../../gslab_fill/tests/input/legal.log') self.assertIn('filled successfully', message)
def test_tags_not_closed(self): with nostderrout(): error = textfill( input='../../gslab_fill/tests/input/tags_not_closed.log', template='../../gslab_fill/tests/input/textfill_template.lyx', output='./build/textfill_template_filled.lyx') self.assertIn('HTMLParseError', error)
def test_tags_dont_match(self): with nostderrout(): error = textfill( input='../../gslab_fill/tests/input/tags_dont_match.log', template='../../gslab_fill/tests/input/textfill_template.lyx', output='./build/textfill_template_filled.lyx') self.assertIn('ValueError', error)
def testBreaksRoundingString(self): with nostderrout(): error = tablefill(input = '../../gslab_fill/tests/input/tables_appendix.txt ' + '../../gslab_fill/tests/input/tables_appendix_two.txt', template = '../../gslab_fill/tests/input/tablefill_template_breaks.lyx', output = './build/tablefill_template_filled.lyx') self.assertIn('InvalidOperation', error)
def test_remove_echoes(self): with nostderrout(): textfill(input = '../../gslab_fill/tests/input/legal.log', template = '../../gslab_fill/tests/input/textfill_template.lyx', output = './build/textfill_template_filled.lyx', remove_echoes = True) log_remove_string = '. ' log = '../../gslab_fill/tests/input/legal.log' self.check_log_in_LyX(log, log_remove_string, "textfill_")
def test_input(self): with nostderrout(): message = textfill(input = '../../gslab_fill/tests/input/legal.log', template = '../../gslab_fill/tests/input/textfill_template.lyx', output = './build/textfill_template_filled.lyx') self.assertIn('filled successfully', message) log_remove_string = '. insert_tag' log = '../../gslab_fill/tests/input/legal.log' self.check_log_in_LyX(log, log_remove_string, "textfill_")
def test_tags_incorrectly_specified(self): with nostderrout(): textfill(input = '../../gslab_fill/tests/input/tags_incorrectly_named.log', template = '../../gslab_fill/tests/input/textfill_template.lyx', output = './build/textfill_template_filled.lyx') log_remove_string = '. insert_tag' with self.assertRaises(AssertionError): log = '../../gslab_fill/tests/input/tags_incorrectly_named.log' self.check_log_in_LyX(log, log_remove_string, "textfill_")
def testInput(self): with nostderrout(): message = tablefill(input = '../../gslab_fill/tests/input/tables_appendix.txt ' + \ '../../gslab_fill/tests/input/tables_appendix_two.txt', template = '../../gslab_fill/tests/input/tablefill_template.lyx', output = './build/tablefill_template_filled.lyx') self.assertIn('filled successfully', message) tag_data = open('../../gslab_fill/tests/input/tablefill_template.lyx', 'rU').readlines() filled_data = open('./build/tablefill_template_filled.lyx', 'rU').readlines() self.assertEqual(len(tag_data) + 13, len(filled_data)) for n in range(len(tag_data)): self.tag_compare(tag_data[n], filled_data[n + 13])
def test_error_traceback(self, mock_tablefill): ''' Test that build_tables() properly outputs traceback. ''' mock_tablefill.side_effect = self.table_fill_side_effect_error source = [ './input/tablefill_template.lyx', './input/tables_appendix.txt', './input/tables_appendix_two.txt' ] target = './build/tablefill_template_filled.lyx' with self.assertRaises(ExecCallError), nostderrout(): gs.build_tables(target, source, {})
def test_up_to_date(self, mock_call): ''' Test that up_to_date() correctly recognises an SCons directory as up-to-date or out of date. ''' # The mode argument needs to be one of the valid options with self.assertRaises(ReleaseError), nostderrout(): gslab_scons._release_tools.up_to_date(mode='invalid') # The mock of subprocess call should write pre-specified text # to stdout. This mock prevents us from having to set up real # SCons and git directories. mock_call.side_effect = \ fx.make_call_side_effect('Your branch is up-to-date') self.assertTrue(gslab_scons._release_tools.up_to_date(mode='git')) mock_call.side_effect = \ fx.make_call_side_effect('modified: .sconsign.dblite') self.assertFalse(gslab_scons._release_tools.up_to_date(mode='git')) mock_call.side_effect = \ fx.make_call_side_effect("scons: `.' is up to date.") self.assertTrue(gslab_scons._release_tools.up_to_date(mode='scons')) mock_call.side_effect = \ fx.make_call_side_effect('python some_script.py') self.assertFalse(gslab_scons._release_tools.up_to_date(mode='scons')) # The up_to_date() function shouldn't work in SCons or git mode # when it is called outside of a SCons directory or a git # repository, respectively. mock_call.side_effect = \ fx.make_call_side_effect("Not a git repository") with self.assertRaises(ReleaseError), nostderrout(): gslab_scons._release_tools.up_to_date(mode='git') mock_call.side_effect = \ fx.make_call_side_effect("No SConstruct file found") with self.assertRaises(ReleaseError), nostderrout(): gslab_scons._release_tools.up_to_date(mode='scons')
def test_upload_asset_bad_file(self, mock_session): ''' Test that upload_asset() raises an error when its file_name argument isn't valid. ''' mock_session.return_value = mock.MagicMock(post=mock.MagicMock()) with self.assertRaises(ReleaseError), nostderrout(): tools.upload_asset(token='test_token', org='gslab-econ', repo='gslab_python', release_id='test_release', file_name='nonexistent_file', content_type='text/markdown')
def testArgumentOrder(self): with nostderrout(): message = tablefill(input = '../../gslab_fill/tests/input/tables_appendix.txt ' + \ '../../gslab_fill/tests/input/tables_appendix_two.txt', output = './build/tablefill_template_filled.lyx', template = '../../gslab_fill/tests/input/tablefill_template.lyx') self.assertIn('filled successfully', message) with open('./build/tablefill_template_filled.lyx', 'rU') as filled_file: filled_data_args1 = filled_file.readlines() with nostderrout(): message = tablefill(output = './build/tablefill_template_filled.lyx', template = '../../gslab_fill/tests/input/tablefill_template.lyx', input = '../../gslab_fill/tests/input/tables_appendix.txt ' + \ '../../gslab_fill/tests/input/tables_appendix_two.txt') self.assertIn('filled successfully', message) with open('./build/tablefill_template_filled.lyx', 'rU') as filled_file: filled_data_args2 = filled_file.readlines() self.assertEqual(filled_data_args1, filled_data_args2)
def test_target_extension(self): '''Test that build_tables() recognises an inappropriate file extension''' # Specify the sources and the target. source = [ './input/tablefill_template.lyx', './input/tables_appendix.txt', './input/tables_appendix_two.txt' ] target = './build/tablefill_template_filled.BAD' # Calling build_tables() with a target argument whose file extension # is unexpected should raise a BadExtensionError. with self.assertRaises(BadExtensionError), nostderrout(): gs.build_tables(target, source, '')
def test_check_code_extension(self): '''Unit tests for check_code_extension() This method tests that check_code_extensions() associates software with file extensions as intended. The function should return None in cases where the extension is correctly specified and raise an error otherwise. ''' self.assertEqual(misc.check_code_extension('test.do', '.do'), None) self.assertEqual(misc.check_code_extension('test.r', '.r'), None) self.assertEqual(misc.check_code_extension('test.lyx', '.lyx'), None) self.assertEqual(misc.check_code_extension('test.py', '.py'), None) self.assertEqual(misc.check_code_extension('test.m', '.m'), None) self.assertEqual(misc.check_code_extension('test.M', '.M'), None) with self.assertRaises(ex_classes.BadExtensionError), nostderrout(): misc.check_code_extension('test.badextension', '.py')
def testInput(self): for ext in ['lyx', 'tex']: with nostderrout(): message = tablefill(input = '../../gslab_fill/tests/input/tables_appendix.txt ' + \ '../../gslab_fill/tests/input/tables_appendix_two.txt', template = '../../gslab_fill/tests/input/tablefill_template.%s' % ext, output = './build/tablefill_template_filled.%s' % ext) self.assertIn('filled successfully', message) tag_data = open('../../gslab_fill/tests/input/tablefill_template.%s' % ext, 'rU').readlines() filled_data = open('./build/tablefill_template_filled.%s' % ext, 'rU').readlines() self.assertEqual(len(tag_data), len(filled_data)) for n in range(len(tag_data)): if ext == 'tex': self.tag_compare_latex(tag_data[n], filled_data[n]) elif ext == 'lyx': self.tag_compare_lyx(tag_data[n], filled_data[n])
def test_create_size_dictionary(self, mock_isdir, mock_walk, mock_getsize): ''' Test that create_size_dictionary() correctly reports files' sizes in bytes. ''' # Assign side effects mock_isdir.side_effect = isdir_dict_side_effect mock_walk.side_effect = make_walk_side_effect('create_size_dictionary') mock_getsize.side_effect = getsize_dict_side_effect # Test when one directory is provided sizes = sw.create_size_dictionary(['test_files']) self.assertEqual(len(sizes), 3) # Check that root_file.txt and test.pdf are in the dictionary root_path = [k for k in sizes.keys() if re.search('root_file.txt$', k)] pdf_path = [k for k in sizes.keys() if re.search('test.pdf$', k)] self.assertTrue(len(root_path) > 0) self.assertTrue(len(pdf_path) > 0) # Check that the size dictionary reports these files' correct sizes in bytes self.assertEqual(sizes[root_path[0]], 100) self.assertEqual(sizes[pdf_path[0]], 1000) # Check when two directories are provided sizes = sw.create_size_dictionary(['test_files', 'release']) self.assertEqual(len(sizes), 4) path = [k for k in sizes.keys() if re.search('output.txt$', k)] self.assertTrue(len(path) > 0) self.assertEqual(sizes[path[0]], 16) # Check when '.' is provided sizes = sw.create_size_dictionary(['.']) self.assertEqual(len(sizes), 4) # Check that the function does not raise an error when its path argument # is not a directory. sizes = sw.create_size_dictionary(['nonexistent_directory']) self.assertEqual(sizes, dict()) # The path argument must be a string with self.assertRaises(TypeError), nostderrout(): sizes = sw.create_size_dictionary([10])
def test_start_log_no_lfs(self, mock_check_lfs): # Check that start_log() fails if check_lfs() finds that # git-lfs is not installed mock_check_lfs.side_effect = self.check_lfs_effect with self.assertRaises(ex_classes.LFSError), nostderrout(): gs.start_log()