diff --git a/google-cloud-datastore/acceptance/datastore_helper.rb b/google-cloud-datastore/acceptance/datastore_helper.rb index 4b507899fe5d..72e4c244088a 100644 --- a/google-cloud-datastore/acceptance/datastore_helper.rb +++ b/google-cloud-datastore/acceptance/datastore_helper.rb @@ -19,7 +19,7 @@ require "google/cloud/datastore" # Create shared dataset object so we don't create new for each test -$dataset = Google::Cloud.new.datastore retries: 10 +$dataset = Google::Cloud.new.datastore module Acceptance ## diff --git a/google-cloud-datastore/lib/google-cloud-datastore.rb b/google-cloud-datastore/lib/google-cloud-datastore.rb index 304f737dea98..52d5fc6b8375 100644 --- a/google-cloud-datastore/lib/google-cloud-datastore.rb +++ b/google-cloud-datastore/lib/google-cloud-datastore.rb @@ -38,9 +38,9 @@ module Cloud # The default scope is: # # * `https://www.googleapis.com/auth/datastore` - # @param [Integer] retries Number of times to retry requests on server - # error. The default value is `3`. Optional. # @param [Integer] timeout Default timeout to use in requests. Optional. + # @param [Hash] client_config A hash of values to override the default + # behavior of the API client. See Google::Gax::CallSettings. Optional. # # @return [Google::Cloud::Datastore::Dataset] # @@ -66,10 +66,10 @@ module Cloud # platform_scope = "https://www.googleapis.com/auth/cloud-platform" # datastore = gcloud.datastore scope: platform_scope # - def datastore scope: nil, retries: nil, timeout: nil + def datastore scope: nil, timeout: nil, client_config: nil Google::Cloud.datastore @project, @keyfile, - scope: scope, retries: (retries || @retries), - timeout: (timeout || @timeout) + scope: scope, timeout: (timeout || @timeout), + client_config: client_config end ## @@ -91,9 +91,9 @@ def datastore scope: nil, retries: nil, timeout: nil # The default scope is: # # * `https://www.googleapis.com/auth/datastore` - # @param [Integer] retries Number of times to retry requests on server - # error. The default value is `3`. Optional. # @param [Integer] timeout Default timeout to use in requests. Optional. + # @param [Hash] client_config A hash of values to override the default + # behavior of the API client. See Google::Gax::CallSettings. Optional. # # @return [Google::Cloud::Datastore::Dataset] # @@ -108,16 +108,16 @@ def datastore scope: nil, retries: nil, timeout: nil # t["done"] = false # t["priority"] = 4 # t["description"] = "Learn Cloud Datastore" - # end + # end `` # # datastore.save task # - def self.datastore project = nil, keyfile = nil, scope: nil, retries: nil, - timeout: nil + def self.datastore project = nil, keyfile = nil, scope: nil, timeout: nil, + client_config: nil require "google/cloud/datastore" Google::Cloud::Datastore.new project: project, keyfile: keyfile, - scope: scope, retries: retries, - timeout: timeout + scope: scope, timeout: timeout, + client_config: client_config end end end diff --git a/google-cloud-datastore/lib/google/cloud/datastore.rb b/google-cloud-datastore/lib/google/cloud/datastore.rb index 203685ecaec9..182cc7bf879a 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore.rb @@ -444,29 +444,16 @@ module Cloud # end # ``` # - # ## Configuring retries and timeout + # ## Configuring timeout # - # You can configure how many times API requests may be automatically - # retried. When an API request fails, the response will be inspected to see - # if the request meets criteria indicating that it may succeed on retry, - # such as `500` and `503` status codes or a specific internal error code - # such as `rateLimitExceeded`. If it meets the criteria, the request will be - # retried after a delay. If another error occurs, the delay will be - # increased before a subsequent attempt, until the `retries` limit is - # reached. - # - # You can also set the request `timeout` value in seconds. + # You can configure the request `timeout` value in seconds. # # ```ruby # require "google/cloud/datastore" # - # datastore = Google::Cloud::Datastore.new retries: 10, timeout: 120 + # datastore = Google::Cloud::Datastore.new timeout: 120 # ``` # - # See the [Datastore error - # codes](https://cloud.google.com/datastore/docs/concepts/errors#error_codes) - # for a list of error conditions. - # # ## The Cloud Datastore Emulator # # As of this release, the Cloud Datastore emulator that is part of the @@ -531,9 +518,9 @@ module Datastore # The default scope is: # # * `https://www.googleapis.com/auth/datastore` - # @param [Integer] retries Number of times to retry requests on server - # error. The default value is `3`. Optional. # @param [Integer] timeout Default timeout to use in requests. Optional. + # @param [Hash] client_config A hash of values to override the default + # behavior of the API client. See Google::Gax::CallSettings. Optional. # # @return [Google::Cloud::Datastore::Dataset] # @@ -554,8 +541,8 @@ module Datastore # # datastore.save task # - def self.new project: nil, keyfile: nil, scope: nil, retries: nil, - timeout: nil + def self.new project: nil, keyfile: nil, scope: nil, timeout: nil, + client_config: nil project ||= Google::Cloud::Datastore::Dataset.default_project project = project.to_s # Always cast to a string fail ArgumentError, "project is missing" if project.empty? @@ -564,20 +551,24 @@ def self.new project: nil, keyfile: nil, scope: nil, retries: nil, return Google::Cloud::Datastore::Dataset.new( Google::Cloud::Datastore::Service.new( project, :this_channel_is_insecure, - host: ENV["DATASTORE_EMULATOR_HOST"], retries: retries)) + host: ENV["DATASTORE_EMULATOR_HOST"], + client_config: client_config)) end + credentials = credentials_with_scope keyfile, scope + Google::Cloud::Datastore::Dataset.new( + Google::Cloud::Datastore::Service.new( + project, credentials, timeout: timeout, + client_config: client_config)) + end + ## + # @private + def self.credentials_with_scope keyfile, scope if keyfile.nil? - credentials = Google::Cloud::Datastore::Credentials.default( - scope: scope) + Google::Cloud::Datastore::Credentials.default(scope: scope) else - credentials = Google::Cloud::Datastore::Credentials.new( - keyfile, scope: scope) + Google::Cloud::Datastore::Credentials.new(keyfile, scope: scope) end - - Google::Cloud::Datastore::Dataset.new( - Google::Cloud::Datastore::Service.new( - project, credentials, retries: retries, timeout: timeout)) end end end diff --git a/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb b/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb index 0668412ea90b..4de7d0c97267 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore/dataset.rb @@ -54,7 +54,7 @@ module Datastore # class Dataset ## - # @private The gRPC Service object. + # @private The Service object. attr_accessor :service ## diff --git a/google-cloud-datastore/lib/google/cloud/datastore/service.rb b/google-cloud-datastore/lib/google/cloud/datastore/service.rb index 2dac07910226..696360fba1bb 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore/service.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore/service.rb @@ -13,46 +13,55 @@ # limitations under the License. +require "google/cloud/errors" require "google/cloud/datastore/credentials" -require "google/datastore/v1/datastore_pb" -require "google/cloud/core/grpc_backoff" +require "google/cloud/datastore/version" +require "google/cloud/datastore/v1" +require "google/gax/errors" module Google module Cloud module Datastore ## - # @private Represents the gRPC Datastore service, including all the API + # @private Represents the GAX Datastore service, including all the API # methods. class Service - attr_accessor :project, :credentials, :host, :retries, :timeout + attr_accessor :project, :credentials, :host, :timeout, :client_config ## # Creates a new Service instance. - def initialize project, credentials, host: nil, retries: nil, - timeout: nil + def initialize project, credentials, host: nil, timeout: nil, + client_config: nil @project = project @credentials = credentials - @host = host || "datastore.googleapis.com" - @retries = retries + @host = host || V1::DatastoreApi::SERVICE_ADDRESS @timeout = timeout + @client_config = client_config || {} end - def creds + def channel + require "grpc" + GRPC::Core::Channel.new host, nil, chan_creds + end + + def chan_creds return credentials if insecure? + require "grpc" GRPC::Core::ChannelCredentials.new.compose \ GRPC::Core::CallCredentials.new credentials.client.updater_proc end - def datastore - return mocked_datastore if mocked_datastore - @datastore ||= begin - require "google/datastore/v1/datastore_services_pb" - - Google::Datastore::V1::Datastore::Stub.new( - host, creds, timeout: timeout) - end + def service + return mocked_service if mocked_service + @service ||= V1::DatastoreApi.new( + service_path: host, + channel: channel, + timeout: timeout, + client_config: client_config, + app_name: "google-cloud-datastore", + app_version: Google::Cloud::Datastore::VERSION) end - attr_accessor :mocked_datastore + attr_accessor :mocked_service def insecure? credentials == :this_channel_is_insecure @@ -62,99 +71,63 @@ def insecure? # Allocate IDs for incomplete keys. # (This is useful for referencing an entity before it is inserted.) def allocate_ids *incomplete_keys - allocate_req = Google::Datastore::V1::AllocateIdsRequest.new( - project_id: project, - keys: incomplete_keys - ) - - execute { datastore.allocate_ids allocate_req } + execute { service.allocate_ids project, incomplete_keys } end ## # Look up entities by keys. def lookup *keys, consistency: nil, transaction: nil - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys - ) - lookup_req.read_options = generate_read_options consistency, - transaction - - execute { datastore.lookup lookup_req } + read_options = generate_read_options consistency, transaction + + execute { service.lookup project, read_options, keys } end # Query for entities. def run_query query, namespace = nil, consistency: nil, transaction: nil - run_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project) - if query.is_a? Google::Datastore::V1::Query - run_req["query"] = query - elsif query.is_a? Google::Datastore::V1::GqlQuery - run_req["gql_query"] = query - else - fail ArgumentError, "Unable to query with a #{query.class} object." + gql_query = nil + if query.is_a? Google::Datastore::V1::GqlQuery + gql_query = query + query = nil end - run_req.read_options = generate_read_options consistency, transaction - - run_req.partition_id = Google::Datastore::V1::PartitionId.new( + read_options = generate_read_options consistency, transaction + partition_id = Google::Datastore::V1::PartitionId.new( namespace_id: namespace) if namespace - execute { datastore.run_query run_req } + execute do + service.run_query project, + partition_id, + read_options, + query: query, + gql_query: gql_query + end end ## # Begin a new transaction. def begin_transaction - tx_req = Google::Datastore::V1::BeginTransactionRequest.new( - project_id: project - ) - - execute { datastore.begin_transaction tx_req } + execute { service.begin_transaction project } end ## # Commit a transaction, optionally creating, deleting or modifying # some entities. def commit mutations, transaction: nil - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: mutations - ) - if transaction - commit_req.mode = :TRANSACTIONAL - commit_req.transaction = transaction + mode = transaction.nil? ? :NON_TRANSACTIONAL : :TRANSACTIONAL + execute do + service.commit project, mode, mutations, transaction: transaction end - - execute { datastore.commit commit_req } end ## # Roll back a transaction. def rollback transaction - rb_req = Google::Datastore::V1::RollbackRequest.new( - project_id: project, - transaction: transaction - ) - - execute { datastore.rollback rb_req } + execute { service.rollback project, transaction } end def inspect "#{self.class}(#{@project})" end - ## - # Performs backoff and error handling - def execute - require "grpc" # Ensure GRPC is loaded before rescuing exception - Google::Cloud::Core::GrpcBackoff.new(retries: retries).execute do - yield - end - rescue GRPC::BadStatus => e - raise Google::Cloud::Error.from_error(e) - end - protected def generate_read_options consistency, transaction @@ -170,6 +143,13 @@ def generate_read_options consistency, transaction end nil end + + def execute + yield + rescue Google::Gax::GaxError => e + # GaxError wraps BadStatus, but exposes it as #cause + raise Google::Cloud::Error.from_error(e.cause) + end end end end diff --git a/google-cloud-datastore/lib/google/cloud/datastore/transaction.rb b/google-cloud-datastore/lib/google/cloud/datastore/transaction.rb index b14a7ffa90bc..e0b3e45e1e3e 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore/transaction.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore/transaction.rb @@ -59,7 +59,7 @@ class Transaction < Dataset ## # @private Creates a new Transaction instance. - # Takes a Connection and Service instead of project and Credentials. + # Takes a Service instead of project and Credentials. def initialize service @service = service reset! diff --git a/google-cloud-datastore/lib/google/cloud/datastore/v1.rb b/google-cloud-datastore/lib/google/cloud/datastore/v1.rb index c9cec948018a..2d46f03fcea3 100644 --- a/google-cloud-datastore/lib/google/cloud/datastore/v1.rb +++ b/google-cloud-datastore/lib/google/cloud/datastore/v1.rb @@ -14,3 +14,5 @@ require "google/cloud/datastore/v1/datastore_api" +# Require the protobufs so we can create objects before GRPC is loaded. +require "google/datastore/v1/datastore_pb" diff --git a/google-cloud-datastore/test/google/cloud/datastore/dataset_test.rb b/google-cloud-datastore/test/google/cloud/datastore/dataset_test.rb index 54afb1176217..00db849e0fee 100644 --- a/google-cloud-datastore/test/google/cloud/datastore/dataset_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore/dataset_test.rb @@ -15,9 +15,11 @@ require "helper" describe Google::Cloud::Datastore::Dataset do - let(:project) { "my-todo-project" } + let(:project) { "my-todo-project" } let(:credentials) { OpenStruct.new } - let(:dataset) { Google::Cloud::Datastore::Dataset.new(Google::Cloud::Datastore::Service.new(project, credentials)) } + let(:dataset) { Google::Cloud::Datastore::Dataset.new(Google::Cloud::Datastore::Service.new(project, credentials)) } + let(:query) { Google::Cloud::Datastore::Query.new.kind("User") } + let(:gql_query_grpc) { Google::Datastore::V1::GqlQuery.new(query_string: "SELECT * FROM Task") } let(:run_query_res) do run_query_res_entities = 2.times.map do |i| Google::Datastore::V1::EntityResult.new( @@ -72,22 +74,19 @@ end before do - dataset.service.mocked_datastore = Minitest::Mock.new + dataset.service.mocked_service = Minitest::Mock.new end after do - dataset.service.mocked_datastore.verify + dataset.service.mocked_service.verify end it "allocate_ids returns complete keys" do - allocate_req = Google::Datastore::V1::AllocateIdsRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test").to_grpc] - ) + keys = [Google::Cloud::Datastore::Key.new("ds-test").to_grpc] allocate_res = Google::Datastore::V1::AllocateIdsResponse.new( keys: [Google::Cloud::Datastore::Key.new("ds-test", 1234).to_grpc] ) - dataset.service.mocked_datastore.expect :allocate_ids, allocate_res, [allocate_req] + dataset.service.mocked_service.expect :allocate_ids, allocate_res, [project, keys] incomplete_key = Google::Cloud::Datastore::Key.new "ds-test" incomplete_key.must_be :incomplete? @@ -113,12 +112,9 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" e["name"] = "thingamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -138,12 +134,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test" e["name"] = "thingamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test" @@ -164,12 +158,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" e["name"] = "thingamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -188,12 +180,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test" e["name"] = "thingamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test" @@ -217,13 +207,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thangie" e["name"] = "thungamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -251,13 +238,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thangie" e["name"] = "thungamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -282,12 +266,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" e["name"] = "thingamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -306,12 +288,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test" e["name"] = "thingamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test" @@ -335,13 +315,11 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thangie" e["name"] = "thungamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] + + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -369,13 +347,11 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thangie" e["name"] = "thungamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] + + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -400,12 +376,9 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" e["name"] = "thingamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -431,13 +404,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thangie" e["name"] = "thungamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -467,13 +437,10 @@ e.key = Google::Cloud::Datastore::Key.new "ds-test", "thangie" e["name"] = "thungamajig" end.to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -491,33 +458,24 @@ end it "find can take a kind and id" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", 123).to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", 123).to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res, [project, nil, keys] entity = dataset.find "ds-test", 123 entity.must_be_kind_of Google::Cloud::Datastore::Entity end it "find can take a kind and name" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res, [project, nil, keys] entity = dataset.find "ds-test", "thingie" entity.must_be_kind_of Google::Cloud::Datastore::Entity end it "find can take a key" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res, [project, nil, keys] key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" entity = dataset.find key @@ -525,23 +483,17 @@ end it "find is aliased to get" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", 123).to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", 123).to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res, [project, nil, keys] entity = dataset.get "ds-test", 123 entity.must_be_kind_of Google::Cloud::Datastore::Entity end it "find can specify consistency" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", 123).to_grpc], - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", 123).to_grpc] + read_options = Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) + dataset.service.mocked_service.expect :lookup, lookup_res, [project, read_options, keys] entity = dataset.find "ds-test", 123, consistency: :eventual entity.must_be_kind_of Google::Cloud::Datastore::Entity @@ -555,12 +507,9 @@ end it "find_all takes several keys" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, - Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, + Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res, [project, nil, keys] key1 = Google::Cloud::Datastore::Key.new "ds-test", "thingie1" key2 = Google::Cloud::Datastore::Key.new "ds-test", "thingie2" @@ -574,12 +523,9 @@ end it "find_all is aliased to lookup" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, - Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, + Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res, [project, nil, keys] key1 = Google::Cloud::Datastore::Key.new "ds-test", "thingie1" key2 = Google::Cloud::Datastore::Key.new "ds-test", "thingie2" @@ -593,13 +539,10 @@ end it "find_all can specify consistency" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, - Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc], - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, + Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] + read_options = Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) + dataset.service.mocked_service.expect :lookup, lookup_res, [project, read_options, keys] key1 = Google::Cloud::Datastore::Key.new "ds-test", "thingie1" key2 = Google::Cloud::Datastore::Key.new "ds-test", "thingie2" @@ -633,7 +576,7 @@ lookup_res.tap do |response| 2.times.map do response.missing << Google::Datastore::V1::EntityResult.new( - entity: Google::Cloud::Datastore::Entity.new.tap do |e| + entity: Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" e["name"] = "thingamajig" end.to_grpc @@ -643,12 +586,9 @@ end it "contains deferred entities" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, - Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res_deferred, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, + Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res_deferred, [project, nil, keys] key1 = Google::Cloud::Datastore::Key.new "ds-test", "thingie1" key2 = Google::Cloud::Datastore::Key.new "ds-test", "thingie2" @@ -665,12 +605,9 @@ end it "contains missing entities" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, - Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] - ) - dataset.service.mocked_datastore.expect :lookup, lookup_res_missing, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, + Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] + dataset.service.mocked_service.expect :lookup, lookup_res_missing, [project, nil, keys] key1 = Google::Cloud::Datastore::Key.new "ds-test", "thingie1" key2 = Google::Cloud::Datastore::Key.new "ds-test", "thingie2" @@ -691,12 +628,9 @@ mutation = Google::Datastore::V1::Mutation.new( delete: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc ) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -710,13 +644,10 @@ delete: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc) mutation2 = Google::Datastore::V1::Mutation.new( delete: Google::Cloud::Datastore::Key.new("ds-test", "thangie").to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -734,13 +665,10 @@ delete: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc) mutation2 = Google::Datastore::V1::Mutation.new( delete: Google::Cloud::Datastore::Key.new("ds-test", "thangie").to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -757,12 +685,9 @@ mutation = Google::Datastore::V1::Mutation.new( delete: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc ) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation] - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + mode = :NON_TRANSACTIONAL + mutations = [mutation] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" dataset.delete key @@ -773,13 +698,10 @@ delete: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc) mutation2 = Google::Datastore::V1::Mutation.new( delete: Google::Cloud::Datastore::Key.new("ds-test", "thangie").to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -797,13 +719,10 @@ delete: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc) mutation2 = Google::Datastore::V1::Mutation.new( delete: Google::Cloud::Datastore::Key.new("ds-test", "thangie").to_grpc) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: [mutation1, mutation2] - ) + mode = :NON_TRANSACTIONAL + mutations = [mutation1, mutation2] - dataset.service.mocked_datastore.expect :commit, multiple_commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, multiple_commit_res, [project, mode, mutations, transaction: nil] entity1 = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -817,20 +736,15 @@ end it "run will fulfill a query" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + dataset.service.mocked_service.expect :run_query, run_query_res, [project, nil, nil, query: query.to_grpc, gql_query: nil] - query = Google::Cloud::Datastore::Query.new.kind("User") entities = dataset.run query entities.count.must_equal 2 entities.each do |entity| entity.must_be_kind_of Google::Cloud::Datastore::Entity end entities.cursor_for(entities.first).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-0") - entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") + entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") entities.each_with_cursor do |entity, cursor| entity.must_be_kind_of Google::Cloud::Datastore::Entity cursor.must_be_kind_of Google::Cloud::Datastore::Cursor @@ -852,19 +766,15 @@ end it "commit will save and delete entities" do + mode = :NON_TRANSACTIONAL mutations = [Google::Datastore::V1::Mutation.new( - upsert: Google::Cloud::Datastore::Entity.new.tap do |e| - e.key = Google::Cloud::Datastore::Key.new "ds-test", "to-be-saved" - e["name"] = "Gonna be saved" - end.to_grpc), Google::Datastore::V1::Mutation.new( - delete: Google::Cloud::Datastore::Key.new("ds-test", "to-be-deleted").to_grpc) + upsert: Google::Cloud::Datastore::Entity.new.tap do |e| + e.key = Google::Cloud::Datastore::Key.new "ds-test", "to-be-saved" + e["name"] = "Gonna be saved" + end.to_grpc), Google::Datastore::V1::Mutation.new( + delete: Google::Cloud::Datastore::Key.new("ds-test", "to-be-deleted").to_grpc) ] - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :NON_TRANSACTIONAL, - mutations: mutations - ) - dataset.service.mocked_datastore.expect :commit, commit_res, [commit_req] + dataset.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: nil] entity_to_be_saved = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "to-be-saved" @@ -884,20 +794,15 @@ end it "run_query will fulfill a query" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + dataset.service.mocked_service.expect :run_query, run_query_res, [project, nil, nil, query: query.to_grpc, gql_query: nil] - query = Google::Cloud::Datastore::Query.new.kind("User") entities = dataset.run_query query entities.count.must_equal 2 entities.each do |entity| entity.must_be_kind_of Google::Cloud::Datastore::Entity end entities.cursor_for(entities.first).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-0") - entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") + entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") entities.each_with_cursor do |entity, cursor| entity.must_be_kind_of Google::Cloud::Datastore::Entity cursor.must_be_kind_of Google::Cloud::Datastore::Cursor @@ -918,23 +823,16 @@ end it "run_query will fulfill a query with a namespace" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - partition_id: Google::Datastore::V1::PartitionId.new( - namespace_id: "foobar" - ), - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + partition_id = Google::Datastore::V1::PartitionId.new(namespace_id: "foobar") + dataset.service.mocked_service.expect :run_query, run_query_res, [project, partition_id, nil, query: query.to_grpc, gql_query: nil] - query = Google::Cloud::Datastore::Query.new.kind("User") entities = dataset.run_query query, namespace: "foobar" entities.count.must_equal 2 entities.each do |entity| entity.must_be_kind_of Google::Cloud::Datastore::Entity end entities.cursor_for(entities.first).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-0") - entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") + entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") entities.each_with_cursor do |entity, cursor| entity.must_be_kind_of Google::Cloud::Datastore::Entity cursor.must_be_kind_of Google::Cloud::Datastore::Cursor @@ -955,12 +853,7 @@ end it "run will fulfill a gql query" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - gql_query: Google::Datastore::V1::GqlQuery.new( - query_string: "SELECT * FROM Task") - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + dataset.service.mocked_service.expect :run_query, run_query_res, [project, nil, nil, query: nil, gql_query: gql_query_grpc] gql = dataset.gql "SELECT * FROM Task" entities = dataset.run gql @@ -969,7 +862,7 @@ entity.must_be_kind_of Google::Cloud::Datastore::Entity end entities.cursor_for(entities.first).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-0") - entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") + entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") entities.each_with_cursor do |entity, cursor| entity.must_be_kind_of Google::Cloud::Datastore::Entity cursor.must_be_kind_of Google::Cloud::Datastore::Cursor @@ -991,15 +884,8 @@ end it "run will fulfill a gql query with a namespace" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - partition_id: Google::Datastore::V1::PartitionId.new( - namespace_id: "foobar" - ), - gql_query: Google::Datastore::V1::GqlQuery.new( - query_string: "SELECT * FROM Task") - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + partition_id = Google::Datastore::V1::PartitionId.new(namespace_id: "foobar") + dataset.service.mocked_service.expect :run_query, run_query_res, [project, partition_id, nil, query: nil, gql_query: gql_query_grpc] gql = dataset.gql "SELECT * FROM Task" entities = dataset.run gql, namespace: "foobar" @@ -1008,7 +894,7 @@ entity.must_be_kind_of Google::Cloud::Datastore::Entity end entities.cursor_for(entities.first).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-0") - entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") + entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") entities.each_with_cursor do |entity, cursor| entity.must_be_kind_of Google::Cloud::Datastore::Entity cursor.must_be_kind_of Google::Cloud::Datastore::Cursor @@ -1030,12 +916,7 @@ end it "run_query will fulfill a gql query" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - gql_query: Google::Datastore::V1::GqlQuery.new( - query_string: "SELECT * FROM Task") - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + dataset.service.mocked_service.expect :run_query, run_query_res, [project, nil, nil, query: nil, gql_query: gql_query_grpc] gql = dataset.gql "SELECT * FROM Task" entities = dataset.run_query gql @@ -1044,7 +925,7 @@ entity.must_be_kind_of Google::Cloud::Datastore::Entity end entities.cursor_for(entities.first).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-0") - entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") + entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") entities.each_with_cursor do |entity, cursor| entity.must_be_kind_of Google::Cloud::Datastore::Entity cursor.must_be_kind_of Google::Cloud::Datastore::Cursor @@ -1066,15 +947,8 @@ end it "run_query will fulfill a gql query with a namespace" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - partition_id: Google::Datastore::V1::PartitionId.new( - namespace_id: "foobar" - ), - gql_query: Google::Datastore::V1::GqlQuery.new( - query_string: "SELECT * FROM Task") - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + partition_id = Google::Datastore::V1::PartitionId.new(namespace_id: "foobar") + dataset.service.mocked_service.expect :run_query, run_query_res, [project, partition_id, nil, query: nil, gql_query: gql_query_grpc] gql = dataset.gql "SELECT * FROM Task" entities = dataset.run_query gql, namespace: "foobar" @@ -1083,7 +957,7 @@ entity.must_be_kind_of Google::Cloud::Datastore::Entity end entities.cursor_for(entities.first).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-0") - entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") + entities.cursor_for(entities.last).must_equal Google::Cloud::Datastore::Cursor.from_grpc("result-cursor-1") entities.each_with_cursor do |entity, cursor| entity.must_be_kind_of Google::Cloud::Datastore::Entity cursor.must_be_kind_of Google::Cloud::Datastore::Cursor @@ -1248,7 +1122,7 @@ it "transaction will return a Transaction" do tx_id = "giterdone".encode("ASCII-8BIT") begin_tx_res = Google::Datastore::V1::BeginTransactionResponse.new(transaction: tx_id) - dataset.service.mocked_datastore.expect :begin_transaction, begin_tx_res, [Google::Datastore::V1::BeginTransactionRequest] + dataset.service.mocked_service.expect :begin_transaction, begin_tx_res, [project] tx = dataset.transaction tx.must_be_kind_of Google::Cloud::Datastore::Transaction @@ -1260,11 +1134,16 @@ begin_tx_res = Google::Datastore::V1::BeginTransactionResponse.new(transaction: tx_id) commit_res = Google::Datastore::V1::CommitResponse.new( mutation_results: [Google::Datastore::V1::MutationResult.new( - key: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc - )] + key: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc + )] ) - dataset.service.mocked_datastore.expect :begin_transaction, begin_tx_res, [Google::Datastore::V1::BeginTransactionRequest] - dataset.service.mocked_datastore.expect :commit, commit_res, [Google::Datastore::V1::CommitRequest] + mutation = Google::Datastore::V1::Mutation.new( + upsert: Google::Cloud::Datastore::Entity.new.tap do |e| + e.key = Google::Cloud::Datastore::Key.new "ds-test" + e["name"] = "thingamajig" + end.to_grpc) + dataset.service.mocked_service.expect :begin_transaction, begin_tx_res, [project] + dataset.service.mocked_service.expect :commit, commit_res, [project, :TRANSACTIONAL, [mutation], transaction: tx_id] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test" @@ -1279,8 +1158,8 @@ tx_id = "giterdone".encode("ASCII-8BIT") begin_tx_res = Google::Datastore::V1::BeginTransactionResponse.new(transaction: tx_id) rollback_res = Google::Datastore::V1::RollbackResponse.new - dataset.service.mocked_datastore.expect :begin_transaction, begin_tx_res, [Google::Datastore::V1::BeginTransactionRequest] - dataset.service.mocked_datastore.expect :rollback, rollback_res, [Google::Datastore::V1::RollbackRequest] + dataset.service.mocked_service.expect :begin_transaction, begin_tx_res, [project] + dataset.service.mocked_service.expect :rollback, rollback_res, [project, tx_id] error = assert_raises Google::Cloud::Datastore::TransactionError do dataset.transaction do |tx| diff --git a/google-cloud-datastore/test/google/cloud/datastore/lookup_results/find_all_test.rb b/google-cloud-datastore/test/google/cloud/datastore/lookup_results/find_all_test.rb index 312c70873d82..0372b9e407ab 100644 --- a/google-cloud-datastore/test/google/cloud/datastore/lookup_results/find_all_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore/lookup_results/find_all_test.rb @@ -27,6 +27,8 @@ let(:key5) { Google::Cloud::Datastore::Key.new "ds-test", "thingie5" } let(:key6) { Google::Cloud::Datastore::Key.new "ds-test", "thingie6" } let(:keys) { [key1, key2, key3, key4, key5, key6] } + let(:first_keys) { keys.map(&:to_grpc) } + let(:second_keys) { [key3, key4].map(&:to_grpc) } let(:first_lookup_res) do Google::Datastore::V1::LookupResponse.new( @@ -65,24 +67,16 @@ end before do - dataset.service.mocked_datastore = Minitest::Mock.new + dataset.service.mocked_service = Minitest::Mock.new end after do - dataset.service.mocked_datastore.verify + dataset.service.mocked_service.verify end it "paginates" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, nil, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, nil, second_keys] first_entities = dataset.find_all keys first_entities.count.must_equal 2 @@ -108,18 +102,9 @@ end it "paginates with consistency" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + read_options = Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, read_options, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, read_options, second_keys] first_entities = dataset.find_all keys, consistency: :eventual first_entities.count.must_equal 2 @@ -152,21 +137,11 @@ # key: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc # )] ) - dataset.service.mocked_datastore.expect :begin_transaction, begin_tx_res, [Google::Datastore::V1::BeginTransactionRequest] - dataset.service.mocked_datastore.expect :commit, commit_res, [Google::Datastore::V1::CommitRequest] - - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :begin_transaction, begin_tx_res, [project] + dataset.service.mocked_service.expect :commit, commit_res, [project, :TRANSACTIONAL, [], transaction: tx_id] + read_options = Google::Datastore::V1::ReadOptions.new(transaction: tx_id) + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, read_options, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, read_options, second_keys] dataset.transaction do |tx| first_entities = tx.find_all keys @@ -194,16 +169,8 @@ end it "paginates with next? and next" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, nil, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, nil, second_keys] first_entities = dataset.find_all keys first_entities.next?.must_equal true @@ -231,18 +198,9 @@ end it "paginates with next? and next and consistency" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + read_options = Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, read_options, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, read_options, second_keys] first_entities = dataset.find_all keys, consistency: :eventual first_entities.next?.must_equal true @@ -277,21 +235,11 @@ # key: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc # )] ) - dataset.service.mocked_datastore.expect :begin_transaction, begin_tx_res, [Google::Datastore::V1::BeginTransactionRequest] - dataset.service.mocked_datastore.expect :commit, commit_res, [Google::Datastore::V1::CommitRequest] - - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :begin_transaction, begin_tx_res, [project] + dataset.service.mocked_service.expect :commit, commit_res, [project, :TRANSACTIONAL, [], transaction: tx_id] + read_options = Google::Datastore::V1::ReadOptions.new(transaction: tx_id) + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, read_options, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, read_options, second_keys] dataset.transaction do |tx| first_entities = tx.find_all keys @@ -321,16 +269,8 @@ end it "paginates with all" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, nil, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, nil, second_keys] entities = dataset.find_all(keys).all.to_a entities.count.must_equal 4 @@ -340,18 +280,9 @@ end it "paginates with all and consistency" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + read_options = Google::Datastore::V1::ReadOptions.new(read_consistency: :EVENTUAL) + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, read_options, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, read_options, second_keys] entities = dataset.find_all(keys, consistency: :eventual).all.to_a entities.count.must_equal 4 @@ -368,21 +299,11 @@ # key: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc # )] ) - dataset.service.mocked_datastore.expect :begin_transaction, begin_tx_res, [Google::Datastore::V1::BeginTransactionRequest] - dataset.service.mocked_datastore.expect :commit, commit_res, [Google::Datastore::V1::CommitRequest] - - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc), - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :begin_transaction, begin_tx_res, [project] + dataset.service.mocked_service.expect :commit, commit_res, [project, :TRANSACTIONAL, [], transaction: tx_id] + read_options = Google::Datastore::V1::ReadOptions.new(transaction: tx_id) + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, read_options, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, read_options, second_keys] dataset.transaction do |tx| entities = tx.find_all(keys).all.to_a @@ -394,16 +315,8 @@ end it "iterates with all using Enumerator" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, nil, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, nil, second_keys] entities = dataset.find_all(keys).all.take(3) entities.count.must_equal 3 @@ -413,16 +326,8 @@ end it "iterates with all and request_limit set" do - first_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: keys.map(&:to_grpc) - ) - second_lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [key3, key4].map(&:to_grpc) - ) - dataset.service.mocked_datastore.expect :lookup, first_lookup_res, [first_lookup_req] - dataset.service.mocked_datastore.expect :lookup, second_lookup_res, [second_lookup_req] + dataset.service.mocked_service.expect :lookup, first_lookup_res, [project, nil, first_keys] + dataset.service.mocked_service.expect :lookup, second_lookup_res, [project, nil, second_keys] # This test is a bit handwavy, as there aren't more results to lookup. # But if you reduce the limit it will not make additional call. diff --git a/google-cloud-datastore/test/google/cloud/datastore/query_results/all_test.rb b/google-cloud-datastore/test/google/cloud/datastore/query_results/all_test.rb index f69743b35d42..afa5c9899abb 100644 --- a/google-cloud-datastore/test/google/cloud/datastore/query_results/all_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore/query_results/all_test.rb @@ -18,12 +18,7 @@ let(:project) { "my-todo-project" } let(:credentials) { OpenStruct.new } let(:dataset) { Google::Cloud::Datastore::Dataset.new(Google::Cloud::Datastore::Service.new(project, credentials)) } - let(:first_run_query_req) do - Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("Task").to_grpc - ) - end + let(:first_run_query) { Google::Cloud::Datastore::Query.new.kind("Task").to_grpc } let(:first_run_query_res) do run_query_res_entities = 25.times.map do |i| Google::Datastore::V1::EntityResult.new( @@ -42,13 +37,10 @@ ) ) end - let(:next_run_query_req) do - Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("Task").start( - Google::Cloud::Datastore::Cursor.from_grpc("second-page-cursor") - ).to_grpc - ) + let(:next_run_query) do + Google::Cloud::Datastore::Query.new.kind("Task").start( + Google::Cloud::Datastore::Cursor.from_grpc("second-page-cursor") + ).to_grpc end let(:next_run_query_res) do run_query_res_entities = 25.times.map do |i| @@ -69,13 +61,13 @@ end before do - dataset.service.mocked_datastore = Minitest::Mock.new - dataset.service.mocked_datastore.expect :run_query, first_run_query_res, [first_run_query_req] - dataset.service.mocked_datastore.expect :run_query, next_run_query_res, [next_run_query_req] + dataset.service.mocked_service = Minitest::Mock.new + dataset.service.mocked_service.expect :run_query, first_run_query_res, [project, nil, nil, query: first_run_query, gql_query: nil] + dataset.service.mocked_service.expect :run_query, next_run_query_res, [project, nil, nil, query: next_run_query, gql_query: nil] end after do - dataset.service.mocked_datastore.verify + dataset.service.mocked_service.verify end it "run will fulfill a query and return an object that can paginate" do diff --git a/google-cloud-datastore/test/google/cloud/datastore/query_results/all_with_more_test.rb b/google-cloud-datastore/test/google/cloud/datastore/query_results/all_with_more_test.rb index 16fd1ac8b57e..b5be6f416510 100644 --- a/google-cloud-datastore/test/google/cloud/datastore/query_results/all_with_more_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore/query_results/all_with_more_test.rb @@ -18,12 +18,7 @@ let(:project) { "my-todo-project" } let(:credentials) { OpenStruct.new } let(:dataset) { Google::Cloud::Datastore::Dataset.new(Google::Cloud::Datastore::Service.new(project, credentials)) } - let(:first_run_query_req) do - Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("Task").to_grpc - ) - end + let(:first_run_query) { Google::Cloud::Datastore::Query.new.kind("Task").to_grpc } let(:first_run_query_res) do run_query_res_entities = 25.times.map do |i| Google::Datastore::V1::EntityResult.new( @@ -42,14 +37,11 @@ ) ) end - let(:next_run_query_req) do - Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("Task").start( + let(:next_run_query) do + Google::Cloud::Datastore::Query.new.kind("Task").start( Google::Cloud::Datastore::Cursor.from_grpc("second-page-cursor") ).to_grpc - ) - end + end let(:next_run_query_res) do run_query_res_entities = 25.times.map do |i| Google::Datastore::V1::EntityResult.new( @@ -70,13 +62,13 @@ end before do - dataset.service.mocked_datastore = Minitest::Mock.new - dataset.service.mocked_datastore.expect :run_query, first_run_query_res, [first_run_query_req] - dataset.service.mocked_datastore.expect :run_query, next_run_query_res, [next_run_query_req] + dataset.service.mocked_service = Minitest::Mock.new + dataset.service.mocked_service.expect :run_query, first_run_query_res, [project, nil, nil, query: first_run_query, gql_query: nil] + dataset.service.mocked_service.expect :run_query, next_run_query_res, [project, nil, nil, query: next_run_query, gql_query: nil] end after do - dataset.service.mocked_datastore.verify + dataset.service.mocked_service.verify end it "run will fulfill a query and can use the all and limit api calls" do diff --git a/google-cloud-datastore/test/google/cloud/datastore/query_results_test.rb b/google-cloud-datastore/test/google/cloud/datastore/query_results_test.rb index c78929fdadfb..414253b7c6cd 100644 --- a/google-cloud-datastore/test/google/cloud/datastore/query_results_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore/query_results_test.rb @@ -18,6 +18,7 @@ let(:project) { "my-todo-project" } let(:credentials) { OpenStruct.new } let(:dataset) { Google::Cloud::Datastore::Dataset.new(Google::Cloud::Datastore::Service.new(project, credentials)) } + let(:query) { Google::Cloud::Datastore::Query.new.kind("User") } let(:run_query_res) do run_query_res_entities = 2.times.map do |i| Google::Datastore::V1::EntityResult.new( @@ -58,21 +59,17 @@ end before do - dataset.service.mocked_datastore = Minitest::Mock.new + dataset.service.mocked_service = Minitest::Mock.new end after do - dataset.service.mocked_datastore.verify + dataset.service.mocked_service.verify end it "has more_results not_finished" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res_not_finished, [run_query_req] - query = Google::Cloud::Datastore::Query.new.kind("User") + dataset.service.mocked_service.expect :run_query, run_query_res_not_finished, [project, nil, nil, query: query.to_grpc, gql_query: nil] + entities = dataset.run query entities.count.must_equal 2 entities.each do |entity| @@ -100,13 +97,8 @@ end it "has more_results more_after_limit" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res_more_after_limit, [run_query_req] + dataset.service.mocked_service.expect :run_query, run_query_res_more_after_limit, [project, nil, nil, query: query.to_grpc, gql_query: nil] - query = Google::Cloud::Datastore::Query.new.kind("User") entities = dataset.run query entities.count.must_equal 2 entities.each do |entity| @@ -134,13 +126,8 @@ end it "has more_results more_after_cursor" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res_more_after_cursor, [run_query_req] + dataset.service.mocked_service.expect :run_query, run_query_res_more_after_cursor, [project, nil, nil, query: query.to_grpc, gql_query: nil] - query = Google::Cloud::Datastore::Query.new.kind("User") entities = dataset.run query entities.count.must_equal 2 entities.each do |entity| @@ -168,13 +155,8 @@ end it "has more_results no_more" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc - ) - dataset.service.mocked_datastore.expect :run_query, run_query_res_no_more, [run_query_req] + dataset.service.mocked_service.expect :run_query, run_query_res_no_more, [project, nil, nil, query: query.to_grpc, gql_query: nil] - query = Google::Cloud::Datastore::Query.new.kind("User") entities = dataset.run query entities.count.must_equal 2 entities.each do |entity| diff --git a/google-cloud-datastore/test/google/cloud/datastore/query_test.rb b/google-cloud-datastore/test/google/cloud/datastore/query_test.rb index f07db3bb31c1..b5f041872345 100644 --- a/google-cloud-datastore/test/google/cloud/datastore/query_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore/query_test.rb @@ -15,8 +15,8 @@ require "helper" describe Google::Cloud::Datastore::Query do + let(:query) { Google::Cloud::Datastore::Query.new } it "can query on kind" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" grpc = query.to_grpc @@ -32,7 +32,6 @@ end it "can filter properties" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" query.where "completed", "=", true @@ -71,7 +70,6 @@ end it "can order results" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" query.order "due" @@ -90,7 +88,6 @@ end it "accepts any string that starts with 'd' for DESCENDING" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" query.order "completed", "DOWN" @@ -101,7 +98,6 @@ end it "can limit and offset" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" grpc = query.to_grpc @@ -124,7 +120,6 @@ raw_cursor = "\x13\xE0\x01\x00\xEB".force_encoding Encoding::ASCII_8BIT encoded_cursor = "E+ABAOs=" - query = Google::Cloud::Datastore::Query.new query.kind "Task" grpc = query.to_grpc @@ -137,7 +132,6 @@ end it "can select the properties to return" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" grpc = query.to_grpc @@ -152,7 +146,6 @@ end it "can select the properties using projection alias" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" grpc = query.to_grpc @@ -168,7 +161,6 @@ end it "can group on properties" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" grpc = query.to_grpc @@ -183,7 +175,6 @@ end it "can group on properties using distinct_on" do - query = Google::Cloud::Datastore::Query.new query.kind "Task" grpc = query.to_grpc @@ -199,7 +190,6 @@ it "can query ancestor" do ancestor_key = Google::Cloud::Datastore::Key.new("User", "username") - query = Google::Cloud::Datastore::Query.new query.kind "Task" query.ancestor ancestor_key @@ -218,7 +208,6 @@ it "can manually filter on ancestor" do ancestor_key = Google::Cloud::Datastore::Key.new("User", "username") - query = Google::Cloud::Datastore::Query.new query.kind "Task" query.filter "__key__", "~", ancestor_key @@ -236,7 +225,6 @@ end it "can chain query methods" do - query = Google::Cloud::Datastore::Query.new q2 = query.kind("Task").select("due", "completed"). where("completed", "=", true).group_by("completed"). order("due", :desc).limit(10).offset(20) diff --git a/google-cloud-datastore/test/google/cloud/datastore/transaction_test.rb b/google-cloud-datastore/test/google/cloud/datastore/transaction_test.rb index 0d2937426fa5..9127b8c1403e 100644 --- a/google-cloud-datastore/test/google/cloud/datastore/transaction_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore/transaction_test.rb @@ -20,8 +20,8 @@ let(:dataset) { Google::Cloud::Datastore::Dataset.new(Google::Cloud::Datastore::Service.new(project, credentials)) } let(:service) do s = dataset.service - s.mocked_datastore = Minitest::Mock.new - s.mocked_datastore.expect :begin_transaction, begin_tx_res, [Google::Datastore::V1::BeginTransactionRequest] + s.mocked_service = Minitest::Mock.new + s.mocked_service.expect :begin_transaction, begin_tx_res, [project] s end let(:transaction) { Google::Cloud::Datastore::Transaction.new service } @@ -65,7 +65,7 @@ let(:tx_id) { "giterdone".encode("ASCII-8BIT") } after do - transaction.service.mocked_datastore.verify + transaction.service.mocked_service.verify end it "save does not persist entities" do @@ -288,26 +288,27 @@ Google::Datastore::V1::MutationResult.new, Google::Datastore::V1::MutationResult.new] ) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :TRANSACTIONAL, - transaction: tx_id, - mutations: [Google::Datastore::V1::Mutation.new( + mode = :TRANSACTIONAL + mutations = [ + Google::Datastore::V1::Mutation.new( upsert: Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "to-be-saved" e["name"] = "Gonna be saved" - end.to_grpc), Google::Datastore::V1::Mutation.new( + end.to_grpc), + Google::Datastore::V1::Mutation.new( insert: Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "to-be-inserted" e["name"] = "Gonna be inserted" - end.to_grpc), Google::Datastore::V1::Mutation.new( + end.to_grpc), + Google::Datastore::V1::Mutation.new( update: Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "to-be-updated" e["name"] = "Gonna be updated" - end.to_grpc), Google::Datastore::V1::Mutation.new( - delete: Google::Cloud::Datastore::Key.new("ds-test", "to-be-deleted").to_grpc)] - ) - transaction.service.mocked_datastore.expect :commit, commit_res, [commit_req] + end.to_grpc), + Google::Datastore::V1::Mutation.new( + delete: Google::Cloud::Datastore::Key.new("ds-test", "to-be-deleted").to_grpc) + ] + transaction.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: tx_id] entity_to_be_saved = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "to-be-saved" @@ -337,12 +338,9 @@ end it "find can take a key" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc], - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - transaction.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc] + read_options = Google::Datastore::V1::ReadOptions.new(transaction: tx_id) + transaction.service.mocked_service.expect :lookup, lookup_res, [project, read_options, keys] key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" entity = transaction.find key @@ -350,13 +348,10 @@ end it "find_all takes several keys" do - lookup_req = Google::Datastore::V1::LookupRequest.new( - project_id: project, - keys: [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, - Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc], - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - transaction.service.mocked_datastore.expect :lookup, lookup_res, [lookup_req] + keys = [Google::Cloud::Datastore::Key.new("ds-test", "thingie1").to_grpc, + Google::Cloud::Datastore::Key.new("ds-test", "thingie2").to_grpc] + read_options = Google::Datastore::V1::ReadOptions.new(transaction: tx_id) + transaction.service.mocked_service.expect :lookup, lookup_res, [project, read_options, keys] key1 = Google::Cloud::Datastore::Key.new "ds-test", "thingie1" key2 = Google::Cloud::Datastore::Key.new "ds-test", "thingie2" @@ -370,12 +365,9 @@ end it "run will fulfill a query" do - run_query_req = Google::Datastore::V1::RunQueryRequest.new( - project_id: project, - query: Google::Cloud::Datastore::Query.new.kind("User").to_grpc, - read_options: Google::Datastore::V1::ReadOptions.new(transaction: tx_id) - ) - transaction.service.mocked_datastore.expect :run_query, run_query_res, [run_query_req] + query_grpc = Google::Cloud::Datastore::Query.new.kind("User").to_grpc + read_options = Google::Datastore::V1::ReadOptions.new(transaction: tx_id) + transaction.service.mocked_service.expect :run_query, run_query_res, [project, nil, read_options, query: query_grpc, gql_query: nil] query = Google::Cloud::Datastore::Query.new.kind("User") entities = transaction.run query @@ -396,17 +388,15 @@ commit_res = Google::Datastore::V1::CommitResponse.new( mutation_results: [Google::Datastore::V1::MutationResult.new] ) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :TRANSACTIONAL, - transaction: tx_id, - mutations: [Google::Datastore::V1::Mutation.new( + mode = :TRANSACTIONAL + mutations = [ + Google::Datastore::V1::Mutation.new( upsert: Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" e["name"] = "thingamajig" - end.to_grpc)] - ) - transaction.service.mocked_datastore.expect :commit, commit_res, [commit_req] + end.to_grpc) + ] + transaction.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: tx_id] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" @@ -427,17 +417,15 @@ key: Google::Cloud::Datastore::Key.new("ds-test", "thingie").to_grpc )] ) - commit_req = Google::Datastore::V1::CommitRequest.new( - project_id: project, - mode: :TRANSACTIONAL, - transaction: tx_id, - mutations: [Google::Datastore::V1::Mutation.new( + mode = :TRANSACTIONAL + mutations = [ + Google::Datastore::V1::Mutation.new( upsert: Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test" e["name"] = "thingamajig" - end.to_grpc)] - ) - transaction.service.mocked_datastore.expect :commit, commit_res, [commit_req] + end.to_grpc) + ] + transaction.service.mocked_service.expect :commit, commit_res, [project, mode, mutations, transaction: tx_id] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test" @@ -453,11 +441,7 @@ it "rollback does not persist entities" do rollback_res = Google::Datastore::V1::RollbackResponse.new - rollback_req = Google::Datastore::V1::RollbackRequest.new( - project_id: project, - transaction: tx_id - ) - transaction.service.mocked_datastore.expect :rollback, rollback_res, [rollback_req] + transaction.service.mocked_service.expect :rollback, rollback_res, [project, tx_id] entity = Google::Cloud::Datastore::Entity.new.tap do |e| e.key = Google::Cloud::Datastore::Key.new "ds-test", "thingie" diff --git a/google-cloud-datastore/test/google/cloud/datastore_test.rb b/google-cloud-datastore/test/google/cloud/datastore_test.rb index 8eab798a5797..1762e6f28f86 100644 --- a/google-cloud-datastore/test/google/cloud/datastore_test.rb +++ b/google-cloud-datastore/test/google/cloud/datastore_test.rb @@ -19,12 +19,12 @@ describe "#datastore" do it "calls out to Google::Cloud.datastore" do gcloud = Google::Cloud.new - stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + stubbed_datastore = ->(project, keyfile, scope: nil, timeout: nil, client_config: nil) { project.must_equal nil keyfile.must_equal nil scope.must_be :nil? - retries.must_be :nil? timeout.must_be :nil? + client_config.must_be :nil? "datastore-dataset-object-empty" } Google::Cloud.stub :datastore, stubbed_datastore do @@ -35,12 +35,12 @@ it "passes project and keyfile to Google::Cloud.datastore" do gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + stubbed_datastore = ->(project, keyfile, scope: nil, timeout: nil, client_config: nil) { project.must_equal "project-id" keyfile.must_equal "keyfile-path" scope.must_be :nil? - retries.must_be :nil? timeout.must_be :nil? + client_config.must_be :nil? "datastore-dataset-object" } Google::Cloud.stub :datastore, stubbed_datastore do @@ -51,16 +51,16 @@ it "passes project and keyfile and options to Google::Cloud.datastore" do gcloud = Google::Cloud.new "project-id", "keyfile-path" - stubbed_datastore = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) { + stubbed_datastore = ->(project, keyfile, scope: nil, timeout: nil, client_config: nil) { project.must_equal "project-id" keyfile.must_equal "keyfile-path" scope.must_equal "http://example.com/scope" - retries.must_equal 5 timeout.must_equal 60 + client_config.must_equal({ "gax" => "options" }) "datastore-dataset-object-scoped" } Google::Cloud.stub :datastore, stubbed_datastore do - dataset = gcloud.datastore scope: "http://example.com/scope", retries: 5, timeout: 60 + dataset = gcloud.datastore scope: "http://example.com/scope", timeout: 60, client_config: { "gax" => "options" } dataset.must_equal "datastore-dataset-object-scoped" end end @@ -91,11 +91,11 @@ scope.must_equal nil "datastore-credentials" } - stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + stubbed_service = ->(project, credentials, timeout: nil, client_config: nil) { project.must_equal "project-id" credentials.must_equal "datastore-credentials" - retries.must_equal nil timeout.must_equal nil + client_config.must_equal nil OpenStruct.new project: project } @@ -142,11 +142,11 @@ scope.must_equal nil "datastore-credentials" } - stubbed_service = ->(project, credentials, retries: nil, timeout: nil) { + stubbed_service = ->(project, credentials, timeout: nil, client_config: nil) { project.must_equal "project-id" credentials.must_equal "datastore-credentials" - retries.must_equal nil timeout.must_equal nil + client_config.must_equal nil OpenStruct.new project: project }