-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtest_function.rb
More file actions
244 lines (210 loc) · 7.45 KB
/
test_function.rb
File metadata and controls
244 lines (210 loc) · 7.45 KB
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
# 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 "ostruct"
describe FunctionsFramework::Function do
it "represents an http function using a block" do
tester = self
function = FunctionsFramework::Function.http "my_func" do |request|
tester.assert_equal "the-request", request
tester.assert_equal "my_func", global(:function_name)
"hello"
end
assert_equal "my_func", function.name
assert_equal :http, function.type
response = function.call "the-request", globals: { function_name: function.name }
assert_equal "hello", response
end
it "represents an http function using a block with a return statement" do
function = FunctionsFramework::Function.http "my_func" do |request|
return "hello" if request == "the-request"
"goodbye"
end
assert_equal "my_func", function.name
assert_equal :http, function.type
response = function.call "the-request"
assert_equal "hello", response
end
it "defines a cloud_event function using a block" do
tester = self
function = FunctionsFramework::Function.cloud_event "my_event_func" do |event|
tester.assert_equal "the-event", event
tester.assert_equal "my_event_func", global(:function_name)
"ok"
end
assert_equal "my_event_func", function.name
assert_equal :cloud_event, function.type
function.call "the-event", globals: { function_name: function.name }
end
it "defines a startup function using a block" do
tester = self
function = FunctionsFramework::Function.startup_task do |func|
tester.assert_equal "the-function", func
tester.assert_nil global(:function_name)
end
assert_nil function.name
assert_equal :startup_task, function.type
function.call "the-function", globals: { function_name: function.name }
end
it "represents an http function using an object" do
callable = proc do |request|
assert_equal "the-request", request
"hello"
end
function = FunctionsFramework::Function.http "my_func", callable: callable
assert_equal "my_func", function.name
assert_equal :http, function.type
response = function.call "the-request"
assert_equal "hello", response
end
it "represents an http function using a class" do
class MyCallable
def initialize **_keywords
end
def call request
request == "the-request" ? "hello" : "whoops"
end
end
function = FunctionsFramework::Function.http "my_func", callable: MyCallable
assert_equal "my_func", function.name
assert_equal :http, function.type
response = function.call "the-request"
assert_equal "hello", response
end
it "can call a startup function with no formal argument" do
tester = self
function = FunctionsFramework::Function.startup_task do
tester.assert_nil global(:function_name)
end
function.call "the-function", globals: { function_name: function.name }
end
it "sets a global from a startup task" do
tester = self
startup = FunctionsFramework::Function.startup_task do
set_global :foo, :bar
end
function = FunctionsFramework::Function.http "my_func" do |_request|
tester.assert_equal :bar, global(:foo)
"hello"
end
globals = {}
startup.call "the-startup", globals: globals
function.call "the-function", globals: globals
end
it "sets a lazy global from a startup task" do
tester = self
counter = 0
startup = FunctionsFramework::Function.startup_task do
set_global :foo do
counter += 1
:bar
end
end
function = FunctionsFramework::Function.http "my_func" do |_request|
tester.assert_equal :bar, global(:foo)
"hello"
end
globals = {}
startup.call "the-startup", globals: globals
assert_equal 0, counter
function.call "the-function", globals: globals
assert_equal 1, counter
function.call "the-function", globals: globals
assert_equal 1, counter
end
it "allows a global of type Minitest::Mock" do
startup = FunctionsFramework::Function.startup_task do
set_global :foo, Minitest::Mock.new
end
function = FunctionsFramework::Function.http "my_func" do |_request|
global :foo
"hello"
end
globals = {}
startup.call "the-startup", globals: globals
function.call "the-function", globals: globals
end
describe "typed" do
# Ruby class representing a single integer value encoded as a JSON int
class IntValue
def initialize val
@value = val
end
def self.decode_json json
IntValue.new json.to_i
end
def to_json(*_args)
get.to_s
end
def get
@value
end
end
# class_func provides a function as a class that implements the Callable
# interface.
class_func = ::Class.new FunctionsFramework::Function::Callable do
define_method :call do |_request|
global :function_name
end
end
it "can be defined with no custom type" do
function = FunctionsFramework::Function.typed "int_adder" do |request|
request + 1
end
assert_equal "int_adder", function.name
assert_equal :typed, function.type
res = function.call 1, globals: {}
assert_equal 2, res
end
it "can be defined using a block and custom_type" do
function = FunctionsFramework::Function.typed "int_adder", request_class: IntValue do |request|
IntValue.new request.get + 1
end
assert_equal "int_adder", function.name
assert_equal :typed, function.type
res = function.call IntValue.new(1), globals: {}
assert_equal 2, res.get
end
it "can be defined using a callable class" do
function = FunctionsFramework::Function.typed "using_callable_class", callable: class_func
assert_equal "using_callable_class", function.name
assert_equal :typed, function.type
globals = function.populate_globals
res = function.call nil, globals: globals
assert_equal function.name, res
end
it "can be defined using an instance of a callable" do
callable_class = class_func.new globals: { function_name: "fake_global_name" }
function = FunctionsFramework::Function.typed "using_callable", callable: callable_class
assert_equal "using_callable", function.name
assert_equal :typed, function.type
globals = function.populate_globals
res = function.call nil, globals: globals
assert_equal "fake_global_name", res
end
it "function can access globals" do
function = FunctionsFramework::Function.typed "printName" do |_request|
global :function_name
end
globals = function.populate_globals
res = function.call nil, globals: globals
assert_equal function.name, res
end
it "function rejects request_class that does not implement decode_json" do
assert_raises ::ArgumentError do
FunctionsFramework::Function.typed "bad_fn", request_class: ::Class
end
end
end
end