-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcel_env_test.py
More file actions
333 lines (310 loc) · 9.21 KB
/
Copy pathcel_env_test.py
File metadata and controls
333 lines (310 loc) · 9.21 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
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
# Copyright 2026 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.
"""Tests for Env/Config.
This module contains tests for the `cel.EnvConfig` class, focusing on its
ability to be created from and serialized to YAML format.
"""
from absl.testing import absltest
from cel_expr_python import cel
from cel_expr_python.ext import ext_math
from cel.expr.conformance.proto2 import test_all_types_pb2 as test_all_types_pb
class CelEnvTest(absltest.TestCase):
def test_env_config_from_and_to_yaml(self):
config = cel.NewEnvConfigFromYaml("""
name: foo
container: test.container
stdlib:
exclude_macros:
- map
- filter
exclude_functions:
- name: "_+_"
extensions:
- name: math
variables:
- name: one
type_name: int
value: 1
functions:
- name: add
overloads:
- id: "add_int_int"
args:
- type_name: int
- type_name: int
return:
type_name: int
""")
yaml = config.to_yaml()
self.assertEqual(
normalize_yaml(yaml),
normalize_yaml("""
name: "foo"
container: "test.container"
extensions:
- name: "math"
stdlib:
exclude_macros:
- "filter"
- "map"
exclude_functions:
- name: "_+_"
variables:
- name: "one"
type_name: "int"
value: 1
functions:
- name: "add"
overloads:
- id: "add_int_int"
args:
- type_name: "int"
- type_name: "int"
return:
type_name: "int"
"""),
)
def test_invalid_yaml(self):
with self.assertRaises(Exception) as e:
cel.NewEnvConfigFromYaml(" invalid yaml")
self.assertIn(
"1:2: Invalid CEL environment config YAML\n"
+ "| invalid yaml\n"
+ "| ^",
str(e.exception),
)
def test_config_export_variables(self):
config = cel.NewEnv(
variables={
"var_bool": cel.Type.BOOL,
"var_int": cel.Type.INT,
"var_uint": cel.Type.UINT,
"var_double": cel.Type.DOUBLE,
"var_str": cel.Type.STRING,
"var_bytes": cel.Type.BYTES,
"var_msg": cel.Type("cel.expr.conformance.proto2.TestAllTypes"),
"var_string_list": cel.Type.List(cel.Type.STRING),
"var_timestamp": cel.Type.TIMESTAMP,
"var_duration": cel.Type.DURATION,
"var_dyn_list": cel.Type.LIST,
"var_int_map": cel.Type.Map(cel.Type.INT, cel.Type.STRING),
"var_string_map": cel.Type.Map(cel.Type.STRING, cel.Type.BOOL),
"var_dyn_map": cel.Type.MAP,
"var_dyn": cel.Type.DYN,
}
)
yaml = config.config().to_yaml()
self.assertEqual(
normalize_yaml(yaml),
normalize_yaml("""
variables:
- name: "var_bool"
type_name: "bool"
- name: "var_bytes"
type_name: "bytes"
- name: "var_double"
type_name: "double"
- name: "var_duration"
type_name: "duration"
- name: "var_dyn"
type_name: "dyn"
- name: "var_dyn_list"
type_name: "list"
params:
- type_name: "dyn"
- name: "var_dyn_map"
type_name: "map"
params:
- type_name: "dyn"
- type_name: "dyn"
- name: "var_int"
type_name: "int"
- name: "var_int_map"
type_name: "map"
params:
- type_name: "int"
- type_name: "string"
- name: "var_msg"
type_name: "cel.expr.conformance.proto2.TestAllTypes"
- name: "var_str"
type_name: "string"
- name: "var_string_list"
type_name: "list"
params:
- type_name: "string"
- name: "var_string_map"
type_name: "map"
params:
- type_name: "string"
- type_name: "bool"
- name: "var_timestamp"
type_name: "timestamp"
- name: "var_uint"
type_name: "uint"
"""),
)
def test_config_augmented_variables(self):
config = cel.NewEnvConfigFromYaml("""
variables:
- name: "var_bool"
type_name: "bool"
""")
env = cel.NewEnv(
config=config,
variables={
"var_msg": cel.Type("cel.expr.conformance.proto2.TestAllTypes"),
},
)
yaml = env.config().to_yaml()
self.assertEqual(
normalize_yaml(yaml),
normalize_yaml("""
variables:
- name: "var_bool"
type_name: "bool"
- name: "var_msg"
type_name: "cel.expr.conformance.proto2.TestAllTypes"
"""),
)
def test_config_variable_override(self):
config = cel.NewEnvConfigFromYaml("""
variables:
- name: "var_bool"
type_name: "bool"
""")
with self.assertRaises(Exception) as e:
cel.NewEnv(
config=config,
variables={
"var_bool": cel.Type.INT,
},
)
self.assertIn(
"Variable 'var_bool' is already included",
str(e.exception),
)
def test_config_variable_types(self):
config = cel.NewEnvConfigFromYaml("""
variables:
- name: "var_bool"
type_name: "bool"
- name: "var_int"
type_name: "int"
value: 42
""")
env = cel.NewEnv(
config=config,
variables={
"var_msg": cel.Type("cel.expr.conformance.proto2.TestAllTypes"),
},
)
data = {
"var_bool": True,
"var_msg": test_all_types_pb.TestAllTypes(single_string="hello"),
}
res = env.compile("var_bool").eval(data=data)
self.assertEqual(res.type(), cel.Type.BOOL)
self.assertTrue(res.value())
res = env.compile("var_msg.single_string").eval(data=data)
self.assertEqual(res.type(), cel.Type.STRING)
self.assertEqual(res.value(), "hello")
res = env.compile("var_int").eval(data=data)
self.assertEqual(res.type(), cel.Type.INT)
self.assertEqual(res.value(), 42)
def test_config_extensions(self):
config = cel.NewEnvConfigFromYaml("""
extensions:
- name: math
- name: strings
""")
env = cel.NewEnv(
config=config,
extensions=[TestCelExtension()],
)
yaml = env.config().to_yaml()
self.assertEqual(
normalize_yaml(yaml),
normalize_yaml("""
extensions:
- name: "math"
- name: "strings"
- name: "test_cel_extension"
"""),
)
res = env.compile("'%.4f'.format([math.sqrt(2)])").eval()
self.assertEqual(res.value(), "1.4142")
res = env.compile("hello('World')").eval()
self.assertEqual(res.value(), "Hello, World!")
def test_config_extensions_override(self):
# TODO(b/498655870): add assertion based on extension aliases once
# supported.
config = cel.NewEnvConfigFromYaml("""
extensions:
- name: cel.lib.ext.math
version: 0
- name: cel.lib.ext.strings
""")
with self.assertRaises(Exception) as e:
cel.NewEnv(
config=config,
extensions=[ext_math.ExtMath()],
)
self.assertIn(
"Extension 'cel.lib.ext.math' version 0 is already included. Cannot"
" also include version 'latest'",
str(e.exception),
)
class TestCelExtension(cel.CelExtension):
"""An example CEL extension for testing."""
def __init__(self):
super().__init__(
"test_cel_extension",
functions=[
cel.FunctionDecl(
"hello",
[
cel.Overload(
"hello(string)",
return_type=cel.Type.STRING,
parameters=[
cel.Type.STRING,
],
impl=lambda arg: f"Hello, {arg}!",
)
],
),
],
)
def normalize_yaml(yaml: str) -> str:
lines = yaml.split("\n")
indent = -1
unindented_lines = []
for line in lines:
pos = -1
for i, char in enumerate(line):
if char != " " and char != "\t":
pos = i
break
if pos == -1:
# Skip blank lines.
continue
if indent == -1:
indent = pos
if pos >= indent:
unindented_lines.append(line[indent:])
else:
unindented_lines.append(line)
return "\n".join(unindented_lines)
if __name__ == "__main__":
absltest.main()