Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions google-cloud-firestore/lib/google/cloud/firestore/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_documents document_paths, mask: nil, transaction: nil, read_time: nil
if read_time
batch_get_req[:read_time] = read_time_to_timestamp(read_time)
end
firestore.batch_get_documents batch_get_req, call_options(parent: database_path)
firestore.batch_get_documents batch_get_req, call_options(parent: database_path, transaction: transaction)
end

##
Expand All @@ -85,13 +85,16 @@ def get_documents document_paths, mask: nil, transaction: nil, read_time: nil
# method.
def list_documents parent, collection_id, token: nil, max: nil, read_time: nil
mask = { field_paths: [] }
paged_enum = firestore.list_documents parent: parent,
collection_id: collection_id,
page_size: max,
page_token: token,
mask: mask,
show_missing: true,
read_time: read_time_to_timestamp(read_time)
list_docs_req = {
parent: parent,
collection_id: collection_id,
page_size: max,
page_token: token,
mask: mask,
show_missing: true,
read_time: read_time_to_timestamp(read_time)
}
paged_enum = firestore.list_documents list_docs_req, call_options(parent: database_path)
paged_enum.response
end

Expand All @@ -118,7 +121,7 @@ def partition_query parent, query_grpc, partition_count, token: nil, max: nil, r
page_size: max,
read_time: read_time_to_timestamp(read_time)
)
paged_enum = firestore.partition_query request
paged_enum = firestore.partition_query request, call_options(parent: database_path)
paged_enum.response
end

Expand All @@ -142,7 +145,7 @@ def run_query path, query_grpc, transaction: nil, read_time: nil, explain_option
run_query_req[:explain_options] = explain_options
end

firestore.run_query run_query_req, call_options(parent: database_path)
firestore.run_query run_query_req, call_options(parent: database_path, transaction: transaction)
end

##
Expand All @@ -160,7 +163,7 @@ def run_aggregate_query parent, structured_aggregation_query, transaction: nil,
if explain_options
request.explain_options = explain_options
end
firestore.run_aggregation_query request
firestore.run_aggregation_query request, call_options(parent: database_path, transaction: transaction)
end

def listen enum
Expand All @@ -173,7 +176,7 @@ def begin_transaction transaction_opt
database: database_path,
options: transaction_opt
},
call_options(parent: database_path)
call_options(parent: database_path, transaction: transaction_opt)
)
end

Expand All @@ -184,7 +187,7 @@ def commit writes, transaction: nil
}
commit_req[:transaction] = transaction if transaction

firestore.commit commit_req, call_options(parent: database_path)
firestore.commit commit_req, call_options(parent: database_path, transaction: transaction)
end

def rollback transaction
Expand All @@ -193,7 +196,7 @@ def rollback transaction
database: database_path,
transaction: transaction
},
call_options(parent: database_path)
call_options(parent: database_path, transaction: transaction)
)
end

Expand Down Expand Up @@ -236,15 +239,18 @@ def read_time_to_timestamp read_time

protected

def default_headers parent = nil
def default_headers parent = nil, transaction: nil
parent ||= database_path
routing_val = CGI.escapeURIComponent parent
{ "x-goog-request-params" => "database=#{routing_val}" }
{
"x-goog-request-params" => "database=#{routing_val}",
"x-goog-is-transaction" => (!transaction.nil?).to_s
}
end

def call_options parent: nil, token: nil
def call_options parent: nil, token: nil, transaction: nil
Gapic::CallOptions.new(**{
metadata: default_headers(parent),
metadata: default_headers(parent, transaction: transaction),
page_token: token
}.compact)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
explain_options: expected_explain_options
)

firestore_mock.expect :run_aggregation_query, response_with_planning_metrics_only, [request]
firestore_mock.expect :run_aggregation_query, response_with_planning_metrics_only, [request, default_options]

explain_result = aggregate_query.explain
_(explain_result).must_be_kind_of Google::Cloud::Firestore::AggregateQueryExplainResult
Expand All @@ -82,7 +82,7 @@
explain_options: expected_explain_options
)

firestore_mock.expect :run_aggregation_query, response_with_metrics_and_result, [request]
firestore_mock.expect :run_aggregation_query, response_with_metrics_and_result, [request, default_options]

explain_result = aggregate_query.explain analyze: true
_(explain_result).must_be_kind_of Google::Cloud::Firestore::AggregateQueryExplainResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
)
)
].to_enum
firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params]
firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params, default_options]

aggregate_query = query.aggregate_query.add_count aggregate_alias: "total_score" # custom alias
results_enum = aggregate_query.get
Expand Down Expand Up @@ -295,7 +295,7 @@
)
)
].to_enum
firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params]
firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params, default_options]

aggregate_query = query.aggregate_query.add_sum 'score', aggregate_alias: "total_score" # custom alias
results_enum = aggregate_query.get
Expand Down Expand Up @@ -433,7 +433,7 @@
)
)
].to_enum
firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params]
firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params, default_options]

aggregate_query = query.aggregate_query.add_avg 'score', aggregate_alias: "total_score" # custom alias
results_enum = aggregate_query.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,99 @@
end
end

