2222
2323from absl .testing import absltest
2424from cel_expr_python import cel
25+ from cel_expr_python .ext import ext_bindings
2526from 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
2629from cel .expr .conformance .proto2 import test_all_types_pb2 as test_all_types_pb
2730
2831
@@ -95,9 +98,7 @@ def test_invalid_yaml(self):
9598 )
9699
97100 def test_config_export_container (self ):
98- env = cel .NewEnv (
99- container = "test.container"
100- )
101+ env = cel .NewEnv (container = "test.container" )
101102 yaml = env .config ().to_yaml ()
102103 self .assertEqual (
103104 normalize_yaml (yaml ),
@@ -251,6 +252,52 @@ def test_config_variable_types(self):
251252 self .assertEqual (res .type (), cel .Type .INT )
252253 self .assertEqual (res .value (), 42 )
253254
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+
254301 def test_config_extensions (self ):
255302 config = cel .NewEnvConfigFromYaml ("""
256303 extensions:
@@ -276,23 +323,47 @@ def test_config_extensions(self):
276323 res = env .compile ("hello('World')" ).eval ()
277324 self .assertEqual (res .value (), "Hello, World!" )
278325
279- def test_config_extensions_override (self ):
280- # TODO(b/498655870): add assertion based on extension aliases once
281- # supported.
326+ def test_config_extension_override_same_version (self ):
282327 config = cel .NewEnvConfigFromYaml ("""
283328 extensions:
284329 - 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
285345 version: 0
286346 - name: cel.lib.ext.strings
347+ version: 2
287348 """ )
288349 with self .assertRaises (Exception ) as e :
289350 cel .NewEnv (
290351 config = config ,
291352 extensions = [ext_math .ExtMath ()],
292353 )
293354 self .assertIn (
294- "Extension 'cel.lib.ext.math' version 0 is already included. Cannot"
295- " also include version 'latest'" ,
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" ,
296367 str (e .exception ),
297368 )
298369
0 commit comments