forked from googleapis/google-cloud-ruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
routine_test.rb
417 lines (340 loc) · 16.2 KB
/
routine_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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Copyright 2020 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"
require "json"
require "uri"
describe Google::Cloud::Bigquery::Routine, :resource, :mock_bigquery do
let(:dataset) { "my_dataset" }
let(:routine_id) { "my_routine" }
let(:etag) { "etag123456789" }
let(:routine_type) { "SCALAR_FUNCTION" }
let(:now) { ::Time.now }
let(:language) { "SQL" }
let(:return_type) { Google::Cloud::Bigquery::StandardSql::DataType.new type_kind: "INT64" }
let(:imported_libraries) { ["gs://cloud-samples-data/bigquery/udfs/max-value.js"] }
let(:body) { "x * 3" }
let(:description) { "This is my routine" }
let(:determinism_level) { "DETERMINISTIC" }
let(:new_routine_type) { "PROCEDURE" }
let(:new_language) { "JAVASCRIPT" }
let(:new_arguments_gapi) do
[
Google::Apis::BigqueryV2::Argument.new(
data_type: Google::Apis::BigqueryV2::StandardSqlDataType.new(type_kind: "INT64"),
name: "x"
),
Google::Apis::BigqueryV2::Argument.new(
data_type: Google::Apis::BigqueryV2::StandardSqlDataType.new(type_kind: "STRING"),
name: "y"
),
Google::Apis::BigqueryV2::Argument.new(
data_type: Google::Apis::BigqueryV2::StandardSqlDataType.new(type_kind: "BOOL"),
name: "z"
)
]
end
let(:new_arguments) do
[
Google::Cloud::Bigquery::Argument.new(
data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64"),
name: "x"
),
Google::Cloud::Bigquery::Argument.new(
data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING"),
name: "y"
),
Google::Cloud::Bigquery::Argument.new(
data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "BOOL"),
name: "z"
)
]
end
let(:new_return_type) { Google::Cloud::Bigquery::StandardSql::DataType.new type_kind: "STRING" }
let(:new_imported_libraries) { ["gs://cloud-samples-data/bigquery/udfs/max-value-2.js"] }
let(:new_body) { "x * 4" }
let(:new_description) { "This is my updated routine" }
let(:new_determinism_level) { "NOT_DETERMINISTIC" }
let(:routine_hash) { random_routine_hash dataset, routine_id, determinism_level: determinism_level }
let(:routine_gapi) { Google::Apis::BigqueryV2::Routine.from_json routine_hash.to_json }
let(:routine) { Google::Cloud::Bigquery::Routine.from_gapi routine_gapi, bigquery.service }
it "knows its attributes" do
_(routine.routine_id).must_equal routine_id
_(routine.dataset_id).must_equal dataset
_(routine.project_id).must_equal project
# routine_ref is private
_(routine.routine_ref).must_be_kind_of Google::Apis::BigqueryV2::RoutineReference
_(routine.routine_ref.routine_id).must_equal routine_id
_(routine.routine_ref.dataset_id).must_equal dataset
_(routine.routine_ref.project_id).must_equal project
_(routine.etag).must_equal etag
_(routine.routine_type).must_equal "SCALAR_FUNCTION"
_(routine.procedure?).must_equal false
_(routine.scalar_function?).must_equal true
_(routine.created_at).must_be_close_to now, 1
_(routine.modified_at).must_be_close_to now, 1
_(routine.language).must_equal "SQL"
_(routine.javascript?).must_equal false
_(routine.sql?).must_equal true
_(routine.arguments).must_be_kind_of Array
_(routine.arguments).must_be :frozen?
_(routine.arguments.size).must_equal 2
_(routine.arguments[0]).must_be_kind_of Google::Cloud::Bigquery::Argument
_(routine.arguments[0].name).must_equal "arr"
_(routine.arguments[0].argument_kind).must_equal "FIXED_TYPE"
_(routine.arguments[0].fixed_type?).must_equal true
_(routine.arguments[0].any_type?).must_equal false
_(routine.arguments[0].mode).must_equal "IN"
_(routine.arguments[0].in?).must_equal true
_(routine.arguments[0].out?).must_equal false
_(routine.arguments[0].inout?).must_equal false
_(routine.arguments[0].data_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(routine.arguments[0].data_type.type_kind).must_equal "ARRAY"
_(routine.arguments[0].data_type.array_element_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(routine.arguments[0].data_type.array_element_type.type_kind).must_equal "STRUCT"
_(routine.arguments[0].data_type.array_element_type.struct_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::StructType
_(routine.arguments[0].data_type.array_element_type.struct_type.fields).must_be_kind_of Array
_(routine.arguments[0].data_type.array_element_type.struct_type.fields).must_be :frozen?
_(routine.arguments[0].data_type.array_element_type.struct_type.fields.size).must_equal 2
_(routine.arguments[0].data_type.array_element_type.struct_type.fields[0]).must_be_kind_of Google::Cloud::Bigquery::StandardSql::Field
_(routine.arguments[0].data_type.array_element_type.struct_type.fields[0].name).must_equal "my-struct-name"
_(routine.arguments[0].data_type.array_element_type.struct_type.fields[0].type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(routine.arguments[0].data_type.array_element_type.struct_type.fields[0].type.type_kind).must_equal "STRING"
_(routine.arguments[0].data_type.array_element_type.struct_type.fields[1].name).must_equal "my-struct-val"
_(routine.arguments[0].data_type.array_element_type.struct_type.fields[1].type.type_kind).must_equal "INT64"
_(routine.arguments[1].name).must_equal "out"
_(routine.arguments[1].argument_kind).must_equal "ANY_TYPE"
_(routine.arguments[1].fixed_type?).must_equal false
_(routine.arguments[1].any_type?).must_equal true
_(routine.arguments[1].mode).must_equal "OUT"
_(routine.arguments[1].in?).must_equal false
_(routine.arguments[1].out?).must_equal true
_(routine.arguments[1].inout?).must_equal false
_(routine.arguments[1].data_type.type_kind).must_equal "STRING"
_(routine.return_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(routine.return_type.type_kind).must_equal "INT64"
_(routine.imported_libraries).must_equal ["gs://cloud-samples-data/bigquery/udfs/max-value.js"]
_(routine.imported_libraries).must_be :frozen?
_(routine.body).must_equal "x * 3"
_(routine.description).must_equal description
_(routine.determinism_level).must_equal determinism_level
_(routine.determinism_level_deterministic?).must_equal true
_(routine.determinism_level_not_deterministic?).must_equal false
end
it "can test its existence" do
_(routine.exists?).must_equal true
end
it "can test its existence with force to load resource" do
mock = Minitest::Mock.new
mock.expect :get_routine, routine_gapi, [routine.project_id, routine.dataset_id, routine.routine_id]
routine.service.mocked_service = mock
_(routine.exists?(force: true)).must_equal true
mock.verify
end
it "can delete itself" do
mock = Minitest::Mock.new
mock.expect :delete_routine, nil, [project, dataset, routine_id]
routine.service.mocked_service = mock
_(routine.delete).must_equal true
_(routine.exists?).must_equal false
mock.verify
end
it "can reload itself" do
new_description = "New description of the routine."
mock = Minitest::Mock.new
routine_hash = random_routine_hash dataset, routine_id, description: new_description
mock.expect :get_routine, Google::Apis::BigqueryV2::Routine.from_json(routine_hash.to_json),
[project, dataset, routine_id]
routine.service.mocked_service = mock
_(routine.description).must_equal description
routine.reload!
mock.verify
_(routine.description).must_equal new_description
end
it "updates its routine_type" do
_(routine.routine_type).must_equal routine_type
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.routine_type = new_routine_type
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.routine_type = new_routine_type
mock.verify
_(routine.routine_type).must_equal new_routine_type
end
it "updates its language" do
_(routine.language).must_equal language
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.language = new_language
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.language = new_language
mock.verify
_(routine.language).must_equal new_language
end
it "updates its arguments" do
_(routine.arguments.size).must_equal routine_gapi.arguments.size
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.arguments = new_arguments_gapi
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.arguments = new_arguments
mock.verify
_(routine.arguments.size).must_equal new_arguments.size
end
it "updates its return_type" do
_(routine.return_type.type_kind).must_equal return_type.type_kind
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.return_type = Google::Apis::BigqueryV2::StandardSqlDataType.new type_kind: "STRING"
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.return_type = new_return_type
mock.verify
_(routine.return_type.type_kind).must_equal new_return_type.type_kind
end
it "updates its return_type with a string" do
_(routine.return_type.type_kind).must_equal return_type.type_kind
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.return_type = Google::Apis::BigqueryV2::StandardSqlDataType.new type_kind: "STRING"
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.return_type = "STRING"
mock.verify
_(routine.return_type.type_kind).must_equal new_return_type.type_kind
end
it "updates its return_type to nil" do
_(routine.return_type.type_kind).must_equal return_type.type_kind
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.return_type = nil
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.return_type = nil
mock.verify
_(routine.return_type).must_be :nil?
end
it "updates its imported_libraries" do
_(routine.imported_libraries).must_equal imported_libraries
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.imported_libraries = new_imported_libraries
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.imported_libraries = new_imported_libraries
mock.verify
_(routine.imported_libraries).must_equal new_imported_libraries
end
it "updates its body" do
_(routine.body).must_equal body
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.definition_body = new_body
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.body = new_body
mock.verify
_(routine.body).must_equal new_body
end
it "updates its description" do
_(routine.description).must_equal description
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.description = new_description
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.description = new_description
mock.verify
_(routine.description).must_equal new_description
end
it "updates its determinism_level" do
_(routine.determinism_level).must_equal determinism_level
_(routine.determinism_level_deterministic?).must_equal true
_(routine.determinism_level_not_deterministic?).must_equal false
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.determinism_level = new_determinism_level
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.determinism_level = new_determinism_level
mock.verify
_(routine.determinism_level).must_equal new_determinism_level
_(routine.determinism_level_deterministic?).must_equal false
_(routine.determinism_level_not_deterministic?).must_equal true
end
it "updates its attributes in a block" do
_(routine.description).must_equal description
_(routine.determinism_level).must_equal determinism_level
_(routine.determinism_level_deterministic?).must_equal true
_(routine.determinism_level_not_deterministic?).must_equal false
mock = Minitest::Mock.new
updated_routine_gapi = routine_gapi.dup
updated_routine_gapi.routine_type = new_routine_type
updated_routine_gapi.language = new_language
updated_routine_gapi.arguments = new_arguments_gapi
updated_routine_gapi.return_type = Google::Apis::BigqueryV2::StandardSqlDataType.new type_kind: "STRING"
updated_routine_gapi.imported_libraries = new_imported_libraries
updated_routine_gapi.definition_body = new_body
updated_routine_gapi.description = new_description
updated_routine_gapi.determinism_level = new_determinism_level
mock.expect :update_routine, updated_routine_gapi,
[project, dataset, routine_id, updated_routine_gapi, options: { header: { "If-Match" => etag } }]
routine.service.mocked_service = mock
routine.update do |r|
r.routine_type = new_routine_type
r.language = new_language
r.arguments = new_arguments
r.return_type = new_return_type
r.imported_libraries = new_imported_libraries
r.body = new_body
r.description = new_description
r.determinism_level = new_determinism_level
end
mock.verify
_(routine.routine_type).must_equal new_routine_type
_(routine.language).must_equal new_language
_(routine.arguments.size).must_equal new_arguments.size
_(routine.return_type.type_kind).must_equal new_return_type.type_kind
_(routine.imported_libraries).must_equal new_imported_libraries
_(routine.body).must_equal new_body
_(routine.description).must_equal new_description
_(routine.determinism_level).must_equal new_determinism_level
_(routine.determinism_level_deterministic?).must_equal false
_(routine.determinism_level_not_deterministic?).must_equal true
end
it "skips update when no updates are made in a block" do
routine.update do |r|
end
end
it "raises from unsupported methods called on the updater" do
routine.update do |r|
expect { r.update }.must_raise RuntimeError
expect { r.delete }.must_raise RuntimeError
expect { r.reload! }.must_raise RuntimeError
expect { r.refresh! }.must_raise RuntimeError
end
end
end