def test_dynamic_delete(self): async_true = gen.Future() async_true.set_result(True) entity_lock = flexmock(EntityLock) entity_lock.should_receive('acquire').and_return(async_true) entity_lock.should_receive('release') del_request = flexmock() del_request.should_receive("key_list") del_request.should_receive("has_transaction").never() del_request.should_receive("transaction").never() db_batch = flexmock() db_batch.should_receive('valid_data_version_sync').and_return(True) transaction_manager = flexmock( create_transaction_id=lambda project, xg: 1, delete_transaction_id=lambda project, txid: None, set_groups=lambda project_id, txid, groups: None) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) dd.index_manager = flexmock( projects={'guestbook': flexmock(indexes_pb=[])}) yield dd.dynamic_delete("appid", del_request) fake_key = entity_pb.Reference() fake_key.set_app('foo') path = fake_key.mutable_path() element = path.add_element() element.set_type('bar') element.set_id(1) del_request = flexmock() del_request.should_receive("key_list").and_return([fake_key]) del_request.should_receive("has_transaction").and_return(True) transaction = flexmock() transaction.should_receive("handle").and_return(1) del_request.should_receive("transaction").and_return(transaction) del_request.should_receive("has_mark_changes").and_return(False) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) dd.index_manager = flexmock( projects={'appid': flexmock(indexes_pb=[])}) flexmock(utils).should_receive("get_entity_kind").and_return("kind") db_batch.should_receive('delete_entities_tx').and_return(ASYNC_NONE) yield dd.dynamic_delete("appid", del_request) del_request = flexmock() del_request.should_receive("key_list").and_return([fake_key]) del_request.should_receive("has_transaction").and_return(False) del_request.should_receive("has_mark_changes").and_return(False) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) dd.index_manager = flexmock( projects={'appid': flexmock(indexes_pb=[])}) flexmock(dd).should_receive("delete_entities").and_return(ASYNC_NONE).once() yield dd.dynamic_delete("appid", del_request)
def test_put_entities(self): app_id = 'test' db_batch = flexmock() db_batch.should_receive('valid_data_version_sync').and_return(True) entity_proto1 = self.get_new_entity_proto( app_id, "test_kind", "bob", "prop1name", "prop1val", ns="blah") entity_key1 = 'test\x00blah\x00test_kind:bob\x01' entity_proto2 = self.get_new_entity_proto( app_id, "test_kind", "nancy", "prop1name", "prop2val", ns="blah") entity_key2 = 'test\x00blah\x00test_kind:nancy\x01' entity_list = [entity_proto1, entity_proto2] async_result = gen.Future() async_result.set_result({entity_key1: {}, entity_key2: {}}) db_batch.should_receive('batch_get_entity').and_return(async_result) db_batch.should_receive('normal_batch').and_return(ASYNC_NONE) transaction_manager = flexmock( create_transaction_id=lambda project, xg: 1, delete_transaction_id=lambda project, txid: None, set_groups=lambda project, txid, groups: None) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) dd.index_manager = flexmock( projects={app_id: flexmock(indexes_pb=[])}) async_true = gen.Future() async_true.set_result(True) entity_lock = flexmock(EntityLock) entity_lock.should_receive('acquire').and_return(async_true) entity_lock.should_receive('release') yield dd.put_entities(app_id, entity_list)
def test_delete_composite_index_metadata(self): db_batch = flexmock() db_batch.should_receive('valid_data_version_sync').and_return(True) db_batch.should_receive("batch_delete").and_return(ASYNC_NONE) transaction_manager = flexmock() dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) dd = flexmock(dd) dd.index_manager = flexmock( projects={'appid': flexmock(delete_index_definition=lambda id_: None)}) composite_index = entity_pb.CompositeIndex() composite_index.set_id(1) yield dd.delete_composite_index_metadata("appid", composite_index)
def test_apply_txn_changes(self): app = 'guestbook' txn = 1 entity = self.get_new_entity_proto(app, *self.BASIC_ENTITY[1:]) async_metadata = gen.Future() async_metadata.set_result({ 'puts': {entity.key().Encode(): entity.Encode()}, 'deletes': [], 'tasks': [], 'reads': set(), 'start': datetime.datetime.utcnow(), 'is_xg': False, }) db_batch = flexmock() db_batch.should_receive('get_transaction_metadata').\ and_return(async_metadata) db_batch.should_receive('valid_data_version_sync').and_return(True) db_batch.should_receive('group_updates').and_return([]) transaction_manager = flexmock( delete_transaction_id=lambda project_id, txid: None, set_groups=lambda project_id, txid, groups: None) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) dd.index_manager = flexmock( projects={'guestbook': flexmock(indexes_pb=[])}) prefix = dd.get_table_prefix(entity) entity_key = get_entity_key(prefix, entity.key().path()) async_result = gen.Future() async_result.set_result({entity_key: {}}) db_batch.should_receive('batch_get_entity').and_return(async_result) db_batch.should_receive('normal_batch').and_return(ASYNC_NONE) async_true = gen.Future() async_true.set_result(True) entity_lock = flexmock(EntityLock) entity_lock.should_receive('acquire').and_return(async_true) entity_lock.should_receive('release') yield dd.apply_txn_changes(app, txn)
def test_dynamic_put(self): db_batch = flexmock(session=flexmock()) db_batch.should_receive('valid_data_version_sync').and_return(True) entity_proto1 = self.get_new_entity_proto( "test", "test_kind", "bob", "prop1name", "prop1val", ns="blah") entity_key1 = 'test\x00blah\x00test_kind:bob\x01' entity_proto2 = self.get_new_entity_proto( "test", "test_kind", "nancy", "prop1name", "prop2val", ns="blah") entity_key2 = 'test\x00blah\x00test_kind:nancy\x01' async_result = gen.Future() async_result.set_result({entity_key1: {}, entity_key2: {}}) db_batch.should_receive('batch_get_entity').and_return(async_result) db_batch.should_receive('normal_batch').and_return(ASYNC_NONE) transaction_manager = flexmock( create_transaction_id=lambda project, xg: 1, delete_transaction_id=lambda project, txid: None, set_groups=lambda project, txid, groups: None) dd = DatastoreDistributed(db_batch, transaction_manager, self.get_zookeeper()) dd.index_manager = flexmock( projects={'test': flexmock(indexes_pb=[])}) putreq_pb = datastore_pb.PutRequest() putreq_pb.add_entity() putreq_pb.mutable_entity(0).MergeFrom(entity_proto1) putreq_pb.add_entity() putreq_pb.mutable_entity(1).MergeFrom(entity_proto2) putresp_pb = datastore_pb.PutResponse() async_true = gen.Future() async_true.set_result(True) entity_lock = flexmock(EntityLock) entity_lock.should_receive('acquire').and_return(async_true) entity_lock.should_receive('release') flexmock(ScatteredAllocator).should_receive('next').\ and_return(random.randint(1, 500)) yield dd.dynamic_put('test', putreq_pb, putresp_pb) self.assertEquals(len(putresp_pb.key_list()), 2)