it "runs an aggregate query" do
expected_query = Google::Cloud::Firestore::V1::RunAggregationQueryRequest.new(
parent: documents_path,
structured_aggregation_query: Google::Cloud::Firestore::V1::StructuredAggregationQuery.new(
structured_query: Google::Cloud::Firestore::V1::StructuredQuery.new(
from: [Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector.new(collection_id: "users", all_descendants: false)]
),
aggregations: [
Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
count: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Count.new,
alias: "count"
)
]
),
new_transaction: transaction_opt
)
agg_response = [
Google::Cloud::Firestore::V1::RunAggregationQueryResponse.new(
transaction: transaction_id,
result: Google::Cloud::Firestore::V1::AggregationResult.new(
aggregate_fields: {
"count": Google::Cloud::Firestore::V1::Value.new(integer_value: 2)
}
)
)
].to_enum
firestore_mock.expect :run_aggregation_query, agg_response, [expected_query, transaction_options]
firestore_mock.expect :commit, empty_commit_resp, commit_args(transaction: transaction_id)

firestore.transaction do |tx|
aq = firestore.col(:users).aggregate_query.add_count
results = tx.get_aggregate(aq).to_a
_(results.count).must_equal 1
_(results.first.get("count")).must_equal 2
end
end

it "runs multiple aggregate queries" do
structured_agg_query = Google::Cloud::Firestore::V1::StructuredAggregationQuery.new(
structured_query: Google::Cloud::Firestore::V1::StructuredQuery.new(
from: [Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector.new(collection_id: "users", all_descendants: false)]
),
aggregations: [
Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
count: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Count.new,
alias: "count"
)
]
)
first_query = Google::Cloud::Firestore::V1::RunAggregationQueryRequest.new(
parent: documents_path,
structured_aggregation_query: structured_agg_query,
new_transaction: transaction_opt
)
second_query = Google::Cloud::Firestore::V1::RunAggregationQueryRequest.new(
parent: documents_path,
structured_aggregation_query: structured_agg_query,
transaction: transaction_id
)
agg_response = [
Google::Cloud::Firestore::V1::RunAggregationQueryResponse.new(
transaction: transaction_id,
result: Google::Cloud::Firestore::V1::AggregationResult.new(
aggregate_fields: {
"count": Google::Cloud::Firestore::V1::Value.new(integer_value: 2)
}
)
)
].to_enum
firestore_mock.expect :run_aggregation_query, agg_response, [first_query, transaction_options]
firestore_mock.expect :run_aggregation_query, agg_response, [second_query, transaction_options]
firestore_mock.expect :commit, empty_commit_resp, commit_args(transaction: transaction_id)

firestore.transaction do |tx|
aq = firestore.col(:users).aggregate_query.add_count
results = tx.get_aggregate(aq).to_a
_(results.count).must_equal 1
_(results.first.get("count")).must_equal 2

results = tx.get_aggregate(aq).to_a
_(results.count).must_equal 1
_(results.first.get("count")).must_equal 2
end
end

it "that does nothing makes no calls" do
firestore.transaction do |tx|
# no op
end
end

it "creates a new document using string path" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: create_writes)

resp = firestore.transaction commit_response: true do |tx|
Expand All @@ -213,7 +298,7 @@
end

it "creates a new document using doc ref" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: create_writes)

doc = firestore.doc document_path
Expand All @@ -235,7 +320,7 @@
end

it "sets a new document using string path" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: set_writes)

resp = firestore.transaction commit_response: true do |tx|
Expand All @@ -247,7 +332,7 @@
end

it "sets a new document using doc ref" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: set_writes)

doc = firestore.doc document_path
Expand All @@ -269,7 +354,7 @@
end

it "updates a new document using string path" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: update_writes)

resp = firestore.transaction commit_response: true do |tx|
Expand All @@ -281,7 +366,7 @@
end

it "updates a new document using doc ref" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: update_writes)

doc = firestore.doc document_path
Expand All @@ -303,7 +388,7 @@
end

it "deletes a document using string path" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: delete_writes)

resp = firestore.transaction commit_response: true do |tx|
Expand All @@ -315,7 +400,7 @@
end

it "deletes a document using doc ref" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: delete_writes)

doc = firestore.doc document_path
Expand Down Expand Up @@ -355,7 +440,7 @@

it "performs multiple writes in the same commit (string)" do
all_writes = create_writes + set_writes + update_writes + delete_writes
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: all_writes)

resp = firestore.transaction commit_response: true do |tx|
Expand All @@ -371,7 +456,7 @@

it "performs multiple writes in the same commit (doc ref)" do
all_writes = create_writes + set_writes + update_writes + delete_writes
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: all_writes)

doc_ref = firestore.doc document_path
Expand Down Expand Up @@ -500,8 +585,8 @@ def firestore.sleep num
end

it "does not retry when an unsupported error is raised" do
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options]
firestore_mock.expect :rollback, nil, [{ database: database_path, transaction: transaction_id }, default_options]
firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options]
firestore_mock.expect :rollback, nil, [{ database: database_path, transaction: transaction_id }, transaction_options]

# Unable to use mocks to raise an error, so stub the method instead
def firestore_mock.commit req, options
Expand Down
Loading
Loading