@@ -65,22 +65,20 @@ def test_execute_with_context_object(self):
6565 def test_execute_same_program_different_contexts (self ):
6666 """Test executing the same program with different contexts."""
6767 program = cel .compile ("price * quantity" )
68-
68+
6969 result1 = program .execute ({"price" : 10 , "quantity" : 5 })
7070 assert result1 == 50
71-
71+
7272 result2 = program .execute ({"price" : 25 , "quantity" : 4 })
7373 assert result2 == 100
74-
74+
7575 result3 = program .execute ({"price" : 100 , "quantity" : 1 })
7676 assert result3 == 100
7777
7878 def test_execute_with_nested_context (self ):
7979 """Test executing with nested dictionary context."""
8080 program = cel .compile ("user.name + ' (' + user.role + ')'" )
81- result = program .execute ({
82- "user" : {"name" : "Bob" , "role" : "admin" }
83- })
81+ result = program .execute ({"user" : {"name" : "Bob" , "role" : "admin" }})
8482 assert result == "Bob (admin)"
8583
8684 def test_execute_with_list_context (self ):
@@ -209,71 +207,75 @@ def test_access_control_policy(self):
209207 policy = cel .compile (
210208 'user.role == "admin" || (resource.owner == user.id && action == "read")'
211209 )
212-
210+
213211 # Admin can do anything
214- assert policy .execute ({
215- "user" : {"id" : "alice" , "role" : "admin" },
216- "resource" : {"owner" : "bob" },
217- "action" : "delete"
218- }) is True
219-
212+ assert (
213+ policy .execute (
214+ {
215+ "user" : {"id" : "alice" , "role" : "admin" },
216+ "resource" : {"owner" : "bob" },
217+ "action" : "delete" ,
218+ }
219+ )
220+ is True
221+ )
222+
220223 # Owner can read their own resource
221- assert policy .execute ({
222- "user" : {"id" : "bob" , "role" : "user" },
223- "resource" : {"owner" : "bob" },
224- "action" : "read"
225- }) is True
226-
224+ assert (
225+ policy .execute (
226+ {
227+ "user" : {"id" : "bob" , "role" : "user" },
228+ "resource" : {"owner" : "bob" },
229+ "action" : "read" ,
230+ }
231+ )
232+ is True
233+ )
234+
227235 # Non-owner cannot read others' resources
228- assert policy .execute ({
229- "user" : {"id" : "charlie" , "role" : "user" },
230- "resource" : {"owner" : "bob" },
231- "action" : "read"
232- }) is False
236+ assert (
237+ policy .execute (
238+ {
239+ "user" : {"id" : "charlie" , "role" : "user" },
240+ "resource" : {"owner" : "bob" },
241+ "action" : "read" ,
242+ }
243+ )
244+ is False
245+ )
233246
234247 def test_pricing_calculation (self ):
235248 """Test pricing calculation with discounts."""
236- pricing = cel .compile (
237- "price * quantity * (1.0 - discount)"
238- )
239-
249+ pricing = cel .compile ("price * quantity * (1.0 - discount)" )
250+
240251 # No discount
241- assert pricing .execute ({
242- "price" : 100.0 , "quantity" : 2.0 , "discount" : 0.0
243- }) == 200.0
244-
252+ assert pricing .execute ({"price" : 100.0 , "quantity" : 2.0 , "discount" : 0.0 }) == 200.0
253+
245254 # 10% discount
246- result = pricing .execute ({
247- "price" : 100.0 , "quantity" : 2.0 , "discount" : 0.1
248- })
255+ result = pricing .execute ({"price" : 100.0 , "quantity" : 2.0 , "discount" : 0.1 })
249256 assert abs (result - 180.0 ) < 0.001
250257
251258 def test_validation_rules (self ):
252259 """Test validation rules."""
253260 age_check = cel .compile ("age >= 18 && age <= 120" )
254-
261+
255262 assert age_check .execute ({"age" : 25 }) is True
256263 assert age_check .execute ({"age" : 17 }) is False
257264 assert age_check .execute ({"age" : 121 }) is False
258265
259266 def test_data_filtering (self ):
260267 """Test data filtering expression."""
261- filter_expr = cel .compile (
262- 'status == "active" && score >= min_score'
263- )
264-
268+ filter_expr = cel .compile ('status == "active" && score >= min_score' )
269+
265270 items = [
266271 {"status" : "active" , "score" : 85 },
267272 {"status" : "inactive" , "score" : 90 },
268273 {"status" : "active" , "score" : 70 },
269274 {"status" : "active" , "score" : 95 },
270275 ]
271-
272- filtered = [
273- item for item in items
274- if filter_expr .execute ({** item , "min_score" : 80 })
275- ]
276-
276+
277+ filtered = [item for item in items if filter_expr .execute ({** item , "min_score" : 80 })]
278+
277279 assert len (filtered ) == 2
278280 assert filtered [0 ]["score" ] == 85
279281 assert filtered [1 ]["score" ] == 95
@@ -286,13 +288,13 @@ def test_compile_once_execute_many(self):
286288 """Demonstrate compile-once-execute-many pattern."""
287289 # Compile the expression once
288290 expr = cel .compile ("x * x + y * y" )
289-
291+
290292 # Execute many times with different values
291293 results = []
292294 for i in range (100 ):
293295 result = expr .execute ({"x" : i , "y" : i + 1 })
294296 results .append (result )
295-
297+
296298 # Verify some results
297299 assert results [0 ] == 0 * 0 + 1 * 1 # 1
298300 assert results [1 ] == 1 * 1 + 2 * 2 # 5
@@ -301,7 +303,7 @@ def test_compile_once_execute_many(self):
301303 def test_reuse_compiled_program (self ):
302304 """Test that compiled programs can be reused safely."""
303305 program = cel .compile ("value > threshold" )
304-
306+
305307 # Multiple sequential executions
306308 assert program .execute ({"value" : 10 , "threshold" : 5 }) is True
307309 assert program .execute ({"value" : 3 , "threshold" : 5 }) is False
0 commit comments