<s> import unittest2 <EOL> def _make_entity_pb ( project , kind , integer_id , name = None , str_val = None ) : <EOL> from gcloud . datastore . _generated import entity_pb2 <EOL> from gcloud . datastore . helpers import _new_value_pb <EOL> entity_pb = entity_pb2 . Entity ( ) <EOL> entity_pb . key . partition_id . project_id = project <EOL> path_element = entity_pb . key . path . add ( ) <EOL> path_element . kind = kind <EOL> path_element . id = integer_id <EOL> if name is not None and str_val is not None : <EOL> value_pb = _new_value_pb ( entity_pb , name ) <EOL> value_pb . string_value = str_val <EOL> return entity_pb <EOL> class Test__get_gcd_project ( unittest2 . TestCase ) : <EOL> def _callFUT ( self ) : <EOL> from gcloud . datastore . client import _get_gcd_project <EOL> return _get_gcd_project ( ) <EOL> def test_no_value ( self ) : <EOL> import os <EOL> from gcloud . _testing import _Monkey <EOL> environ = { } <EOL> with _Monkey ( os , getenv = environ . get ) : <EOL> project = self . _callFUT ( ) <EOL> self . assertEqual ( project , None ) <EOL> def test_value_set ( self ) : <EOL> import os <EOL> from gcloud . _testing import _Monkey <EOL> from gcloud . datastore . client import GCD_DATASET <EOL> MOCK_PROJECT = object ( ) <EOL> environ = { GCD_DATASET : MOCK_PROJECT } <EOL> with _Monkey ( os , getenv = environ . get ) : <EOL> project = self . _callFUT ( ) <EOL> self . assertEqual ( project , MOCK_PROJECT ) <EOL> class Test__determine_default_project ( unittest2 . TestCase ) : <EOL> def _callFUT ( self , project = None ) : <EOL> from gcloud . datastore . client import ( <EOL> _determine_default_project ) <EOL> return _determine_default_project ( project = project ) <EOL> def _determine_default_helper ( self , gcd = None , fallback = None , <EOL> project_called = None ) : <EOL> from gcloud . _testing import _Monkey <EOL> from gcloud . datastore import client <EOL> _callers = [ ] <EOL> def gcd_mock ( ) : <EOL> _callers . append ( '<STR_LIT>' ) <EOL> return gcd <EOL> def fallback_mock ( project = None ) : <EOL> _callers . append ( ( '<STR_LIT>' , project ) ) <EOL> return fallback <EOL> patched_methods = { <EOL> '<STR_LIT>' : gcd_mock , <EOL> '<STR_LIT>' : fallback_mock , <EOL> } <EOL> with _Monkey ( client , ** patched_methods ) : <EOL> returned_project = self . _callFUT ( project_called ) <EOL> return returned_project , _callers <EOL> def test_no_value ( self ) : <EOL> project , callers = self . _determine_default_helper ( ) <EOL> self . assertEqual ( project , None ) <EOL> self . assertEqual ( callers , [ '<STR_LIT>' , ( '<STR_LIT>' , None ) ] ) <EOL> def test_explicit ( self ) : <EOL> PROJECT = object ( ) <EOL> project , callers = self . _determine_default_helper ( <EOL> project_called = PROJECT ) <EOL> self . assertEqual ( project , PROJECT ) <EOL> self . assertEqual ( callers , [ ] ) <EOL> def test_gcd ( self ) : <EOL> PROJECT = object ( ) <EOL> project , callers = self . _determine_default_helper ( gcd = PROJECT ) <EOL> self . assertEqual ( project , PROJECT ) <EOL> self . assertEqual ( callers , [ '<STR_LIT>' ] ) <EOL> def test_fallback ( self ) : <EOL> PROJECT = object ( ) <EOL> project , callers = self . _determine_default_helper ( fallback = PROJECT ) <EOL> self . assertEqual ( project , PROJECT ) <EOL> self . assertEqual ( callers , [ '<STR_LIT>' , ( '<STR_LIT>' , None ) ] ) <EOL> class TestClient ( unittest2 . TestCase ) : <EOL> PROJECT = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> KLASS = self . _getTargetClass ( ) <EOL> self . original_cnxn_class = KLASS . _connection_class <EOL> KLASS . _connection_class = _MockConnection <EOL> def tearDown ( self ) : <EOL> KLASS = self . _getTargetClass ( ) <EOL> KLASS . _connection_class = self . original_cnxn_class <EOL> def _getTargetClass ( self ) : <EOL> from gcloud . datastore . client import Client <EOL> return Client <EOL> def _makeOne ( self , project = PROJECT , namespace = None , <EOL> credentials = None , http = None ) : <EOL> return self . _getTargetClass ( ) ( project = project , <EOL> namespace = namespace , <EOL> credentials = credentials , <EOL> http = http ) <EOL> def test_ctor_w_project_no_environ ( self ) : <EOL> from gcloud . _testing import _Monkey <EOL> from gcloud . datastore import client as _MUT <EOL> with _Monkey ( _MUT , _base_default_project = lambda project : None ) : <EOL> self . assertRaises ( EnvironmentError , self . _makeOne , None ) <EOL> def test_ctor_w_implicit_inputs ( self ) : <EOL> from gcloud . _testing import _Monkey <EOL> from gcloud . datastore import client as _MUT <EOL> from gcloud import client as _base_client <EOL> OTHER = '<STR_LIT>' <EOL> creds = object ( ) <EOL> default_called = [ ] <EOL> def fallback_mock ( project ) : <EOL> default_called . append ( project ) <EOL> return project or OTHER <EOL> klass = self . _getTargetClass ( ) <EOL> with _Monkey ( _MUT , <EOL> _determine_default_project = fallback_mock ) : <EOL> with _Monkey ( _base_client , <EOL> get_credentials = lambda : creds ) : <EOL> client = klass ( ) <EOL> self . assertEqual ( client . project , OTHER ) <EOL> self . assertEqual ( client . namespace , None ) <EOL> self . assertTrue ( isinstance ( client . connection , _MockConnection ) ) <EOL> self . assertTrue ( client . connection . credentials is creds ) <EOL> self . assertTrue ( client . connection . http is None ) <EOL> self . assertTrue ( client . current_batch is None ) <EOL> self . assertTrue ( client . current_transaction is None ) <EOL> self . assertEqual ( default_called , [ None ] ) <EOL> def test_ctor_w_explicit_inputs ( self ) : <EOL> OTHER = '<STR_LIT>' <EOL> NAMESPACE = '<STR_LIT>' <EOL> creds = object ( ) <EOL> http = object ( ) <EOL> client = self . _makeOne ( project = OTHER , <EOL> namespace = NAMESPACE , <EOL> credentials = creds , <EOL> http = http ) <EOL> self . assertEqual ( client . project , OTHER ) <EOL> self . assertEqual ( client . namespace , NAMESPACE ) <EOL> self . assertTrue ( isinstance ( client . connection , _MockConnection ) ) <EOL> self . assertTrue ( client . connection . credentials is creds ) <EOL> self . assertTrue ( client . connection . http is http ) <EOL> self . assertTrue ( client . current_batch is None ) <EOL> self . assertEqual ( list ( client . _batch_stack ) , [ ] ) <EOL> def test__push_batch_and__pop_batch ( self ) : <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> batch = client . batch ( ) <EOL> xact = client . transaction ( ) <EOL> client . _push_batch ( batch ) <EOL> self . assertEqual ( list ( client . _batch_stack ) , [ batch ] ) <EOL> self . assertTrue ( client . current_batch is batch ) <EOL> self . assertTrue ( client . current_transaction is None ) <EOL> client . _push_batch ( xact ) <EOL> self . assertTrue ( client . current_batch is xact ) <EOL> self . assertTrue ( client . current_transaction is xact ) <EOL> self . assertEqual ( list ( client . _batch_stack ) , [ xact , batch ] ) <EOL> self . assertTrue ( client . _pop_batch ( ) is xact ) <EOL> self . assertEqual ( list ( client . _batch_stack ) , [ batch ] ) <EOL> self . assertTrue ( client . _pop_batch ( ) is batch ) <EOL> self . assertEqual ( list ( client . _batch_stack ) , [ ] ) <EOL> def test_get_miss ( self ) : <EOL> _called_with = [ ] <EOL> def _get_multi ( * args , ** kw ) : <EOL> _called_with . append ( ( args , kw ) ) <EOL> return [ ] <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . get_multi = _get_multi <EOL> key = object ( ) <EOL> self . assertTrue ( client . get ( key ) is None ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ( ) ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , [ key ] ) <EOL> self . assertTrue ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] is None ) <EOL> self . assertTrue ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] is None ) <EOL> def test_get_hit ( self ) : <EOL> _called_with = [ ] <EOL> _entity = object ( ) <EOL> def _get_multi ( * args , ** kw ) : <EOL> _called_with . append ( ( args , kw ) ) <EOL> return [ _entity ] <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . get_multi = _get_multi <EOL> key , missing , deferred = object ( ) , [ ] , [ ] <EOL> self . assertTrue ( client . get ( key , missing , deferred ) is _entity ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ( ) ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , [ key ] ) <EOL> self . assertTrue ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] is missing ) <EOL> self . assertTrue ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] is deferred ) <EOL> def test_get_multi_no_keys ( self ) : <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> results = client . get_multi ( [ ] ) <EOL> self . assertEqual ( results , [ ] ) <EOL> def test_get_multi_miss ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _add_lookup_result ( ) <EOL> key = Key ( '<STR_LIT>' , <NUM_LIT> , project = self . PROJECT ) <EOL> results = client . get_multi ( [ key ] ) <EOL> self . assertEqual ( results , [ ] ) <EOL> def test_get_multi_miss_w_missing ( self ) : <EOL> from gcloud . datastore . _generated import entity_pb2 <EOL> from gcloud . datastore . key import Key <EOL> KIND = '<STR_LIT>' <EOL> ID = <NUM_LIT> <EOL> missed = entity_pb2 . Entity ( ) <EOL> missed . key . partition_id . project_id = self . PROJECT <EOL> path_element = missed . key . path . add ( ) <EOL> path_element . kind = KIND <EOL> path_element . id = ID <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _add_lookup_result ( missing = [ missed ] ) <EOL> key = Key ( KIND , ID , project = self . PROJECT ) <EOL> missing = [ ] <EOL> entities = client . get_multi ( [ key ] , missing = missing ) <EOL> self . assertEqual ( entities , [ ] ) <EOL> self . assertEqual ( [ missed . key . to_protobuf ( ) for missed in missing ] , <EOL> [ key . to_protobuf ( ) ] ) <EOL> def test_get_multi_w_missing_non_empty ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> key = Key ( '<STR_LIT>' , <NUM_LIT> , project = self . PROJECT ) <EOL> missing = [ '<STR_LIT>' , '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , client . get_multi , <EOL> [ key ] , missing = missing ) <EOL> def test_get_multi_w_deferred_non_empty ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> key = Key ( '<STR_LIT>' , <NUM_LIT> , project = self . PROJECT ) <EOL> deferred = [ '<STR_LIT>' , '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , client . get_multi , <EOL> [ key ] , deferred = deferred ) <EOL> def test_get_multi_miss_w_deferred ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> key = Key ( '<STR_LIT>' , <NUM_LIT> , project = self . PROJECT ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _add_lookup_result ( deferred = [ key . to_protobuf ( ) ] ) <EOL> deferred = [ ] <EOL> entities = client . get_multi ( [ key ] , deferred = deferred ) <EOL> self . assertEqual ( entities , [ ] ) <EOL> self . assertEqual ( [ def_key . to_protobuf ( ) for def_key in deferred ] , <EOL> [ key . to_protobuf ( ) ] ) <EOL> def test_get_multi_w_deferred_from_backend_but_not_passed ( self ) : <EOL> from gcloud . datastore . _generated import entity_pb2 <EOL> from gcloud . datastore . entity import Entity <EOL> from gcloud . datastore . key import Key <EOL> key1 = Key ( '<STR_LIT>' , project = self . PROJECT ) <EOL> key1_pb = key1 . to_protobuf ( ) <EOL> key2 = Key ( '<STR_LIT>' , <NUM_LIT> , project = self . PROJECT ) <EOL> key2_pb = key2 . to_protobuf ( ) <EOL> entity1_pb = entity_pb2 . Entity ( ) <EOL> entity1_pb . key . CopyFrom ( key1_pb ) <EOL> entity2_pb = entity_pb2 . Entity ( ) <EOL> entity2_pb . key . CopyFrom ( key2_pb ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _add_lookup_result ( [ entity1_pb ] , deferred = [ key2_pb ] ) <EOL> client . connection . _add_lookup_result ( [ entity2_pb ] ) <EOL> missing = [ ] <EOL> found = client . get_multi ( [ key1 , key2 ] , missing = missing ) <EOL> self . assertEqual ( len ( found ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( missing ) , <NUM_LIT:0> ) <EOL> self . assertTrue ( isinstance ( found [ <NUM_LIT:0> ] , Entity ) ) <EOL> self . assertEqual ( found [ <NUM_LIT:0> ] . key . path , key1 . path ) <EOL> self . assertEqual ( found [ <NUM_LIT:0> ] . key . project , key1 . project ) <EOL> self . assertTrue ( isinstance ( found [ <NUM_LIT:1> ] , Entity ) ) <EOL> self . assertEqual ( found [ <NUM_LIT:1> ] . key . path , key2 . path ) <EOL> self . assertEqual ( found [ <NUM_LIT:1> ] . key . project , key2 . project ) <EOL> cw = client . connection . _lookup_cw <EOL> self . assertEqual ( len ( cw ) , <NUM_LIT:2> ) <EOL> ds_id , k_pbs , eventual , tid = cw [ <NUM_LIT:0> ] <EOL> self . assertEqual ( ds_id , self . PROJECT ) <EOL> self . assertEqual ( len ( k_pbs ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( key1_pb , k_pbs [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( key2_pb , k_pbs [ <NUM_LIT:1> ] ) <EOL> self . assertFalse ( eventual ) <EOL> self . assertTrue ( tid is None ) <EOL> ds_id , k_pbs , eventual , tid = cw [ <NUM_LIT:1> ] <EOL> self . assertEqual ( ds_id , self . PROJECT ) <EOL> self . assertEqual ( len ( k_pbs ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( key2_pb , k_pbs [ <NUM_LIT:0> ] ) <EOL> self . assertFalse ( eventual ) <EOL> self . assertTrue ( tid is None ) <EOL> def test_get_multi_hit ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> KIND = '<STR_LIT>' <EOL> ID = <NUM_LIT> <EOL> PATH = [ { '<STR_LIT>' : KIND , '<STR_LIT:id>' : ID } ] <EOL> entity_pb = _make_entity_pb ( self . PROJECT , KIND , ID , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _add_lookup_result ( [ entity_pb ] ) <EOL> key = Key ( KIND , ID , project = self . PROJECT ) <EOL> result , = client . get_multi ( [ key ] ) <EOL> new_key = result . key <EOL> self . assertFalse ( new_key is key ) <EOL> self . assertEqual ( new_key . project , self . PROJECT ) <EOL> self . assertEqual ( new_key . path , PATH ) <EOL> self . assertEqual ( list ( result ) , [ '<STR_LIT:foo>' ] ) <EOL> self . assertEqual ( result [ '<STR_LIT:foo>' ] , '<STR_LIT>' ) <EOL> def test_get_multi_hit_multiple_keys_same_project ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> KIND = '<STR_LIT>' <EOL> ID1 = <NUM_LIT> <EOL> ID2 = <NUM_LIT> <EOL> entity_pb1 = _make_entity_pb ( self . PROJECT , KIND , ID1 ) <EOL> entity_pb2 = _make_entity_pb ( self . PROJECT , KIND , ID2 ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _add_lookup_result ( [ entity_pb1 , entity_pb2 ] ) <EOL> key1 = Key ( KIND , ID1 , project = self . PROJECT ) <EOL> key2 = Key ( KIND , ID2 , project = self . PROJECT ) <EOL> retrieved1 , retrieved2 = client . get_multi ( [ key1 , key2 ] ) <EOL> self . assertEqual ( retrieved1 . key . path , key1 . path ) <EOL> self . assertEqual ( dict ( retrieved1 ) , { } ) <EOL> self . assertEqual ( retrieved2 . key . path , key2 . path ) <EOL> self . assertEqual ( dict ( retrieved2 ) , { } ) <EOL> def test_get_multi_hit_multiple_keys_different_project ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> PROJECT1 = '<STR_LIT>' <EOL> PROJECT2 = '<STR_LIT>' <EOL> self . assertNotEqual ( PROJECT1 , PROJECT2 ) <EOL> key1 = Key ( '<STR_LIT>' , <NUM_LIT> , project = PROJECT1 ) <EOL> key2 = Key ( '<STR_LIT>' , <NUM_LIT> , project = PROJECT2 ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> with self . assertRaises ( ValueError ) : <EOL> client . get_multi ( [ key1 , key2 ] ) <EOL> def test_get_multi_max_loops ( self ) : <EOL> from gcloud . _testing import _Monkey <EOL> from gcloud . datastore import client as _MUT <EOL> from gcloud . datastore . key import Key <EOL> KIND = '<STR_LIT>' <EOL> ID = <NUM_LIT> <EOL> entity_pb = _make_entity_pb ( self . PROJECT , KIND , ID , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _add_lookup_result ( [ entity_pb ] ) <EOL> key = Key ( KIND , ID , project = self . PROJECT ) <EOL> deferred = [ ] <EOL> missing = [ ] <EOL> with _Monkey ( _MUT , _MAX_LOOPS = - <NUM_LIT:1> ) : <EOL> result = client . get_multi ( [ key ] , missing = missing , <EOL> deferred = deferred ) <EOL> self . assertEqual ( result , [ ] ) <EOL> self . assertEqual ( missing , [ ] ) <EOL> self . assertEqual ( deferred , [ ] ) <EOL> def test_put ( self ) : <EOL> _called_with = [ ] <EOL> def _put_multi ( * args , ** kw ) : <EOL> _called_with . append ( ( args , kw ) ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . put_multi = _put_multi <EOL> entity = object ( ) <EOL> client . put ( entity ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ( ) ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , [ entity ] ) <EOL> def test_put_multi_no_entities ( self ) : <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> self . assertEqual ( client . put_multi ( [ ] ) , None ) <EOL> def test_put_multi_w_single_empty_entity ( self ) : <EOL> from gcloud . datastore . entity import Entity <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> self . assertRaises ( ValueError , client . put_multi , Entity ( ) ) <EOL> def test_put_multi_no_batch_w_partial_key ( self ) : <EOL> from gcloud . datastore . helpers import _property_tuples <EOL> from gcloud . datastore . test_batch import _Entity <EOL> from gcloud . datastore . test_batch import _Key <EOL> from gcloud . datastore . test_batch import _KeyPB <EOL> from gcloud . datastore . test_batch import _mutated_pb <EOL> entity = _Entity ( foo = u'<STR_LIT:bar>' ) <EOL> key = entity . key = _Key ( self . PROJECT ) <EOL> key . _id = None <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _commit . append ( [ _KeyPB ( key ) ] ) <EOL> result = client . put_multi ( [ entity ] ) <EOL> self . assertTrue ( result is None ) <EOL> self . assertEqual ( len ( client . connection . _commit_cw ) , <NUM_LIT:1> ) <EOL> ( project , <EOL> commit_req , transaction_id ) = client . connection . _commit_cw [ <NUM_LIT:0> ] <EOL> self . assertEqual ( project , self . PROJECT ) <EOL> mutated_entity = _mutated_pb ( self , commit_req . mutations , '<STR_LIT>' ) <EOL> self . assertEqual ( mutated_entity . key , key . to_protobuf ( ) ) <EOL> prop_list = list ( _property_tuples ( mutated_entity ) ) <EOL> self . assertTrue ( len ( prop_list ) , <NUM_LIT:1> ) <EOL> name , value_pb = prop_list [ <NUM_LIT:0> ] <EOL> self . assertEqual ( name , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( value_pb . string_value , u'<STR_LIT:bar>' ) <EOL> self . assertTrue ( transaction_id is None ) <EOL> def test_put_multi_existing_batch_w_completed_key ( self ) : <EOL> from gcloud . datastore . helpers import _property_tuples <EOL> from gcloud . datastore . test_batch import _Entity <EOL> from gcloud . datastore . test_batch import _Key <EOL> from gcloud . datastore . test_batch import _mutated_pb <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> entity = _Entity ( foo = u'<STR_LIT:bar>' ) <EOL> key = entity . key = _Key ( self . PROJECT ) <EOL> with _NoCommitBatch ( client ) as CURR_BATCH : <EOL> result = client . put_multi ( [ entity ] ) <EOL> self . assertEqual ( result , None ) <EOL> mutated_entity = _mutated_pb ( self , CURR_BATCH . mutations , '<STR_LIT>' ) <EOL> self . assertEqual ( mutated_entity . key , key . to_protobuf ( ) ) <EOL> prop_list = list ( _property_tuples ( mutated_entity ) ) <EOL> self . assertTrue ( len ( prop_list ) , <NUM_LIT:1> ) <EOL> name , value_pb = prop_list [ <NUM_LIT:0> ] <EOL> self . assertEqual ( name , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( value_pb . string_value , u'<STR_LIT:bar>' ) <EOL> def test_delete ( self ) : <EOL> _called_with = [ ] <EOL> def _delete_multi ( * args , ** kw ) : <EOL> _called_with . append ( ( args , kw ) ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . delete_multi = _delete_multi <EOL> key = object ( ) <EOL> client . delete ( key ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ( ) ) <EOL> self . assertEqual ( _called_with [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , [ key ] ) <EOL> def test_delete_multi_no_keys ( self ) : <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> result = client . delete_multi ( [ ] ) <EOL> self . assertEqual ( result , None ) <EOL> self . assertEqual ( len ( client . connection . _commit_cw ) , <NUM_LIT:0> ) <EOL> def test_delete_multi_no_batch ( self ) : <EOL> from gcloud . datastore . test_batch import _Key <EOL> from gcloud . datastore . test_batch import _mutated_pb <EOL> key = _Key ( self . PROJECT ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> client . connection . _commit . append ( [ ] ) <EOL> result = client . delete_multi ( [ key ] ) <EOL> self . assertEqual ( result , None ) <EOL> self . assertEqual ( len ( client . connection . _commit_cw ) , <NUM_LIT:1> ) <EOL> ( project , <EOL> commit_req , transaction_id ) = client . connection . _commit_cw [ <NUM_LIT:0> ] <EOL> self . assertEqual ( project , self . PROJECT ) <EOL> mutated_key = _mutated_pb ( self , commit_req . mutations , '<STR_LIT>' ) <EOL> self . assertEqual ( mutated_key , key . to_protobuf ( ) ) <EOL> self . assertTrue ( transaction_id is None ) <EOL> def test_delete_multi_w_existing_batch ( self ) : <EOL> from gcloud . datastore . test_batch import _Key <EOL> from gcloud . datastore . test_batch import _mutated_pb <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> key = _Key ( self . PROJECT ) <EOL> with _NoCommitBatch ( client ) as CURR_BATCH : <EOL> result = client . delete_multi ( [ key ] ) <EOL> self . assertEqual ( result , None ) <EOL> mutated_key = _mutated_pb ( self , CURR_BATCH . mutations , '<STR_LIT>' ) <EOL> self . assertEqual ( mutated_key , key . _key ) <EOL> self . assertEqual ( len ( client . connection . _commit_cw ) , <NUM_LIT:0> ) <EOL> def test_delete_multi_w_existing_transaction ( self ) : <EOL> from gcloud . datastore . test_batch import _Key <EOL> from gcloud . datastore . test_batch import _mutated_pb <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> key = _Key ( self . PROJECT ) <EOL> with _NoCommitTransaction ( client ) as CURR_XACT : <EOL> result = client . delete_multi ( [ key ] ) <EOL> self . assertEqual ( result , None ) <EOL> mutated_key = _mutated_pb ( self , CURR_XACT . mutations , '<STR_LIT>' ) <EOL> self . assertEqual ( mutated_key , key . _key ) <EOL> self . assertEqual ( len ( client . connection . _commit_cw ) , <NUM_LIT:0> ) <EOL> def test_allocate_ids_w_partial_key ( self ) : <EOL> from gcloud . datastore . test_batch import _Key <EOL> NUM_IDS = <NUM_LIT:2> <EOL> INCOMPLETE_KEY = _Key ( self . PROJECT ) <EOL> INCOMPLETE_KEY . _id = None <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> result = client . allocate_ids ( INCOMPLETE_KEY , NUM_IDS ) <EOL> self . assertEqual ( [ key . _id for key in result ] , list ( range ( NUM_IDS ) ) ) <EOL> def test_allocate_ids_with_completed_key ( self ) : <EOL> from gcloud . datastore . test_batch import _Key <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> COMPLETE_KEY = _Key ( self . PROJECT ) <EOL> self . assertRaises ( ValueError , client . allocate_ids , COMPLETE_KEY , <NUM_LIT:2> ) <EOL> def test_key_w_project ( self ) : <EOL> KIND = '<STR_LIT>' <EOL> ID = <NUM_LIT> <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> self . assertRaises ( TypeError , <EOL> client . key , KIND , ID , project = self . PROJECT ) <EOL> def test_key_wo_project ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> KIND = '<STR_LIT>' <EOL> ID = <NUM_LIT> <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> with _Monkey ( MUT , Key = _Dummy ) : <EOL> key = client . key ( KIND , ID ) <EOL> self . assertTrue ( isinstance ( key , _Dummy ) ) <EOL> self . assertEqual ( key . args , ( KIND , ID ) ) <EOL> expected_kwargs = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> self . assertEqual ( key . kwargs , expected_kwargs ) <EOL> def test_key_w_namespace ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> KIND = '<STR_LIT>' <EOL> ID = <NUM_LIT> <EOL> NAMESPACE = object ( ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( namespace = NAMESPACE , credentials = creds ) <EOL> with _Monkey ( MUT , Key = _Dummy ) : <EOL> key = client . key ( KIND , ID ) <EOL> self . assertTrue ( isinstance ( key , _Dummy ) ) <EOL> expected_kwargs = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : NAMESPACE , <EOL> } <EOL> self . assertEqual ( key . kwargs , expected_kwargs ) <EOL> def test_key_w_namespace_collision ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> KIND = '<STR_LIT>' <EOL> ID = <NUM_LIT> <EOL> NAMESPACE1 = object ( ) <EOL> NAMESPACE2 = object ( ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( namespace = NAMESPACE1 , credentials = creds ) <EOL> with _Monkey ( MUT , Key = _Dummy ) : <EOL> key = client . key ( KIND , ID , namespace = NAMESPACE2 ) <EOL> self . assertTrue ( isinstance ( key , _Dummy ) ) <EOL> expected_kwargs = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : NAMESPACE2 , <EOL> } <EOL> self . assertEqual ( key . kwargs , expected_kwargs ) <EOL> def test_batch ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> with _Monkey ( MUT , Batch = _Dummy ) : <EOL> batch = client . batch ( ) <EOL> self . assertTrue ( isinstance ( batch , _Dummy ) ) <EOL> self . assertEqual ( batch . args , ( client , ) ) <EOL> self . assertEqual ( batch . kwargs , { } ) <EOL> def test_transaction_defaults ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> with _Monkey ( MUT , Transaction = _Dummy ) : <EOL> xact = client . transaction ( ) <EOL> self . assertTrue ( isinstance ( xact , _Dummy ) ) <EOL> self . assertEqual ( xact . args , ( client , ) ) <EOL> self . assertEqual ( xact . kwargs , { } ) <EOL> def test_query_w_client ( self ) : <EOL> KIND = '<STR_LIT>' <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> other = self . _makeOne ( credentials = object ( ) ) <EOL> self . assertRaises ( TypeError , client . query , kind = KIND , client = other ) <EOL> def test_query_w_project ( self ) : <EOL> KIND = '<STR_LIT>' <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> self . assertRaises ( TypeError , <EOL> client . query , kind = KIND , project = self . PROJECT ) <EOL> def test_query_w_defaults ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> with _Monkey ( MUT , Query = _Dummy ) : <EOL> query = client . query ( ) <EOL> self . assertTrue ( isinstance ( query , _Dummy ) ) <EOL> self . assertEqual ( query . args , ( client , ) ) <EOL> expected_kwargs = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> self . assertEqual ( query . kwargs , expected_kwargs ) <EOL> def test_query_explicit ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> KIND = '<STR_LIT>' <EOL> NAMESPACE = '<STR_LIT>' <EOL> ANCESTOR = object ( ) <EOL> FILTERS = [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> PROJECTION = [ '<STR_LIT>' ] <EOL> ORDER = [ '<STR_LIT>' ] <EOL> DISTINCT_ON = [ '<STR_LIT>' ] <EOL> creds = object ( ) <EOL> client = self . _makeOne ( credentials = creds ) <EOL> with _Monkey ( MUT , Query = _Dummy ) : <EOL> query = client . query ( <EOL> kind = KIND , <EOL> namespace = NAMESPACE , <EOL> ancestor = ANCESTOR , <EOL> filters = FILTERS , <EOL> projection = PROJECTION , <EOL> order = ORDER , <EOL> distinct_on = DISTINCT_ON , <EOL> ) <EOL> self . assertTrue ( isinstance ( query , _Dummy ) ) <EOL> self . assertEqual ( query . args , ( client , ) ) <EOL> kwargs = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : KIND , <EOL> '<STR_LIT>' : NAMESPACE , <EOL> '<STR_LIT>' : ANCESTOR , <EOL> '<STR_LIT>' : FILTERS , <EOL> '<STR_LIT>' : PROJECTION , <EOL> '<STR_LIT>' : ORDER , <EOL> '<STR_LIT>' : DISTINCT_ON , <EOL> } <EOL> self . assertEqual ( query . kwargs , kwargs ) <EOL> def test_query_w_namespace ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> KIND = '<STR_LIT>' <EOL> NAMESPACE = object ( ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( namespace = NAMESPACE , credentials = creds ) <EOL> with _Monkey ( MUT , Query = _Dummy ) : <EOL> query = client . query ( kind = KIND ) <EOL> self . assertTrue ( isinstance ( query , _Dummy ) ) <EOL> self . assertEqual ( query . args , ( client , ) ) <EOL> expected_kwargs = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : NAMESPACE , <EOL> '<STR_LIT>' : KIND , <EOL> } <EOL> self . assertEqual ( query . kwargs , expected_kwargs ) <EOL> def test_query_w_namespace_collision ( self ) : <EOL> from gcloud . datastore import client as MUT <EOL> from gcloud . _testing import _Monkey <EOL> KIND = '<STR_LIT>' <EOL> NAMESPACE1 = object ( ) <EOL> NAMESPACE2 = object ( ) <EOL> creds = object ( ) <EOL> client = self . _makeOne ( namespace = NAMESPACE1 , credentials = creds ) <EOL> with _Monkey ( MUT , Query = _Dummy ) : <EOL> query = client . query ( kind = KIND , namespace = NAMESPACE2 ) <EOL> self . assertTrue ( isinstance ( query , _Dummy ) ) <EOL> self . assertEqual ( query . args , ( client , ) ) <EOL> expected_kwargs = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : NAMESPACE2 , <EOL> '<STR_LIT>' : KIND , <EOL> } <EOL> self . assertEqual ( query . kwargs , expected_kwargs ) <EOL> class _Dummy ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> class _MockConnection ( object ) : <EOL> def __init__ ( self , credentials = None , http = None ) : <EOL> self . credentials = credentials <EOL> self . http = http <EOL> self . _lookup_cw = [ ] <EOL> self . _lookup = [ ] <EOL> self . _commit_cw = [ ] <EOL> self . _commit = [ ] <EOL> self . _alloc_cw = [ ] <EOL> self . _alloc = [ ] <EOL> self . _index_updates = <NUM_LIT:0> <EOL> def _add_lookup_result ( self , results = ( ) , missing = ( ) , deferred = ( ) ) : <EOL> self . _lookup . append ( ( list ( results ) , list ( missing ) , list ( deferred ) ) ) <EOL> def lookup ( self , project , key_pbs , eventual = False , transaction_id = None ) : <EOL> self . _lookup_cw . append ( ( project , key_pbs , eventual , transaction_id ) ) <EOL> triple , self . _lookup = self . _lookup [ <NUM_LIT:0> ] , self . _lookup [ <NUM_LIT:1> : ] <EOL> results , missing , deferred = triple <EOL> return results , missing , deferred <EOL> def commit ( self , project , commit_request , transaction_id ) : <EOL> self . _commit_cw . append ( ( project , commit_request , transaction_id ) ) <EOL> response , self . _commit = self . _commit [ <NUM_LIT:0> ] , self . _commit [ <NUM_LIT:1> : ] <EOL> return self . _index_updates , response <EOL> def allocate_ids ( self , project , key_pbs ) : <EOL> from gcloud . datastore . test_connection import _KeyProto <EOL> self . _alloc_cw . append ( ( project , key_pbs ) ) <EOL> num_pbs = len ( key_pbs ) <EOL> return [ _KeyProto ( i ) for i in list ( range ( num_pbs ) ) ] <EOL> class _NoCommitBatch ( object ) : <EOL> def __init__ ( self , client ) : <EOL> from gcloud . datastore . batch import Batch <EOL> self . _client = client <EOL> self . _batch = Batch ( client ) <EOL> def __enter__ ( self ) : <EOL> self . _client . _push_batch ( self . _batch ) <EOL> return self . _batch <EOL> def __exit__ ( self , * args ) : <EOL> self . _client . _pop_batch ( ) <EOL> class _NoCommitTransaction ( object ) : <EOL> def __init__ ( self , client , transaction_id = '<STR_LIT>' ) : <EOL> from gcloud . datastore . transaction import Transaction <EOL> self . _client = client <EOL> xact = self . _transaction = Transaction ( client ) <EOL> xact . _id = transaction_id <EOL> def __enter__ ( self ) : <EOL> self . _client . _push_batch ( self . _transaction ) <EOL> return self . _transaction <EOL> def __exit__ ( self , * args ) : <EOL> self . _client . _pop_batch ( ) </s>
