@@ -313,6 +313,180 @@ def test_decode_python_template_delegates_to_lazy_decoder(self):
313313 assert op .decode_python_template (encoded ) == "payload"
314314
315315
316+ # The shape the backend gives a Python-generated operator's code inside a
317+ # control block: where a text property holds `$name`, the rendered
318+ # `self.decode_python_template('<base64 of "$name">')` is replaced by
319+ # `self.loop_variable_text('name')`. Mirrors the sklearn trainers'
320+ # `<name> = <type> (<value>),` hyperparameter rendering, including the lambda
321+ # the SVC trainer emits as its boolean converter.
322+ _GENERATED_TRAINER_TEMPLATE = """from pytexera import *
323+
324+ class ProcessTableOperator(UDFTableOperator):
325+
326+ @overrides
327+ def process_table(self, table: Table, port: int) -> Iterator[Optional[TableLike]]:
328+ yield dict(C = float (self.loop_variable_text('c')),kernel = str (self.loop_variable_text('kernel')),probability = (lambda value: value.lower() == "true") (self.loop_variable_text('p')),)
329+ """
330+
331+
332+ class TestLoopVariableText :
333+ def test_returns_the_text_of_an_int_a_float_and_a_string (self ):
334+ op = _ConcreteOperator ()
335+ op .register_loop_state (State ({"n" : 3 , "rate" : 0.5 , "ticker" : "AAPL" }))
336+ assert op .loop_variable_text ("n" ) == "3"
337+ assert op .loop_variable_text ("rate" ) == "0.5"
338+ assert op .loop_variable_text ("ticker" ) == "AAPL"
339+
340+ def test_spells_a_boolean_as_the_jvm_binds_one (self ):
341+ # A JVM operator's text property takes a boolean as true / false
342+ # (LateBoundExecutor), and so does this, not Python's True / False.
343+ op = _ConcreteOperator ()
344+ op .register_loop_state (State ({"on" : True , "off" : False }))
345+ assert op .loop_variable_text ("on" ) == "true"
346+ assert op .loop_variable_text ("off" ) == "false"
347+
348+ def test_a_falsy_value_is_still_carried (self ):
349+ # Zero, False and the empty string are values the state carried, not
350+ # absent ones: none of them may fall into the missing-name error.
351+ op = _ConcreteOperator ()
352+ op .register_loop_state (State ({"n" : 0 , "flag" : False , "note" : "" }))
353+ assert op .loop_variable_text ("n" ) == "0"
354+ assert op .loop_variable_text ("flag" ) == "false"
355+ assert op .loop_variable_text ("note" ) == ""
356+
357+ @pytest .mark .parametrize ("value" , [None , [1 , 2 ], {"p" : 2 }, b"raw" ], ids = repr )
358+ def test_raises_for_a_value_that_is_not_a_scalar (self , value ):
359+ # The JVM rejects a non-scalar for a text property too, rather than
360+ # pass on a spelling of its own, such as Python's repr.
361+ op = _ConcreteOperator ()
362+ op .register_loop_state (State ({"m" : value }))
363+ with pytest .raises (RuntimeError ) as excinfo :
364+ op .loop_variable_text ("m" )
365+ assert str (excinfo .value ) == (
366+ f"the operator refers to loop variable $m, but its value { value !r} "
367+ "is not a scalar"
368+ )
369+
370+ def test_a_value_that_looks_like_a_reference_is_returned_verbatim (self ):
371+ # The text is the value itself; it is never resolved again.
372+ op = _ConcreteOperator ()
373+ op .register_loop_state (State ({"ticker" : "$AAPL" , "AAPL" : "not me" }))
374+ assert op .loop_variable_text ("ticker" ) == "$AAPL"
375+
376+ def test_a_later_state_message_replaces_a_value_it_carries (self ):
377+ # Every iteration delivers a fresh state message; the text is read
378+ # from the latest one that carried the name, never cached.
379+ op = _ConcreteOperator ()
380+ op .register_loop_state (State ({"i" : 1 }))
381+ assert op .loop_variable_text ("i" ) == "1"
382+ op .register_loop_state (State ({"i" : 2 }))
383+ assert op .loop_variable_text ("i" ) == "2"
384+
385+ def test_a_later_state_message_without_the_name_keeps_its_value (self ):
386+ # A loop body operator's own state (here a UDF publishing centroids)
387+ # reaches the operator after the loop's: it must not hide `i`.
388+ op = _ConcreteOperator ()
389+ op .register_loop_state (State ({"i" : 1 }))
390+ op .register_loop_state (State ({"centroids" : 3 }))
391+ assert op .loop_variable_text ("i" ) == "1"
392+ assert op .loop_variable_text ("centroids" ) == "3"
393+
394+ def test_an_inner_loops_state_shadows_the_outer_one_and_keeps_the_rest (self ):
395+ # A nested loop's body receives the outer loop's state first, then the
396+ # inner Loop Start's, as a JVM operator does.
397+ op = _ConcreteOperator ()
398+ op .register_loop_state (State ({"j" : 5 , "i" : "outer" }))
399+ op .register_loop_state (State ({"i" : 0 }))
400+ assert op .loop_variable_text ("j" ) == "5"
401+ assert op .loop_variable_text ("i" ) == "0"
402+
403+ def test_registering_makes_the_message_loop_state_and_changes_no_message (self ):
404+ op = _ConcreteOperator ()
405+ first , second = State ({"i" : 1 }), State ({"j" : 2 })
406+ op .register_loop_state (first )
407+ op .register_loop_state (second )
408+ assert op .loop_state is second
409+ # The merge is the operator's own: neither message gains a key.
410+ assert first == State ({"i" : 1 })
411+ assert second == State ({"j" : 2 })
412+
413+ def test_registering_reaches_the_one_operator_only (self ):
414+ # The defaults live on the class, so a registration written there
415+ # would reach every operator.
416+ target , bystander = _ConcreteOperator (), _ConcreteOperator ()
417+ target .register_loop_state (State ({"i" : 1 }))
418+ assert target .loop_variable_text ("i" ) == "1"
419+ with pytest .raises (RuntimeError , match = "read before the iteration's state" ):
420+ bystander .loop_variable_text ("i" )
421+ assert bystander .loop_state is None
422+ assert _ConcreteOperator .loop_state is None
423+
424+ def test_raises_when_read_before_the_iterations_state_arrived (self ):
425+ op = _ConcreteOperator ()
426+ with pytest .raises (RuntimeError ) as excinfo :
427+ op .loop_variable_text ("i" )
428+ assert str (excinfo .value ) == (
429+ "loop variable $i is read before the iteration's state arrived"
430+ )
431+
432+ def test_raises_for_a_name_no_state_message_carried (self ):
433+ op = _ConcreteOperator ()
434+ op .register_loop_state (State ({"i" : 1 }))
435+ op .register_loop_state (State ({"k" : 2 }))
436+ with pytest .raises (RuntimeError ) as excinfo :
437+ op .loop_variable_text ("j" )
438+ assert str (excinfo .value ) == (
439+ "the operator refers to loop variable $j, but no state message carried it"
440+ )
441+
442+ def test_an_empty_state_has_arrived_but_carries_no_name (self ):
443+ # An empty state is a state that arrived: the missing-name error, not
444+ # the not-yet-arrived one.
445+ op = _ConcreteOperator ()
446+ op .register_loop_state (State ())
447+ with pytest .raises (RuntimeError , match = "no state message carried it" ):
448+ op .loop_variable_text ("i" )
449+
450+ def test_a_prefix_of_a_declared_name_is_not_matched (self ):
451+ op = _ConcreteOperator ()
452+ op .register_loop_state (State ({"items" : 3 }))
453+ with pytest .raises (RuntimeError , match = r"\$item, but no state message" ):
454+ op .loop_variable_text ("item" )
455+
456+ def test_an_extension_of_a_declared_name_is_not_matched (self ):
457+ op = _ConcreteOperator ()
458+ op .register_loop_state (State ({"item" : 3 }))
459+ with pytest .raises (RuntimeError , match = r"\$items, but no state message" ):
460+ op .loop_variable_text ("items" )
461+
462+ def test_names_are_case_sensitive (self ):
463+ op = _ConcreteOperator ()
464+ op .register_loop_state (State ({"i" : 1 }))
465+ with pytest .raises (RuntimeError , match = r"\$I, but no state message" ):
466+ op .loop_variable_text ("I" )
467+
468+ def test_generated_code_reads_the_iterations_value_at_run_time (self ):
469+ # __name__ so the exec'd class's methods get a real __module__ (the
470+ # @overrides decorator on the generated methods inspects it).
471+ namespace : dict = {"__name__" : "generated_trainer_operator" }
472+ exec (_GENERATED_TRAINER_TEMPLATE , namespace )
473+ op = namespace ["ProcessTableOperator" ]()
474+
475+ # Before the iteration's state arrives, the generated call fails loud
476+ # instead of converting a stand-in value.
477+ with pytest .raises (RuntimeError , match = "read before the iteration's state" ):
478+ list (op .process_table (Table ([Tuple ({"a" : 1 })]), 0 ))
479+
480+ op .register_loop_state (State ({"c" : 2 , "kernel" : "rbf" , "p" : True }))
481+ (params ,) = list (op .process_table (Table ([Tuple ({"a" : 1 })]), 0 ))
482+ assert params == {"C" : 2.0 , "kernel" : "rbf" , "probability" : True }
483+
484+ # The next iteration's state reaches the same operator instance.
485+ op .register_loop_state (State ({"c" : 0.25 , "kernel" : "linear" , "p" : False }))
486+ (params ,) = list (op .process_table (Table ([Tuple ({"a" : 1 })]), 0 ))
487+ assert params == {"C" : 0.25 , "kernel" : "linear" , "probability" : False }
488+
489+
316490class TestBatchOperatorValidation :
317491 def test_validate_batch_size_rejects_none (self ):
318492 with pytest .raises (ValueError , match = "cannot be None" ):
0 commit comments