def test_just_files(self, os_mock): config_dir = ConfigDirectory() os_mock.path.isfile.side_effect = self.isfile files = config_dir.just_files(['file_foo', 'dir_bar']) expected_files = ['file_foo'] self.assertEqual(files, expected_files)
def test_just_dirs(self, os_mock): config_dir = ConfigDirectory() os_mock.path.isdir.side_effect = self.isdir dirs = config_dir.just_dirs(['file_foo', 'dir_bar']) expected_dirs = ['dir_bar'] self.assertEqual(dirs, expected_dirs)
def test_get_subdirectories(self, os_mock): config_dir = ConfigDirectory() os_mock.path.join = os.path.join os_mock.listdir.return_value = ['file_foo', 'dir_bar', 'link_baz'] os_mock.path.isdir.side_effect = self.isdir subdirs = config_dir.get_subdirectories('parent_directory') self.assertEqual(subdirs, ['parent_directory/dir_bar'])
def test_find_job_trackers_in_folder(self): config_dir = ConfigDirectory() script_mock = MagicMock() script_mock.return_value = ['parent_directory/foo', 'parent_directory/foo.conf', 'parent_directory/foo_pipeline', 'parent_directory/foo_pipeline.conf'] config_dir.get_files_in_directory = script_mock trackers = config_dir.find_job_trackers_in_folder('parent_directory') expected_trackers = ['parent_directory/foo_pipeline.conf'] self.assertEqual(trackers, expected_trackers)
def test_get_all_job_tracker_filenames(self, os_mock): config_dir = ConfigDirectory() os_mock.path.join = os.path.join os_mock.path.isdir.side_effect = self.isdir os_mock.path.isfile.side_effect = self.isfile os_mock.listdir.side_effect = self.nested_directory os_mock.path.sep = '/' pipeline_files = config_dir.get_all_job_tracker_filenames('dir_parent') expected_files = ['dir_parent/file_parent_pipeline.conf', 'dir_parent/dir_child/file_child_pipeline.conf'] self.assertEqual(pipeline_files, expected_files)
def test_get_files_in_directory(self, os_mock): config_dir = ConfigDirectory() os_mock.path.join = os.path.join os_mock.listdir.return_value = ['file_foo', 'dir_bar', 'link_baz'] os_mock.path.isfile.side_effect = self.isfile files = config_dir.get_files_in_directory('parent_directory') expected_files = ['parent_directory/file_foo', 'parent_directory/link_baz'] self.assertItemsEqual(files, expected_files) os_mock.listdir.assert_called_with('parent_directory')
def test_get_job_trackers(self): config_dir = ConfigDirectory() config_dir.get_all_job_tracker_filenames = MagicMock() pipeline_files =['dir_parent/file_parent_pipeline.conf', 'dir_parent/dir_child/file_child_pipeline.conf'] config_dir.get_all_job_tracker_filenames.return_value = pipeline_files tracker_files = config_dir.get_job_trackers('dir_parent') self.assertEquals(len(tracker_files), 2) self.assertIsInstance(tracker_files[0], TrackerFile) self.assertIsInstance(tracker_files[1], TrackerFile) tracker_filenames = [tracker.path for tracker in tracker_files] self.assertEqual(tracker_filenames, pipeline_files)
def test_to_dict(self, datetime_mock): datetime_mock.datetime.now.return_value.isoformat.return_value = '1900-01-01T00:00:00.000000' def new_job(approval_required, job_type, config_file): job = MagicMock() job.approval_required = approval_required job.job_type = job_type job.config_file = config_file return job config_dir = ConfigDirectory() config_dir.get_job_trackers = MagicMock() job_tracker = MagicMock() job_tracker.path = 'parent_dir/foo_pipeline.conf' job_tracker.get_jobs.return_value = [ new_job(True, '__FOO__', 'parent_dir/child_dir/job_1.conf'), new_job(True, '__BAR__', 'parent_dir/child_dir/job_2.conf'), new_job(False, '__BAZ__', 'parent_dir/child_dir/job_3.conf') ] config_dir.get_job_trackers.return_value = [job_tracker] actual_dict = config_dir.to_dict('parent_dir') expected_dict = { 'jobs': [{ 'approval_required': True, 'job_type': '__FOO__', 'config_file': 'parent_dir/child_dir/job_1.conf', 'pipeline_tracker': 'parent_dir/foo_pipeline.conf' }, { 'approval_required': True, 'job_type': '__BAR__', 'config_file': 'parent_dir/child_dir/job_2.conf', 'pipeline_tracker': 'parent_dir/foo_pipeline.conf' }, { 'approval_required': False, 'job_type': '__BAZ__', 'config_file': 'parent_dir/child_dir/job_3.conf', 'pipeline_tracker': 'parent_dir/foo_pipeline.conf' }], 'created_at': '1900-01-01T00:00:00.000000' } self.assertEqual(actual_dict, expected_dict)
def nested_directory(self, dirname): config_dir = ConfigDirectory() if dirname == 'dir_parent': return ['file_parent_pipeline.conf', 'file_parent_random', 'dir_child'] elif dirname == 'dir_parent/dir_child': return ['file_child_pipeline.conf', 'file_child_random', 'dir_grandchild'] else: return ['file_another_pipeline.conf', 'file_another_random', 'dir_yet_another']
def test_to_dict(self, datetime_mock): datetime_mock.datetime.now.return_value.isoformat.return_value = '1900-01-01T00:00:00.000000' def new_job(approval_required, job_type, config_file): job = MagicMock() job.approval_required = approval_required job.job_type = job_type job.config_file = config_file return job config_dir = ConfigDirectory() config_dir.get_job_trackers = MagicMock() job_tracker = MagicMock() job_tracker.path = 'parent_dir/foo_pipeline.conf' job_tracker.get_jobs.return_value = [ new_job(True, '__FOO__', 'parent_dir/child_dir/job_1.conf'), new_job(True, '__BAR__', 'parent_dir/child_dir/job_2.conf'), new_job(False, '__BAZ__', 'parent_dir/child_dir/job_3.conf') ] config_dir.get_job_trackers.return_value = [job_tracker] actual_dict = config_dir.to_dict('parent_dir') expected_dict = { 'jobs': [ { 'approval_required': True, 'job_type': '__FOO__', 'config_file': 'parent_dir/child_dir/job_1.conf', 'pipeline_tracker': 'parent_dir/foo_pipeline.conf' }, { 'approval_required': True, 'job_type': '__BAR__', 'config_file': 'parent_dir/child_dir/job_2.conf', 'pipeline_tracker': 'parent_dir/foo_pipeline.conf' }, { 'approval_required': False, 'job_type': '__BAZ__', 'config_file': 'parent_dir/child_dir/job_3.conf', 'pipeline_tracker': 'parent_dir/foo_pipeline.conf' } ], 'created_at': '1900-01-01T00:00:00.000000' } self.assertEqual(actual_dict, expected_dict)
def test_has_pipeline_conf(self): config_dir = ConfigDirectory() self.assertFalse(config_dir.is_pipeline_conf('foo')) self.assertFalse(config_dir.is_pipeline_conf('foo.conf')) self.assertFalse(config_dir.is_pipeline_conf('foo_pipeline')) self.assertTrue(config_dir.is_pipeline_conf('foo_pipeline.conf')) self.assertFalse(config_dir.is_pipeline_conf('foo_pipeline.conf.backup')) self.assertFalse(config_dir.is_pipeline_conf('~/directory/foo.conf')) self.assertFalse(config_dir.is_pipeline_conf('~/directory/foo_pipeline')) self.assertTrue(config_dir.is_pipeline_conf('~/directory/foo_pipeline.conf')) self.assertFalse(config_dir.is_pipeline_conf('~/directory/foo_pipeline.conf.backup')) self.assertFalse(config_dir.is_pipeline_conf('~/directory_pipeline.conf/foo'))
def test_has_pipeline_conf(self): config_dir = ConfigDirectory() self.assertFalse(config_dir.is_pipeline_conf('foo')) self.assertFalse(config_dir.is_pipeline_conf('foo.conf')) self.assertFalse(config_dir.is_pipeline_conf('foo_pipeline')) self.assertTrue(config_dir.is_pipeline_conf('foo_pipeline.conf')) self.assertFalse( config_dir.is_pipeline_conf('foo_pipeline.conf.backup')) self.assertFalse(config_dir.is_pipeline_conf('~/directory/foo.conf')) self.assertFalse( config_dir.is_pipeline_conf('~/directory/foo_pipeline')) self.assertTrue( config_dir.is_pipeline_conf('~/directory/foo_pipeline.conf')) self.assertFalse( config_dir.is_pipeline_conf( '~/directory/foo_pipeline.conf.backup')) self.assertFalse( config_dir.is_pipeline_conf('~/directory_pipeline.conf/foo'))