-
Notifications
You must be signed in to change notification settings - Fork 535
/
execute_partition_update_test.rb
250 lines (189 loc) · 15.6 KB
/
execute_partition_update_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "helper"
describe Google::Cloud::Spanner::Client, :execute_partition_update, :mock_spanner do
let(:instance_id) { "my-instance-id" }
let(:database_id) { "my-database-id" }
let(:session_id) { "session123" }
let(:session_grpc) { Google::Spanner::V1::Session.new name: session_path(instance_id, database_id, session_id) }
let(:session) { Google::Cloud::Spanner::Session.from_grpc session_grpc, spanner.service }
let(:transaction_id) { "tx789" }
let(:transaction_grpc) { Google::Spanner::V1::Transaction.new id: transaction_id }
let(:transaction) { Google::Cloud::Spanner::Transaction.from_grpc transaction_grpc, session }
let(:tx_selector) { Google::Spanner::V1::TransactionSelector.new id: transaction_id }
let(:pdml_tx_opts) { Google::Spanner::V1::TransactionOptions.new(partitioned_dml: Google::Spanner::V1::TransactionOptions::PartitionedDml.new) }
let(:client) { spanner.client instance_id, database_id, pool: { min: 0 } }
let(:default_options) { Google::Gax::CallOptions.new kwargs: { "google-cloud-resource-prefix" => database_path(instance_id, database_id) } }
let(:results_grpc) {
Google::Spanner::V1::PartialResultSet.new(
metadata: Google::Spanner::V1::ResultSetMetadata.new(
row_type: Google::Spanner::V1::StructType.new(
fields: []
)
),
values: [],
stats: Google::Spanner::V1::ResultSetStats.new(
row_count_lower_bound: 1
)
)
}
let(:results_enum) { Array(results_grpc).to_enum }
it "can execute a PDML statement" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET active = true", transaction: tx_selector, options: default_options
row_count = client.execute_partition_update "UPDATE users SET active = true"
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with bool param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET active = @active", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "active" => Google::Protobuf::Value.new(bool_value: true) }), param_types: { "active" => Google::Spanner::V1::Type.new(code: :BOOL) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET active = @active", params: { active: true }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with int param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET age = @age", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "age" => Google::Protobuf::Value.new(string_value: "29") }), param_types: { "age" => Google::Spanner::V1::Type.new(code: :INT64) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET age = @age", params: { age: 29 }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with float param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET score = @score", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "score" => Google::Protobuf::Value.new(number_value: 0.9) }), param_types: { "score" => Google::Spanner::V1::Type.new(code: :FLOAT64) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET score = @score", params: { score: 0.9 }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with Time param" do
timestamp = Time.parse "2017-01-01 20:04:05.06 -0700"
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET updated_at = @updated_at", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "updated_at" => Google::Protobuf::Value.new(string_value: "2017-01-02T03:04:05.060000000Z") }), param_types: { "updated_at" => Google::Spanner::V1::Type.new(code: :TIMESTAMP) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET updated_at = @updated_at", params: { updated_at: timestamp }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with Date param" do
date = Date.parse "2017-01-02"
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET birthday = @birthday", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "birthday" => Google::Protobuf::Value.new(string_value: "2017-01-02") }), param_types: { "birthday" => Google::Spanner::V1::Type.new(code: :DATE) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET birthday = @birthday", params: { birthday: date }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with String param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET name = @name", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "name" => Google::Protobuf::Value.new(string_value: "Charlie") }), param_types: { "name" => Google::Spanner::V1::Type.new(code: :STRING) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET name = @name", params: { name: "Charlie" }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with IO-ish param" do
file = StringIO.new "contents"
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET avatar = @avatar", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "avatar" => Google::Protobuf::Value.new(string_value: Base64.strict_encode64("contents")) }), param_types: { "avatar" => Google::Spanner::V1::Type.new(code: :BYTES) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET avatar = @avatar", params: { avatar: file }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with an Array param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET project_ids = @list", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "list" => Google::Protobuf::Value.new(list_value: Google::Protobuf::ListValue.new(values: [Google::Protobuf::Value.new(string_value: "1"), Google::Protobuf::Value.new(string_value: "2"), Google::Protobuf::Value.new(string_value: "3")])) }), param_types: { "list" => Google::Spanner::V1::Type.new(code: :ARRAY, array_element_type: Google::Spanner::V1::Type.new(code: :INT64)) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET project_ids = @list", params: { list: [1,2,3] }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with an empty Array param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET project_ids = @list", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "list" => Google::Protobuf::Value.new(list_value: Google::Protobuf::ListValue.new(values: [])) }), param_types: { "list" => Google::Spanner::V1::Type.new(code: :ARRAY, array_element_type: Google::Spanner::V1::Type.new(code: :INT64)) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET project_ids = @list", params: { list: [] }, types: { list: [:INT64] }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with a simple Hash param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET settings = @dict", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "dict" => Google::Protobuf::Value.new(list_value: Google::Protobuf::ListValue.new(values: [Google::Protobuf::Value.new(string_value: "production")])) }), param_types: { "dict" => Google::Spanner::V1::Type.new(code: :STRUCT, struct_type: Google::Spanner::V1::StructType.new(fields: [Google::Spanner::V1::StructType::Field.new(name: "env", type: Google::Spanner::V1::Type.new(code: :STRING))])) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET settings = @dict", params: { dict: { env: :production } }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with a complex Hash param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET settings = @dict", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "dict" => Google::Protobuf::Value.new(list_value: Google::Protobuf::ListValue.new(values: [Google::Protobuf::Value.new(string_value: "production"), Google::Protobuf::Value.new(number_value: 0.9), Google::Protobuf::Value.new(list_value: Google::Protobuf::ListValue.new(values: [Google::Protobuf::Value.new(string_value: "1"), Google::Protobuf::Value.new(string_value: "2"), Google::Protobuf::Value.new(string_value: "3")] )) ])) }), param_types: { "dict" => Google::Spanner::V1::Type.new(code: :STRUCT, struct_type: Google::Spanner::V1::StructType.new(fields: [Google::Spanner::V1::StructType::Field.new(name: "env", type: Google::Spanner::V1::Type.new(code: :STRING)), Google::Spanner::V1::StructType::Field.new(name: "score", type: Google::Spanner::V1::Type.new(code: :FLOAT64)), Google::Spanner::V1::StructType::Field.new(name: "project_ids", type: Google::Spanner::V1::Type.new(code: :ARRAY, array_element_type: Google::Spanner::V1::Type.new(code: :INT64)))] )) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET settings = @dict", params: { dict: { env: "production", score: 0.9, project_ids: [1,2,3] } }
mock.verify
row_count.must_equal 1
end
it "can execute a PDML statement with an empty Hash param" do
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql results_enum, session_grpc.name, "UPDATE users SET settings = @dict", transaction: tx_selector, params: Google::Protobuf::Struct.new(fields: { "dict" => Google::Protobuf::Value.new(list_value: Google::Protobuf::ListValue.new(values: [])) }), param_types: { "dict" => Google::Spanner::V1::Type.new(code: :STRUCT, struct_type: Google::Spanner::V1::StructType.new(fields: [])) }, options: default_options
row_count = client.execute_partition_update "UPDATE users SET settings = @dict", params: { dict: { } }
mock.verify
row_count.must_equal 1
end
it "raises InvalidArgumentError if the response does not contain stats" do
no_stats_results_grpc = results_grpc.dup
no_stats_results_grpc.stats = nil
no_stats_results_enum = Array(no_stats_results_grpc).to_enum
mock = Minitest::Mock.new
mock.expect :create_session, session_grpc, [database_path(instance_id, database_id), session: nil, options: default_options]
mock.expect :begin_transaction, transaction_grpc, [session_grpc.name, pdml_tx_opts, options: default_options]
spanner.service.mocked_service = mock
expect_execute_streaming_sql no_stats_results_enum, session_grpc.name, "UPDATE users SET active = true", transaction: tx_selector, options: default_options
err = expect do
client.execute_partition_update "UPDATE users SET active = true"
end.must_raise Google::Cloud::InvalidArgumentError
mock.verify
end
end