forked from googleapis/google-cloud-ruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
routine_test.rb
279 lines (240 loc) · 10.4 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
# 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 "bigquery_helper"
describe Google::Cloud::Bigquery, :bigquery do
let(:dataset_id) { "#{prefix}_dataset" }
let(:dataset) do
d = bigquery.dataset dataset_id
if d.nil?
d = bigquery.create_dataset dataset_id
end
d
end
let(:routine_id) { "routine_#{SecureRandom.hex(4)}" }
let :routine_sql do
routine_sql = <<~SQL
CREATE FUNCTION `#{routine_id}`(
arr ARRAY<STRUCT<name STRING, val INT64>>
) AS (
(SELECT SUM(IF(elem.name = "foo",elem.val,null)) FROM UNNEST(arr) AS elem)
)
SQL
end
it "can create from SQL, list, read, update, and delete an SQL routine" do
# create from sql
job = dataset.query_job routine_sql
job.wait_until_done!
_(job).wont_be :failed?
_(job.ddl_operation_performed).must_equal "CREATE"
routine = job.ddl_target_routine
_(routine).must_be_kind_of Google::Cloud::Bigquery::Routine
_(routine.reference?).must_equal true
_(routine.project_id).must_equal bigquery.project
_(routine.dataset_id).must_equal dataset.dataset_id
_(routine.routine_id).must_equal routine_id
# list
_(dataset.routines.all.map(&:routine_id)).must_include routine_id
# list with filter
_(dataset.routines(filter: "routineType:SCALAR_FUNCTION").all.map(&:routine_id)).must_include routine_id
# list with filter
_(dataset.routines(filter: "routineType:PROCEDURE").all.map(&:routine_id)).wont_include routine_id
# get
routine = dataset.routine routine_id
_(routine).must_be_kind_of Google::Cloud::Bigquery::Routine
_(routine.project_id).must_equal bigquery.project
_(routine.dataset_id).must_equal dataset.dataset_id
_(routine.routine_id).must_equal routine_id
_(routine.description).must_be :nil?
_(routine.routine_type).must_equal "SCALAR_FUNCTION"
_(routine.language).must_equal "SQL"
_(routine.body).must_equal "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)"
arguments = routine.arguments
_(arguments).must_be_kind_of Array
_(arguments.size).must_equal 1
argument = arguments.first
_(argument).must_be_kind_of Google::Cloud::Bigquery::Argument
_(argument.argument_kind).must_be :nil?
_(argument.fixed_type?).must_equal true
_(argument.any_type?).must_equal false
_(argument.mode).must_be :nil?
_(argument.in?).must_equal false
_(argument.out?).must_equal false
_(argument.inout?).must_equal false
_(argument.name).must_equal "arr"
data_type = argument.data_type
_(data_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(data_type.type_kind).must_equal "ARRAY"
_(data_type.struct_type).must_be :nil?
_(data_type.array_element_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(data_type.array_element_type.type_kind).must_equal "STRUCT"
_(data_type.array_element_type.struct_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::StructType
struct_fields = data_type.array_element_type.struct_type.fields
_(struct_fields).must_be_kind_of Array
_(struct_fields.size).must_equal 2
_(struct_fields[0]).must_be_kind_of Google::Cloud::Bigquery::StandardSql::Field
_(struct_fields[0].name).must_equal "name"
_(struct_fields[0].type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(struct_fields[0].type.type_kind).must_equal "STRING"
_(struct_fields[1]).must_be_kind_of Google::Cloud::Bigquery::StandardSql::Field
_(struct_fields[1].name).must_equal "val"
_(struct_fields[1].type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(struct_fields[1].type.type_kind).must_equal "INT64"
# update
new_description = "Routine was updated #{Time.now}"
routine.description = new_description
routine.refresh!
_(routine.description).must_equal new_description
# delete
_(routine.delete).must_equal true
_(dataset.routine(routine_id)).must_be_nil
end
it "can create, update and delete an SQL routine" do
# create
routine = dataset.create_routine routine_id do |r|
r.routine_type = "SCALAR_FUNCTION"
r.language = :SQL
r.arguments = [
Google::Cloud::Bigquery::Argument.new(name: "x", data_type: "INT64")
]
r.body = "x * 3"
r.description = "my description"
end
_(routine).must_be_kind_of Google::Cloud::Bigquery::Routine
_(routine.project_id).must_equal bigquery.project
_(routine.dataset_id).must_equal dataset.dataset_id
_(routine.routine_id).must_equal routine_id
_(routine.routine_type).must_equal "SCALAR_FUNCTION"
_(routine.language).must_equal "SQL"
_(routine.body).must_equal "x * 3"
_(routine.description).must_equal "my description"
arguments = routine.arguments
_(arguments).must_be_kind_of Array
_(arguments.size).must_equal 1
argument = arguments.first
_(argument).must_be_kind_of Google::Cloud::Bigquery::Argument
_(argument.argument_kind).must_be :nil?
_(argument.mode).must_be :nil?
_(argument.name).must_equal "x"
data_type = argument.data_type
_(data_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(data_type.type_kind).must_equal "INT64"
_(data_type.array_element_type).must_be :nil?
_(data_type.struct_type).must_be :nil?
# update
new_body = "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)"
new_arguments = [
Google::Cloud::Bigquery::Argument.new(
name: "arr",
argument_kind: "FIXED_TYPE",
data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
type_kind: "ARRAY",
array_element_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
type_kind: "STRUCT",
struct_type: Google::Cloud::Bigquery::StandardSql::StructType.new(
fields: [
Google::Cloud::Bigquery::StandardSql::Field.new(
name: "name",
type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING")
),
Google::Cloud::Bigquery::StandardSql::Field.new(
name: "val",
type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64")
)
]
)
)
)
)
]
routine.update do |r|
r.body = new_body
r.arguments = new_arguments
end
_(routine.body).must_equal new_body
arguments = routine.arguments
_(arguments).must_be_kind_of Array
_(arguments.size).must_equal 1
argument = arguments.first
_(argument).must_be_kind_of Google::Cloud::Bigquery::Argument
_(argument.argument_kind).must_equal "FIXED_TYPE"
_(argument.mode).must_be :nil?
_(argument.name).must_equal "arr"
data_type = argument.data_type
_(data_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(data_type.type_kind).must_equal "ARRAY"
_(data_type.struct_type).must_be :nil?
_(data_type.array_element_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(data_type.array_element_type.type_kind).must_equal "STRUCT"
_(data_type.array_element_type.struct_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::StructType
struct_fields = data_type.array_element_type.struct_type.fields
_(struct_fields).must_be_kind_of Array
_(struct_fields.size).must_equal 2
_(struct_fields[0]).must_be_kind_of Google::Cloud::Bigquery::StandardSql::Field
_(struct_fields[0].name).must_equal "name"
_(struct_fields[0].type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(struct_fields[0].type.type_kind).must_equal "STRING"
_(struct_fields[1]).must_be_kind_of Google::Cloud::Bigquery::StandardSql::Field
_(struct_fields[1].name).must_equal "val"
_(struct_fields[1].type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(struct_fields[1].type.type_kind).must_equal "INT64"
# get
routine.reload!
_(routine.body).must_equal new_body
_(routine.arguments.first.data_type.array_element_type.struct_type.fields.last.type.type_kind).must_equal "INT64"
# delete
_(routine.delete).must_equal true
_(dataset.routine(routine_id)).must_be_nil
end
it "can create and delete a JavaScript routine" do
# create
routine = dataset.create_routine routine_id do |r|
r.routine_type = "SCALAR_FUNCTION"
r.language = :JAVASCRIPT
r.arguments = [
Google::Cloud::Bigquery::Argument.new(name: "x", data_type: "INT64")
]
r.body = "return x * 3;"
r.return_type = "INT64"
r.description = "my description"
r.determinism_level = "DETERMINISTIC"
end
_(routine).must_be_kind_of Google::Cloud::Bigquery::Routine
_(routine.project_id).must_equal bigquery.project
_(routine.dataset_id).must_equal dataset.dataset_id
_(routine.routine_id).must_equal routine_id
_(routine.routine_type).must_equal "SCALAR_FUNCTION"
_(routine.language).must_equal "JAVASCRIPT"
_(routine.body).must_equal "return x * 3;"
_(routine.description).must_equal "my description"
_(routine.determinism_level).must_equal "DETERMINISTIC"
_(routine.determinism_level_deterministic?).must_equal true
_(routine.determinism_level_not_deterministic?).must_equal false
arguments = routine.arguments
_(arguments).must_be_kind_of Array
_(arguments.size).must_equal 1
argument = arguments.first
_(argument).must_be_kind_of Google::Cloud::Bigquery::Argument
_(argument.argument_kind).must_be :nil?
_(argument.mode).must_be :nil?
_(argument.name).must_equal "x"
data_type = argument.data_type
_(data_type).must_be_kind_of Google::Cloud::Bigquery::StandardSql::DataType
_(data_type.type_kind).must_equal "INT64"
_(data_type.array_element_type).must_be :nil?
_(data_type.struct_type).must_be :nil?
# delete
_(routine.delete).must_equal true
_(dataset.routine(routine_id)).must_be_nil
end
end