def test_dynamic_delete(self):
    entity_proto1 = self.get_new_entity_proto("test", "test_kind", "nancy", "prop1name", 
                                              "prop2val", ns="blah")
    zookeeper = flexmock()
    zookeeper.should_receive("getTransactionID").and_return(1)
    zookeeper.should_receive("getValidTransactionID").and_return(1)
    zookeeper.should_receive("registUpdatedKey").and_return(1)
    zookeeper.should_receive("acquireLock").and_return(True)
    zookeeper.should_receive("releaseLock").and_return(True)
    db_batch = flexmock()
    db_batch.should_receive("batch_delete").and_return(None)
    db_batch.should_receive("batch_put_entity").and_return(None)
    db_batch.should_receive("batch_get_entity").and_return(
               {"test/blah/test_kind:nancy!": 
                 {
                   APP_ENTITY_SCHEMA[0]: entity_proto1.Encode(),
                   APP_ENTITY_SCHEMA[1]: 1
                 }
               })

    dd = DatastoreDistributed(db_batch, zookeeper) 

    entity_key = entity_proto1.key()
    del_req = datastore_pb.DeleteRequest()
    key = del_req.add_key() 
    key.MergeFrom(entity_key)
    del_resp = datastore_pb.DeleteResponse()
    
    dd.dynamic_delete("test", del_req)

    # Now test while in a transaction
    del_resp = datastore_pb.DeleteResponse()
    del_req.mutable_transaction().set_handle(1)
    dd.dynamic_delete("test", del_req)
    def test_dynamic_delete(self):
        del_request = flexmock()
        del_request.should_receive("key_list")
        del_request.should_receive("has_transaction").never()
        del_request.should_receive("transaction").never()
        dd = DatastoreDistributed(None, None)
        dd.dynamic_delete("appid", del_request)

        del_request = flexmock()
        del_request.should_receive("key_list").and_return(['1'])
        del_request.should_receive("has_transaction").and_return(True).twice()
        transaction = flexmock()
        transaction.should_receive("handle").and_return(1)
        del_request.should_receive("transaction").and_return(
            transaction).once()
        dd = DatastoreDistributed(None, None)
        flexmock(dd).should_receive("acquire_locks_for_trans").and_return({})
        flexmock(dd).should_receive("release_locks_for_nontrans").never()
        flexmock(dd).should_receive("delete_entities").once()
        dd.dynamic_delete("appid", del_request)

        del_request = flexmock()
        del_request.should_receive("key_list").and_return(['1'])
        del_request.should_receive("has_transaction").and_return(False).twice()
        dd = DatastoreDistributed(None, None)
        flexmock(dd).should_receive("acquire_locks_for_trans").never()
        flexmock(dd).should_receive(
            "acquire_locks_for_nontrans").once().and_return({})
        flexmock(dd).should_receive("delete_entities").once()
        flexmock(dd).should_receive("release_locks_for_nontrans").once()
        dd.dynamic_delete("appid", del_request)
  def test_dynamic_delete(self):
    del_request = flexmock()
    del_request.should_receive("key_list")
    del_request.should_receive("has_transaction").never()
    del_request.should_receive("transaction").never()
    dd = DatastoreDistributed(None, None)
    dd.dynamic_delete("appid", del_request)

    del_request = flexmock()
    del_request.should_receive("key_list").and_return(['1'])
    del_request.should_receive("has_transaction").and_return(True).twice()
    transaction = flexmock()
    transaction.should_receive("handle").and_return(1)
    del_request.should_receive("transaction").and_return(transaction).once()
    dd = DatastoreDistributed(None, None)
    flexmock(dd).should_receive("acquire_locks_for_trans").and_return({})
    flexmock(dd).should_receive("release_locks_for_nontrans").never()
    flexmock(dd).should_receive("delete_entities").once()
    dd.dynamic_delete("appid", del_request)

    del_request = flexmock()
    del_request.should_receive("key_list").and_return(['1'])
    del_request.should_receive("has_transaction").and_return(False).twice()
    dd = DatastoreDistributed(None, None)
    flexmock(dd).should_receive("acquire_locks_for_trans").never()
    flexmock(dd).should_receive("acquire_locks_for_nontrans").once().and_return({})
    flexmock(dd).should_receive("delete_entities").once()
    flexmock(dd).should_receive("release_locks_for_nontrans").once()
    dd.dynamic_delete("appid", del_request)
    def test_dynamic_delete(self):
        del_request = flexmock()
        del_request.should_receive("key_list")
        del_request.should_receive("has_transaction").never()
        del_request.should_receive("transaction").never()
        dd = DatastoreDistributed(None, None)
        dd.dynamic_delete("appid", del_request)

        fake_element = flexmock()
        fake_element.should_receive("type").and_return("kind")
        fake_path = flexmock()
        fake_path.should_receive("element_list").and_return([fake_element])
        fake_key = flexmock()
        fake_key.should_receive("path").and_return(fake_path)

        del_request = flexmock()
        del_request.should_receive("key_list").and_return([fake_key])
        del_request.should_receive("has_transaction").and_return(True).twice()
        transaction = flexmock()
        transaction.should_receive("handle").and_return(1)
        del_request.should_receive("transaction").and_return(
            transaction).once()
        del_request.should_receive("has_mark_changes").and_return(False)
        dd = DatastoreDistributed(None, None)
        flexmock(dd).should_receive("acquire_locks_for_trans").and_return({})
        flexmock(dd).should_receive("release_locks_for_nontrans").never()
        flexmock(dd).should_receive("delete_entities").once()
        flexmock(dd).should_receive("get_entity_kind").and_return("kind")
        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).twice()
        del_request.should_receive("has_mark_changes").and_return(False)
        dd = DatastoreDistributed(None, None)
        flexmock(dd).should_receive("acquire_locks_for_trans").never()
        flexmock(dd).should_receive(
            "acquire_locks_for_nontrans").once().and_return({})
        flexmock(dd).should_receive("delete_entities").once()
        flexmock(dd).should_receive("release_locks_for_nontrans").once()
        dd.dynamic_delete("appid", del_request)
  def test_dynamic_delete(self):
    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').and_return(True)
    dd = DatastoreDistributed(db_batch, None)
    dd.dynamic_delete("appid", del_request)

    fake_element = flexmock()
    fake_element.should_receive("type").and_return("kind")
    fake_path = flexmock()
    fake_path.should_receive("element_list").and_return([fake_element])
    fake_key = flexmock()
    fake_key.should_receive("path").and_return(fake_path)

    del_request = flexmock()
    del_request.should_receive("key_list").and_return([fake_key])
    del_request.should_receive("has_transaction").and_return(True).twice()
    transaction = flexmock()
    transaction.should_receive("handle").and_return(1)
    del_request.should_receive("transaction").and_return(transaction).once()
    del_request.should_receive("has_mark_changes").and_return(False)
    dd = DatastoreDistributed(db_batch, None)
    flexmock(dd).should_receive("acquire_locks_for_trans").and_return({})
    flexmock(dd).should_receive("release_locks_for_nontrans").never()
    flexmock(dd).should_receive("get_entity_kind").and_return("kind")
    flexmock(dd).should_receive('delete_entities_txn')
    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).twice()
    del_request.should_receive("has_mark_changes").and_return(False)
    dd = DatastoreDistributed(db_batch, None)
    flexmock(dd).should_receive("acquire_locks_for_trans").never()
    flexmock(dd).should_receive("acquire_locks_for_nontrans").once().and_return({})
    flexmock(dd).should_receive("delete_entities").once()
    flexmock(dd).should_receive("release_locks_for_nontrans").once()
    dd.dynamic_delete("appid", del_request)