MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestSinceModelVersionHandoff

Function TestSinceModelVersionHandoff

cel/cost_test.go:792–850  ·  view source on GitHub ↗

TestSinceModelVersionHandoff checks that a model with BeforeVersion replaces an overload's earlier estimator and tracker from that revision on, in both estimation and tracking, while environments pinned to an earlier revision keep the earlier ones, and that a caller's tracker still overrides both.

(t *testing.T)

Source from the content-addressed store, hash-verified

790// environments pinned to an earlier revision keep the earlier ones, and that a caller's tracker
791// still overrides both.
792func TestSinceModelVersionHandoff(t *testing.T) {
793 legacyCost := uint64(100)
794 overrideCost := uint64(999)
795 lib := func() EnvOption {
796 return Lib(sinceVersionLib{legacyCost: &legacyCost})
797 }
798 tests := []struct {
799 name string
800 opts []EnvOption
801 progOpts []ProgramOption
802 wantEst cost.CostEstimate
803 want uint64
804 }{
805 // The literal costs 0; the call costs 100 under the hand-written pair, 15 under the v1
806 // model, and 7 under the v2+ model.
807 {name: "pinned before the model", opts: []EnvOption{CostModelVersion(0)}, wantEst: cost.FixedCostEstimate(100), want: 100},
808 {name: "pinned at v1 model", opts: []EnvOption{CostModelVersion(1)}, wantEst: cost.FixedCostEstimate(15), want: 15},
809 {name: "pinned at v2 model", opts: []EnvOption{CostModelVersion(2)}, wantEst: cost.FixedCostEstimate(7), want: 7},
810 {name: "latest", wantEst: cost.FixedCostEstimate(7), want: 7},
811 {
812 name: "caller override",
813 progOpts: []ProgramOption{CostTrackerOptions(cost.OverloadTracker("handoff_string",
814 func([]ref.Val, ref.Val) *uint64 { return &overrideCost }))},
815 wantEst: cost.FixedCostEstimate(7),
816 want: 999,
817 },
818 }
819 for _, tc := range tests {
820 tc := tc
821 t.Run(tc.name, func(t *testing.T) {
822 env, err := NewEnv(append([]EnvOption{lib()}, tc.opts...)...)
823 if err != nil {
824 t.Fatalf("NewEnv() failed: %v", err)
825 }
826 ast, iss := env.Compile(`handoff('abc')`)
827 if iss.Err() != nil {
828 t.Fatalf("Compile() failed: %v", iss.Err())
829 }
830 est, err := env.EstimateCost(ast, testCostEstimator{})
831 if err != nil {
832 t.Fatalf("EstimateCost() failed: %v", err)
833 }
834 if est != tc.wantEst {
835 t.Errorf("EstimateCost() = %v, want %v", est, tc.wantEst)
836 }
837 prg, err := env.Program(ast, append([]ProgramOption{CostTracking(nil)}, tc.progOpts...)...)
838 if err != nil {
839 t.Fatalf("Program() failed: %v", err)
840 }
841 _, det, err := prg.Eval(NoVars())
842 if err != nil {
843 t.Fatalf("Eval() failed: %v", err)
844 }
845 if got := *det.ActualCost(); got != tc.want {
846 t.Errorf("ActualCost() = %d, want %d", got, tc.want)
847 }
848 })
849 }

Callers

nothing calls this directly

Calls 14

CompileMethod · 0.95
EstimateCostMethod · 0.95
ProgramMethod · 0.95
FixedCostEstimateFunction · 0.92
OverloadTrackerFunction · 0.92
LibFunction · 0.85
CostModelVersionFunction · 0.85
CostTrackerOptionsFunction · 0.85
CostTrackingFunction · 0.85
NoVarsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70

Tested by

no test coverage detected