def test_start_defers_process(self): patches = [ mock.patch('google.appengine.ext.deferred.defer'), ] with nested(*patches) as (mock_def,): task = ImportTask() task.start() self.assertTrue(mock_def.called)
def test_start_defers_process(self): patches = [ mock.patch('google.appengine.ext.deferred.defer'), ] with nested(*patches) as (mock_def, ): task = ImportTask() task.start() self.assertTrue(mock_def.called)
def test_process_creates_shards(self): task = ImportTask() patches = [ mock.patch('google.appengine.ext.deferred.defer'), mock.patch('osmosis.models.ImportShard.objects.create'), mock.patch('osmosis.models.ImportTask.objects.get', side_effect=lambda *args, **kwargs: task), mock.patch('osmosis.models.ImportTask.save') ] with nested(*patches) as (mock_def, mock_create, mock_get, mock_save): task.Osmosis.rows_per_shard = 1 task.source_data = TEST_FILE_ONE task.process() self.assertEqual(5, mock_create.call_count)
def test_finish_callback_deferred(self): task = ImportTask(shard_count=1, shards_processed=1) with mock.patch('django.db.models.Model.save') as save_patch: with mock.patch('google.appengine.ext.deferred.defer') as def_patch: task.save() self.assertTrue(save_patch.called) task.status = ImportStatus.IN_PROGRESS task.save() self.assertTrue(def_patch.called) with mock.patch('google.appengine.ext.deferred.defer') as def_patch: task.save() self.assertFalse(def_patch.called) #Already been called, so shouldn't happen again
def test_finish_callback_deferred(self): task = ImportTask(shard_count=1, shards_processed=1) with mock.patch('django.db.models.Model.save') as save_patch: with mock.patch( 'google.appengine.ext.deferred.defer') as def_patch: task.save() self.assertTrue(save_patch.called) task.status = ImportStatus.IN_PROGRESS task.save() self.assertTrue(def_patch.called)
def test_shards_processed_updated(self): task = ImportTask() shard1 = ImportShard(task_id=task.pk, task_model_path=task.model_path, id=1, source_data_json="[{}]", total_rows=1) shard2 = ImportShard(task_id=task.pk, task_model_path=task.model_path, id=2, source_data_json="[{}]", total_rows=1) def shard_get(*args, **kwargs): if kwargs.values()[0] == 1: return shard1 else: return shard2 with mock.patch('osmosis.models.ImportTask.import_row'): with mock.patch('osmosis.models.ImportTask.save'): with mock.patch('osmosis.models.ImportShard.save'): with mock.patch('osmosis.models.ImportShard.objects.get', autospec=True, side_effect=shard_get): with mock.patch( 'osmosis.models.ImportTask.objects.get', return_value=task): shard1.process() self.assertEqual(1, task.shards_processed) shard1.process() self.assertEqual( 1, task.shards_processed ) #If the shard retries, don't increment again shard2.process() self.assertEqual(2, task.shards_processed)