def test_remove_experiment_display_message_removing(self, mock_user_input): owner = create_owner('labX') experiment = create_experiment(1, owner=owner) out = StringIO() call_command('remove_experiment', experiment.nes_id, experiment.owner, stdout=out) self.assertIn( 'Removing all versions of experiment "%s" data and files...' % experiment.title, out.getvalue())
def test_remove_experiment_removes_uploads_subdir_if_there_are_not_files( self, mock_user_input): owner = create_owner('labX') experiment = create_experiment(1, owner=owner) uploads_subdir = os.path.join(self.TEMP_MEDIA_ROOT, 'uploads') create_uploads_subdirs_and_files(uploads_subdir, empty=True) out = StringIO() call_command('remove_experiment', experiment.nes_id, experiment.owner, stdout=out) self.assertFalse(os.path.exists(uploads_subdir)) self.assertTrue(os.path.exists(self.TEMP_MEDIA_ROOT))
def test_remove_experiment_removes_all_versions(self, mock_user_input): owner = create_owner('labX') experiment = create_experiment(1, owner=owner) experiment_versions = create_experiment_versions(11, experiment) experiment_version = choice(experiment_versions) out = StringIO() call_command('remove_experiment', experiment_version.nes_id, experiment_version.owner, stdout=out) self.assertFalse(Experiment.objects.exists()) self.assertIn( 'All versions of experiment "%s" successfully removed' % experiment.title, out.getvalue())
def test_remove_experiment_removes_uploads_subdirs_if_they_are_empty( self, mock_user_input): owner = create_owner('labX') experiment = create_experiment(1, owner=owner) uploads_subdir = os.path.join(self.TEMP_MEDIA_ROOT, 'uploads') create_uploads_subdirs_and_files(uploads_subdir) out = StringIO() call_command('remove_experiment', experiment.nes_id, experiment.owner, stdout=out) for root, dirs, files in os.walk(uploads_subdir): # it's in a path tree leaf wo files (e.g. 'uploads/2018/01/15') if not dirs: self.assertTrue(os.path.exists(root))
def test_remove_experiment_displays_prompt_and_user_confirm_removing( self, mock_user_input): owner = create_owner('labX') experiment = create_experiment(1, owner=owner) out = StringIO() call_command('remove_experiment', experiment.nes_id, experiment.owner, stdout=out) self.assertEqual(mock_user_input.called, True) (text, ), kwargs = mock_user_input.call_args self.assertEqual( text, BaseCommand().style.WARNING( 'All versions of experiment "%s" will be destroyed and cannot be ' 'recovered. Are you sure? (Yes/n) ' % experiment.title)) self.assertFalse(Experiment.objects.exists())
def test_remove_the_only_experiment_that_exists_removes_media_download_subdir( self, mock_user_input): owner = create_owner('labX') experiment = create_experiment(1, owner=owner) download_subdir = os.path.join(self.TEMP_MEDIA_ROOT, 'download', str(experiment.id)) create_download_subdirs(download_subdir) out = StringIO() call_command('remove_experiment', experiment.nes_id, experiment.owner, stdout=out) self.assertFalse(os.path.exists(download_subdir)) self.assertFalse( os.path.exists(os.path.join(self.TEMP_MEDIA_ROOT, 'download')))
def test_remove_experiment_last_version_removes_only_last_version( self, mock_user_input): owner = create_owner('labX') experiment = create_experiment(1, owner=owner) experiment_versions = create_experiment_versions(5, experiment) experiment_version = choice(experiment_versions) out = StringIO() call_command('remove_experiment', experiment_version.nes_id, experiment_version.owner, '--last', stdout=out) self.assertEqual(5, Experiment.objects.count()) self.assertEqual(5, Experiment.lastversion_objects.last().version) self.assertIn( 'Last version of experiment "%s" successfully removed' % experiment.title, out.getvalue())
def test_remove_experiment_removes_media_download_experiment_subdir( self, mock_user_input): owner = create_owner('labX') experiments = create_experiment(2, owner=owner) download_subdirs = [] for experiment in experiments: download_subdir = os.path.join(self.TEMP_MEDIA_ROOT, 'download', str(experiment.id)) create_download_subdirs(download_subdir) download_subdirs.append(download_subdir) out = StringIO() call_command('remove_experiment', experiments[0].nes_id, experiments[0].owner, stdout=out) self.assertFalse(os.path.exists(download_subdirs[0])) self.assertTrue( os.path.exists(os.path.join(self.TEMP_MEDIA_ROOT, 'download')))