|
18 | 18 | ability to be created from and serialized to YAML format. |
19 | 19 | """ |
20 | 20 |
|
| 21 | +import textwrap |
| 22 | + |
21 | 23 | from absl.testing import absltest |
22 | 24 | from cel_expr_python import cel |
| 25 | +from cel_expr_python.ext import ext_bindings |
| 26 | +from cel_expr_python.ext import ext_math |
| 27 | +from cel_expr_python.ext import ext_optional |
| 28 | +from cel_expr_python.ext import ext_strings |
23 | 29 | from cel.expr.conformance.proto2 import test_all_types_pb2 as test_all_types_pb |
24 | 30 |
|
25 | 31 |
|
@@ -91,6 +97,16 @@ def test_invalid_yaml(self): |
91 | 97 | str(e.exception), |
92 | 98 | ) |
93 | 99 |
|
| 100 | + def test_config_export_container(self): |
| 101 | + env = cel.NewEnv(container="test.container") |
| 102 | + yaml = env.config().to_yaml() |
| 103 | + self.assertEqual( |
| 104 | + normalize_yaml(yaml), |
| 105 | + normalize_yaml(""" |
| 106 | + container: "test.container" |
| 107 | + """), |
| 108 | + ) |
| 109 | + |
94 | 110 | def test_config_export_variables(self): |
95 | 111 | config = cel.NewEnv( |
96 | 112 | variables={ |
@@ -236,27 +252,148 @@ def test_config_variable_types(self): |
236 | 252 | self.assertEqual(res.type(), cel.Type.INT) |
237 | 253 | self.assertEqual(res.value(), 42) |
238 | 254 |
|
| 255 | + def test_config_export_extension_version(self): |
| 256 | + env = cel.NewEnv( |
| 257 | + extensions=[ |
| 258 | + ext_math.ExtMath(0), |
| 259 | + ext_optional.ExtOptional(1), |
| 260 | + ext_strings.ExtStrings(2), |
| 261 | + ext_bindings.ExtBindings(), |
| 262 | + ], |
| 263 | + ) |
| 264 | + yaml = env.config().to_yaml() |
| 265 | + self.assertEqual( |
| 266 | + normalize_yaml(yaml), |
| 267 | + normalize_yaml(""" |
| 268 | + extensions: |
| 269 | + - name: "bindings" |
| 270 | + - name: "math" |
| 271 | + version: 0 |
| 272 | + - name: "optional" |
| 273 | + version: 1 |
| 274 | + - name: "strings" |
| 275 | + version: 2 |
| 276 | + """), |
| 277 | + ) |
| 278 | + |
| 279 | + def test_config_extension_version_out_of_range(self): |
| 280 | + cases = [ |
| 281 | + [ |
| 282 | + lambda: ext_math.ExtMath(42), |
| 283 | + r"'math' extension version: 42 not in range \[0, \d+\]", |
| 284 | + ], |
| 285 | + [ |
| 286 | + lambda: ext_optional.ExtOptional(6), |
| 287 | + r"'optional' extension version: 6 not in range \[0, \d+\]", |
| 288 | + ], |
| 289 | + [ |
| 290 | + lambda: ext_strings.ExtStrings(18), |
| 291 | + r"'strings' extension version: 18 not in range \[0, \d+\]", |
| 292 | + ], |
| 293 | + ] |
| 294 | + for test_case in cases: |
| 295 | + with self.assertRaises(Exception) as e: |
| 296 | + cel.NewEnv( |
| 297 | + extensions=[test_case[0]()], |
| 298 | + ) |
| 299 | + self.assertRegex(str(e.exception), test_case[1]) |
| 300 | + |
| 301 | + def test_config_extensions(self): |
| 302 | + config = cel.NewEnvConfigFromYaml(""" |
| 303 | + extensions: |
| 304 | + - name: math |
| 305 | + - name: strings |
| 306 | + """) |
| 307 | + env = cel.NewEnv( |
| 308 | + config=config, |
| 309 | + extensions=[TestCelExtension()], |
| 310 | + ) |
| 311 | + yaml = env.config().to_yaml() |
| 312 | + self.assertEqual( |
| 313 | + normalize_yaml(yaml), |
| 314 | + normalize_yaml(""" |
| 315 | + extensions: |
| 316 | + - name: "math" |
| 317 | + - name: "strings" |
| 318 | + - name: "test_cel_extension" |
| 319 | + """), |
| 320 | + ) |
| 321 | + res = env.compile("'%.4f'.format([math.sqrt(2)])").eval() |
| 322 | + self.assertEqual(res.value(), "1.4142") |
| 323 | + res = env.compile("hello('World')").eval() |
| 324 | + self.assertEqual(res.value(), "Hello, World!") |
| 325 | + |
| 326 | + def test_config_extension_override_same_version(self): |
| 327 | + config = cel.NewEnvConfigFromYaml(""" |
| 328 | + extensions: |
| 329 | + - name: cel.lib.ext.math |
| 330 | + version: 1 |
| 331 | + - name: strings |
| 332 | + version: 2 |
| 333 | + """) |
| 334 | + env = cel.NewEnv( |
| 335 | + config=config, |
| 336 | + extensions=[ext_math.ExtMath(1), ext_strings.ExtStrings(2)], |
| 337 | + ) |
| 338 | + res = env.compile("'%.3f'.format([math.floor(3.14)])").eval() |
| 339 | + self.assertEqual(res.value(), "3.000") |
| 340 | + |
| 341 | + def test_config_extension_override_different_version(self): |
| 342 | + config = cel.NewEnvConfigFromYaml(""" |
| 343 | + extensions: |
| 344 | + - name: math |
| 345 | + version: 0 |
| 346 | + - name: cel.lib.ext.strings |
| 347 | + version: 2 |
| 348 | + """) |
| 349 | + with self.assertRaises(Exception) as e: |
| 350 | + cel.NewEnv( |
| 351 | + config=config, |
| 352 | + extensions=[ext_math.ExtMath()], |
| 353 | + ) |
| 354 | + self.assertIn( |
| 355 | + "Extension 'math' version 0 is already included. Cannot" |
| 356 | + " also include version 2", |
| 357 | + str(e.exception), |
| 358 | + ) |
| 359 | + with self.assertRaises(Exception) as e: |
| 360 | + cel.NewEnv( |
| 361 | + config=config, |
| 362 | + extensions=[ext_strings.ExtStrings(1)], |
| 363 | + ) |
| 364 | + self.assertIn( |
| 365 | + "Extension 'cel.lib.ext.strings' version 2 is already included. Cannot" |
| 366 | + " also include version 1", |
| 367 | + str(e.exception), |
| 368 | + ) |
| 369 | + |
| 370 | + |
| 371 | +class TestCelExtension(cel.CelExtension): |
| 372 | + """An example CEL extension for testing.""" |
| 373 | + |
| 374 | + def __init__(self): |
| 375 | + super().__init__( |
| 376 | + "test_cel_extension", |
| 377 | + functions=[ |
| 378 | + cel.FunctionDecl( |
| 379 | + "hello", |
| 380 | + [ |
| 381 | + cel.Overload( |
| 382 | + "hello(string)", |
| 383 | + return_type=cel.Type.STRING, |
| 384 | + parameters=[ |
| 385 | + cel.Type.STRING, |
| 386 | + ], |
| 387 | + impl=lambda arg: f"Hello, {arg}!", |
| 388 | + ) |
| 389 | + ], |
| 390 | + ), |
| 391 | + ], |
| 392 | + ) |
| 393 | + |
239 | 394 |
|
240 | 395 | def normalize_yaml(yaml: str) -> str: |
241 | | - lines = yaml.split("\n") |
242 | | - indent = -1 |
243 | | - unindented_lines = [] |
244 | | - for line in lines: |
245 | | - pos = -1 |
246 | | - for i, char in enumerate(line): |
247 | | - if char != " " and char != "\t": |
248 | | - pos = i |
249 | | - break |
250 | | - if pos == -1: |
251 | | - # Skip blank lines. |
252 | | - continue |
253 | | - if indent == -1: |
254 | | - indent = pos |
255 | | - if pos >= indent: |
256 | | - unindented_lines.append(line[indent:]) |
257 | | - else: |
258 | | - unindented_lines.append(line) |
259 | | - return "\n".join(unindented_lines) |
| 396 | + return textwrap.dedent(yaml).strip() |
260 | 397 |
|
261 | 398 |
|
262 | 399 | if __name__ == "__main__": |
|
0 commit comments