<s> import unittest <EOL> from google . appengine . ext import testbed <EOL> from mock import call <EOL> from mock import Mock <EOL> from mock import patch <EOL> class TestNew ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_new ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> from furious . context import new <EOL> self . assertIsInstance ( new ( ) , Context ) <EOL> def test_new_auto_context ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import AutoContext <EOL> from furious . context import new <EOL> batch_size = <NUM_LIT:100> <EOL> context = new ( batch_size = batch_size ) <EOL> self . assertIsInstance ( context , AutoContext ) <EOL> self . assertEqual ( context . batch_size , batch_size ) <EOL> def test_new_adds_to_registry ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> from furious . context . _local import get_local_context <EOL> from furious . context import new <EOL> ctx = new ( ) <EOL> self . assertIsInstance ( ctx , Context ) <EOL> self . assertIn ( ctx , get_local_context ( ) . registry ) <EOL> class TestContext ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> import os <EOL> import uuid <EOL> harness = testbed . Testbed ( ) <EOL> harness . activate ( ) <EOL> harness . init_taskqueue_stub ( ) <EOL> os . environ [ '<STR_LIT>' ] = uuid . uuid4 ( ) . hex <EOL> def test_context_works ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> with Context ( ) : <EOL> pass <EOL> def test_context_requires_insert_tasks ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> self . assertRaises ( TypeError , Context , insert_tasks = '<STR_LIT>' ) <EOL> def test_context_gets_id ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> self . assertTrue ( Context ( ) . id ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_id_added_to_options ( self , uuid_patch ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> id = '<STR_LIT>' <EOL> uuid_patch . return_value . hex = id <EOL> context = Context ( ) <EOL> self . assertEqual ( context . id , id ) <EOL> self . assertEqual ( context . _options [ '<STR_LIT:id>' ] , id ) <EOL> def test_context_gets_one_id ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> context = Context ( ) <EOL> id1 = context . id <EOL> id2 = context . id <EOL> self . assertEqual ( id1 , id2 ) <EOL> self . assertEqual ( context . id , id1 ) <EOL> def test_context_gets_assigned_id ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> self . assertEqual ( '<STR_LIT>' , Context ( id = '<STR_LIT>' ) . id ) <EOL> def test_insert_success ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> self . assertEqual ( <NUM_LIT:0> , Context ( ) . insert_success ) <EOL> def test_insert_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> self . assertEqual ( <NUM_LIT:0> , Context ( ) . insert_failed ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_add_job_to_context_works ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . async import Async <EOL> from furious . context import Context <EOL> with Context ( ) as ctx : <EOL> job = ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertIsInstance ( job , Async ) <EOL> self . assertEqual ( <NUM_LIT:1> , ctx . insert_success ) <EOL> self . assertEqual ( <NUM_LIT:1> , queue_add_mock . call_count ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_bubbling_exceptions ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> class TestError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( ) : <EOL> with Context ( ) as ctx : <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> raise TestError ( '<STR_LIT>' ) <EOL> self . assertRaises ( TestError , wrapper ) <EOL> self . assertEqual ( <NUM_LIT:0> , queue_add_mock . call_count ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_nested_context_works ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . async import Async <EOL> from furious . context import Context <EOL> with Context ( ) as ctx : <EOL> job = ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> with Context ( ) as ctx2 : <EOL> job2 = ctx2 . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertIsInstance ( job , Async ) <EOL> self . assertIsInstance ( job2 , Async ) <EOL> self . assertEqual ( <NUM_LIT:1> , ctx . insert_success ) <EOL> self . assertEqual ( <NUM_LIT:1> , ctx2 . insert_success ) <EOL> self . assertEqual ( <NUM_LIT:2> , queue_add_mock . call_count ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_add_multiple_jobs_to_context_works ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> with Context ( ) as ctx : <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , queue_add_mock . call_count ) <EOL> self . assertEqual ( <NUM_LIT:10> , len ( queue_add_mock . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:10> , ctx . insert_success ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_added_asyncs_get_context_id ( self , queue_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . async import Async <EOL> from furious . context import Context <EOL> asyncs = [ Async ( '<STR_LIT:test>' , id = i ) for i in xrange ( <NUM_LIT:100> , <NUM_LIT> ) ] <EOL> with Context ( ) as ctx : <EOL> for async in asyncs : <EOL> ctx . add ( async ) <EOL> self . assertEqual ( ctx . id , async . get_options ( ) [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT:10> , ctx . insert_success ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_added_to_correct_queue ( self , queue_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> with Context ( ) as ctx : <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:A>' ) <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:A>' ) <EOL> queue_mock . assert_called_once_with ( name = '<STR_LIT:A>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , ctx . insert_success ) <EOL> def test_add_jobs_to_multiple_queues ( self ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . api . taskqueue import Queue <EOL> from furious . context import Context <EOL> queue_registry = { } <EOL> class AwesomeQueue ( Queue ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AwesomeQueue , self ) . __init__ ( * args , ** kwargs ) <EOL> queue_registry [ kwargs . get ( '<STR_LIT:name>' ) ] = self <EOL> self . _calls = [ ] <EOL> def add ( self , * args , ** kwargs ) : <EOL> self . _calls . append ( ( args , kwargs ) ) <EOL> with patch ( '<STR_LIT>' , AwesomeQueue ) : <EOL> with Context ( ) as ctx : <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:A>' ) <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:A>' ) <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:B>' ) <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:C>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( queue_registry [ '<STR_LIT:A>' ] . _calls [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( queue_registry [ '<STR_LIT:B>' ] . _calls [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( queue_registry [ '<STR_LIT:C>' ] . _calls [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , ctx . insert_success ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_add_task_fails ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . api . taskqueue import TaskAlreadyExistsError <EOL> from furious . context import Context <EOL> def queue_add ( tasks , transactional = False ) : <EOL> if len ( tasks ) != <NUM_LIT:2> : <EOL> raise TaskAlreadyExistsError ( ) <EOL> queue_add_mock . side_effect = queue_add <EOL> with Context ( ) as ctx : <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:A>' ) <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:B>' ) <EOL> ctx . add ( '<STR_LIT:test>' , args = [ <NUM_LIT:1> , <NUM_LIT:2> ] , queue = '<STR_LIT:B>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , ctx . insert_success ) <EOL> self . assertEqual ( <NUM_LIT:1> , ctx . insert_failed ) <EOL> def test_to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> import copy <EOL> from furious . context import Context <EOL> options = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:id>' : '<STR_LIT>' <EOL> } <EOL> context = Context ( ** copy . deepcopy ( options ) ) <EOL> options . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:id>' : '<STR_LIT>' <EOL> } ) <EOL> self . assertEqual ( options , context . to_dict ( ) ) <EOL> def test_to_dict_with_callbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> import copy <EOL> from furious . async import Async <EOL> from furious . context import Context <EOL> options = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:success>' : self . __class__ . test_to_dict_with_callbacks , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : Async ( target = dir , id = '<STR_LIT>' , context_id = '<STR_LIT>' , <EOL> parent_id = '<STR_LIT>' ) <EOL> } <EOL> } <EOL> context = Context ( ** copy . deepcopy ( options ) ) <EOL> options . update ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:success>' : ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : { '<STR_LIT>' : ( '<STR_LIT>' , None , None ) , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:100> } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> } ) <EOL> self . assertEqual ( options , context . to_dict ( ) ) <EOL> def test_from_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> from furious . context . context import _insert_tasks <EOL> options = { <EOL> '<STR_LIT:id>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> context = Context . from_dict ( options ) <EOL> self . assertEqual ( <NUM_LIT> , context . id ) <EOL> self . assertEqual ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , context . task_ids ) <EOL> self . assertEqual ( True , context . _tasks_inserted ) <EOL> self . assertEqual ( '<STR_LIT>' , context . _options . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( _insert_tasks , context . _insert_tasks ) <EOL> self . assertEqual ( Context , context . _persistence_engine ) <EOL> def test_from_dict_with_callbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> callbacks = { <EOL> '<STR_LIT:success>' : ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : { '<STR_LIT>' : ( '<STR_LIT:id>' , None , None ) , '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> context = Context . from_dict ( { '<STR_LIT>' : callbacks } ) <EOL> check_callbacks = { <EOL> '<STR_LIT:success>' : TestContext . test_to_dict_with_callbacks , <EOL> '<STR_LIT>' : dir <EOL> } <EOL> callbacks = context . _options . get ( '<STR_LIT>' ) <EOL> exec_callback = callbacks . pop ( '<STR_LIT>' ) <EOL> correct_dict = { '<STR_LIT>' : ( '<STR_LIT:id>' , None , None ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:100> } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> self . assertEqual ( check_callbacks , callbacks ) <EOL> self . assertEqual ( correct_dict , exec_callback . to_dict ( ) ) <EOL> def test_reconstitution ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> options = { <EOL> '<STR_LIT:id>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> context = Context . from_dict ( options ) <EOL> self . assertEqual ( options , context . to_dict ( ) ) <EOL> def test_persist_with_no_engine ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> context = Context ( ) <EOL> self . assertRaises ( RuntimeError , context . persist ) <EOL> def test_persist_persists ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> persistence_engine = Mock ( ) <EOL> persistence_engine . func_name = '<STR_LIT>' <EOL> persistence_engine . im_class . __name__ = '<STR_LIT>' <EOL> context = Context ( persistence_engine = persistence_engine ) <EOL> context . persist ( ) <EOL> persistence_engine . store_context . assert_called_once_with ( context ) <EOL> def test_load_context ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import Context <EOL> persistence_engine = Mock ( ) <EOL> persistence_engine . func_name = '<STR_LIT>' <EOL> persistence_engine . im_class . __name__ = '<STR_LIT>' <EOL> persistence_engine . load_context . return_value = Context . from_dict ( <EOL> { '<STR_LIT:id>' : '<STR_LIT>' } ) <EOL> context = Context . load ( '<STR_LIT>' , persistence_engine ) <EOL> persistence_engine . load_context . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , context . id ) <EOL> class TestInsertTasks ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> harness = testbed . Testbed ( ) <EOL> harness . activate ( ) <EOL> harness . init_taskqueue_stub ( ) <EOL> def test_no_tasks_doesnt_blow_up ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> inserted = _insert_tasks ( ( ) , '<STR_LIT:A>' ) <EOL> self . assertEqual ( <NUM_LIT:0> , inserted ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_queue_name_is_honored ( self , queue_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> inserted = _insert_tasks ( ( None , ) , '<STR_LIT>' ) <EOL> queue_mock . assert_called_once_with ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , inserted ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_tasks_are_passed_along ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> inserted = _insert_tasks ( ( '<STR_LIT:A>' , <NUM_LIT:1> , '<STR_LIT:B>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> queue_add_mock . assert_called_once_with ( ( '<STR_LIT:A>' , <NUM_LIT:1> , '<STR_LIT:B>' , '<STR_LIT>' ) , <EOL> transactional = False ) <EOL> self . assertEqual ( <NUM_LIT:4> , inserted ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_task_add_error_TransientError ( self , queue_add_mock , mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> queue_add_mock . side_effect = taskqueue . TransientError <EOL> self . assertRaises ( <EOL> taskqueue . TransientError , <EOL> _insert_tasks , ( '<STR_LIT:A>' , ) , '<STR_LIT>' , retry_transient_errors = False <EOL> ) <EOL> queue_add_mock . assert_called_once_with ( ( '<STR_LIT:A>' , ) , transactional = False ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_task_add_error_TransientError_with_delay ( self , queue_add_mock , <EOL> mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> queue_add_mock . side_effect = taskqueue . TransientError <EOL> tasks = [ taskqueue . Task ( '<STR_LIT:A>' ) ] <EOL> self . assertRaises ( <EOL> taskqueue . TransientError , <EOL> _insert_tasks , tasks , '<STR_LIT>' , retry_transient_errors = True , <EOL> retry_delay = <NUM_LIT:12> <EOL> ) <EOL> self . assertEqual ( queue_add_mock . call_count , <NUM_LIT:2> ) <EOL> mock_sleep . assert_called_once_with ( <NUM_LIT:12> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_task_add_error_TransientError_transactional ( self , queue_add_mock , <EOL> mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> queue_add_mock . side_effect = taskqueue . TransientError <EOL> tasks = [ taskqueue . Task ( '<STR_LIT:A>' ) ] <EOL> self . assertRaises ( <EOL> taskqueue . TransientError , <EOL> _insert_tasks , tasks , '<STR_LIT>' , <EOL> transactional = True , <EOL> retry_transient_errors = True , <EOL> retry_delay = <NUM_LIT:1> , <EOL> ) <EOL> self . assertEqual ( queue_add_mock . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( mock_sleep . call_count , <NUM_LIT:0> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_batches_get_split_TransientError ( self , queue_add_mock , mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> queue_add_mock . side_effect = ( taskqueue . TransientError , None ) <EOL> tasks = ( taskqueue . Task ( '<STR_LIT:A>' ) , taskqueue . Task ( '<STR_LIT:1>' ) , taskqueue . Task ( '<STR_LIT:B>' ) ) <EOL> inserted = _insert_tasks ( tasks , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , queue_add_mock . call_count ) <EOL> self . assertEqual ( <NUM_LIT:3> , inserted ) <EOL> self . assertEqual ( mock_sleep . call_count , <NUM_LIT:1> ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_task_add_error_BadTaskStateError ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> def raise_error ( * args , ** kwargs ) : <EOL> from google . appengine . api import taskqueue <EOL> raise taskqueue . BadTaskStateError ( ) <EOL> queue_add_mock . side_effect = raise_error <EOL> inserted = _insert_tasks ( ( '<STR_LIT:A>' , ) , '<STR_LIT>' ) <EOL> queue_add_mock . assert_called_once_with ( ( '<STR_LIT:A>' , ) , transactional = False ) <EOL> self . assertEqual ( <NUM_LIT:0> , inserted ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_batches_get_split_BadTaskStateError ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> def raise_error ( * args , ** kwargs ) : <EOL> raise taskqueue . BadTaskStateError ( ) <EOL> queue_add_mock . side_effect = raise_error <EOL> tasks = ( taskqueue . Task ( '<STR_LIT:A>' ) , taskqueue . Task ( '<STR_LIT:1>' ) , taskqueue . Task ( '<STR_LIT:B>' ) ) <EOL> inserted = _insert_tasks ( tasks , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:5> , queue_add_mock . call_count ) <EOL> self . assertEqual ( <NUM_LIT:0> , inserted ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_task_add_error_TaskAlreadyExistsError ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> def raise_error ( * args , ** kwargs ) : <EOL> from google . appengine . api import taskqueue <EOL> raise taskqueue . TaskAlreadyExistsError ( ) <EOL> queue_add_mock . side_effect = raise_error <EOL> inserted = _insert_tasks ( ( '<STR_LIT:A>' , ) , '<STR_LIT>' ) <EOL> queue_add_mock . assert_called_once_with ( ( '<STR_LIT:A>' , ) , transactional = False ) <EOL> self . assertEqual ( <NUM_LIT:0> , inserted ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_batches_get_split_TaskAlreadyExistsError ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> def raise_error ( * args , ** kwargs ) : <EOL> raise taskqueue . TaskAlreadyExistsError ( ) <EOL> queue_add_mock . side_effect = raise_error <EOL> tasks = ( taskqueue . Task ( '<STR_LIT:A>' ) , taskqueue . Task ( '<STR_LIT:1>' ) , taskqueue . Task ( '<STR_LIT:B>' ) ) <EOL> inserted = _insert_tasks ( tasks , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:5> , queue_add_mock . call_count ) <EOL> self . assertEqual ( <NUM_LIT:0> , inserted ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_task_add_error_TombstonedTaskError ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> def raise_error ( * args , ** kwargs ) : <EOL> from google . appengine . api import taskqueue <EOL> raise taskqueue . TombstonedTaskError ( ) <EOL> queue_add_mock . side_effect = raise_error <EOL> inserted = _insert_tasks ( ( '<STR_LIT:A>' , ) , '<STR_LIT>' ) <EOL> queue_add_mock . assert_called_once_with ( ( '<STR_LIT:A>' , ) , transactional = False ) <EOL> self . assertEqual ( <NUM_LIT:0> , inserted ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_batches_get_split_TombstonedTaskError ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> def raise_error ( * args , ** kwargs ) : <EOL> raise taskqueue . TombstonedTaskError ( ) <EOL> queue_add_mock . side_effect = raise_error <EOL> tasks = ( taskqueue . Task ( '<STR_LIT:A>' ) , taskqueue . Task ( '<STR_LIT:1>' ) , taskqueue . Task ( '<STR_LIT:B>' ) ) <EOL> inserted = _insert_tasks ( tasks , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:5> , queue_add_mock . call_count ) <EOL> self . assertEqual ( <NUM_LIT:0> , inserted ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_single_task_gets_retried ( self , queue_add_mock , mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> queue_add_mock . side_effect = ( taskqueue . TransientError , None ) <EOL> inserted = _insert_tasks ( ( taskqueue . Task ( '<STR_LIT:A>' ) , ) , '<STR_LIT>' ) <EOL> self . assertEqual ( queue_add_mock . call_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( inserted , <NUM_LIT:1> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_tasks_not_enqueued_get_retried ( self , queue_add_mock , mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> queue_add_mock . side_effect = ( taskqueue . TransientError , None ) <EOL> tasks = ( Mock ( ) , taskqueue . Task ( '<STR_LIT:1>' ) , taskqueue . Task ( '<STR_LIT:B>' ) ) <EOL> tasks [ <NUM_LIT:0> ] . was_enqueued = True <EOL> inserted = _insert_tasks ( tasks , '<STR_LIT>' ) <EOL> calls = [ <EOL> call ( tasks , transactional = False ) , <EOL> call ( [ tasks [ <NUM_LIT:1> ] , tasks [ <NUM_LIT:2> ] ] , transactional = False ) <EOL> ] <EOL> self . assertEqual ( queue_add_mock . call_args_list , calls ) <EOL> self . assertEqual ( inserted , <NUM_LIT:3> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_single_task_reraises_after_failure ( self , queue_add_mock , <EOL> mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> queue_add_mock . side_effect = taskqueue . TransientError <EOL> self . assertRaises ( taskqueue . TransientError , <EOL> _insert_tasks , ( taskqueue . Task ( '<STR_LIT:A>' ) , ) , '<STR_LIT>' ) <EOL> self . assertEqual ( queue_add_mock . call_count , <NUM_LIT:2> ) <EOL> @ patch ( '<STR_LIT>' , auto_spec = True ) <EOL> def test_batches_get_split_dont_reinsert_enqueued ( self , queue_add_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _insert_tasks <EOL> from google . appengine . api import taskqueue <EOL> def raise_error ( * args , ** kwargs ) : <EOL> raise taskqueue . TombstonedTaskError ( ) <EOL> queue_add_mock . side_effect = raise_error <EOL> tasks = ( Mock ( ) , taskqueue . Task ( '<STR_LIT:1>' ) , taskqueue . Task ( '<STR_LIT:B>' ) ) <EOL> tasks [ <NUM_LIT:0> ] . was_enqueued = True <EOL> inserted = _insert_tasks ( tasks , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:3> , queue_add_mock . call_count ) <EOL> self . assertEqual ( <NUM_LIT:1> , inserted ) <EOL> class TestTaskBatcher ( unittest . TestCase ) : <EOL> def test_no_tasks ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _task_batcher <EOL> self . assertEqual ( [ ] , list ( _task_batcher ( [ ] ) ) ) <EOL> def test_one_task ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _task_batcher <EOL> tasks = [ <NUM_LIT:1> ] <EOL> result = list ( _task_batcher ( tasks ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result [ <NUM_LIT:0> ] ) ) <EOL> def test_less_than_100_tasks ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _task_batcher <EOL> tasks = '<STR_LIT:a>' * <NUM_LIT> <EOL> result = list ( _task_batcher ( tasks ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result ) ) <EOL> self . assertEqual ( len ( tasks ) , len ( result [ <NUM_LIT:0> ] ) ) <EOL> def test_more_than_100_tasks ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _task_batcher <EOL> tasks = '<STR_LIT:a>' * <NUM_LIT> <EOL> result = list ( _task_batcher ( tasks ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result ) ) <EOL> self . assertEqual ( <NUM_LIT:100> , len ( result [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result [ <NUM_LIT:1> ] ) ) <EOL> def test_tasks_with_small_batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _task_batcher <EOL> tasks = '<STR_LIT:a>' * <NUM_LIT> <EOL> batch_size = <NUM_LIT:30> <EOL> result = list ( _task_batcher ( tasks , batch_size = batch_size ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( result ) ) <EOL> self . assertEqual ( <NUM_LIT:30> , len ( result [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:30> , len ( result [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:30> , len ( result [ <NUM_LIT:2> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:11> , len ( result [ <NUM_LIT:3> ] ) ) <EOL> def test_more_than_100_tasks_with_large_batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . context . context import _task_batcher <EOL> tasks = '<STR_LIT:a>' * <NUM_LIT> <EOL> batch_size = <NUM_LIT> <EOL> result = list ( _task_batcher ( tasks , batch_size = batch_size ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result ) ) <EOL> self . assertEqual ( <NUM_LIT:100> , len ( result [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result [ <NUM_LIT:1> ] ) ) </s>
