def test_log_and_mark_stuck_exports_as_errored(self): task_data = { 'source': reverse('asset-detail', args=[self.asset.uid]), 'type': 'csv', } self.assertEqual( 0, ExportTask._filter_by_source_kludge( ExportTask.objects.filter( user=self.user), task_data['source'] ).count() ) # Simulate a few stuck exports for status in (ExportTask.CREATED, ExportTask.PROCESSING): export_task = ExportTask() export_task.user = self.user export_task.data = task_data export_task.status = status export_task.save() export_task.date_created -= datetime.timedelta(days=1) export_task.save() self.assertSequenceEqual( [ExportTask.CREATED, ExportTask.PROCESSING], ExportTask._filter_by_source_kludge( ExportTask.objects.filter( user=self.user), task_data['source'] ).order_by('pk').values_list('status', flat=True) ) # Run another export, which invokes the cleanup logic export_task = ExportTask() export_task.user = self.user export_task.data = task_data export_task.save() export_task.run() # Verify that the stuck exports have been marked self.assertSequenceEqual( [ExportTask.ERROR, ExportTask.ERROR, ExportTask.COMPLETE], ExportTask._filter_by_source_kludge( ExportTask.objects.filter( user=self.user), task_data['source'] ).order_by('pk').values_list('status', flat=True) )
def test_remove_excess_exports(self): task_data = { 'source': reverse('asset-detail', args=[self.asset.uid]), 'type': 'csv', } # Create and run one export, so we can verify that it's `result` file # is later deleted export_task = ExportTask() export_task.user = self.user export_task.data = task_data export_task.save() export_task.run() self.assertEqual(export_task.status, ExportTask.COMPLETE) result = export_task.result self.assertTrue(result.storage.exists(result.name)) # Make an excessive amount of additional exports excess_count = 5 + settings.MAXIMUM_EXPORTS_PER_USER_PER_FORM for _ in range(excess_count): export_task = ExportTask() export_task.user = self.user export_task.data = task_data export_task.save() created_export_tasks = ExportTask._filter_by_source_kludge( ExportTask.objects.filter(user=self.user), task_data['source'] ) self.assertEqual(excess_count + 1, created_export_tasks.count()) # Identify which exports should be kept export_tasks_to_keep = created_export_tasks.order_by('-date_created')[ :settings.MAXIMUM_EXPORTS_PER_USER_PER_FORM] # Call `run()` once more since it invokes the cleanup logic export_task.run() self.assertEqual(export_task.status, ExportTask.COMPLETE) # Verify the cleanup self.assertFalse(result.storage.exists(result.name)) self.assertListEqual( # assertSequenceEqual isn't working... list(export_tasks_to_keep.values_list('pk', flat=True)), list(ExportTask._filter_by_source_kludge( ExportTask.objects.filter( user=self.user), task_data['source'] ).order_by('-date_created').values_list('pk', flat=True)) )
def test_export_long_form_title(self): what_a_title = ( 'the quick brown fox jumped over the lazy dog and jackdaws love ' 'my big sphinx of quartz and pack my box with five dozen liquor ' 'jugs dum cornelia legit flavia scribit et laeta est flavia quod ' 'cornelia iam in villa habitat et cornelia et flavia sunt amicae') assert len(what_a_title) > ExportTask.MAXIMUM_FILENAME_LENGTH self.asset.name = what_a_title self.asset.save() task_data = { 'source': reverse('asset-detail', args=[self.asset.uid]), 'type': 'csv', } export_task = ExportTask() export_task.user = self.user export_task.data = task_data export_task.save() export_task.run() assert (len(os.path.basename( export_task.result.name)) == ExportTask.MAXIMUM_FILENAME_LENGTH)