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

Function TestCostModelVersionPinsTracking

cel/cost_test.go:742–786  ·  view source on GitHub ↗

TestCostModelVersionPinsTracking checks that CostModelVersion applies to runtime tracking as well as estimation, so that a pinned environment's charge is computed by the same rules as its estimate.

(t *testing.T)

Source from the content-addressed store, hash-verified

740// as estimation, so that a pinned environment's charge is computed by the same rules as its
741// estimate.
742func TestCostModelVersionPinsTracking(t *testing.T) {
743 expr := `'abcdefghijk'.contains('abcdefghijk')`
744 tests := []struct {
745 name string
746 opts []EnvOption
747 wantEst cost.CostEstimate
748 want uint64
749 }{
750 // contains costs ⌈0.1·11⌉ · ⌈0.1·11⌉ = 4 at version 0, and ⌈0.01·11·11⌉ = 2 at version 1.
751 {name: "version 0", opts: []EnvOption{CostModelVersion(0)}, wantEst: cost.FixedCostEstimate(4), want: 4},
752 {name: "version 1", opts: []EnvOption{CostModelVersion(1)}, wantEst: cost.FixedCostEstimate(2), want: 2},
753 {name: "latest", wantEst: cost.FixedCostEstimate(2), want: 2},
754 }
755 for _, tc := range tests {
756 tc := tc
757 t.Run(tc.name, func(t *testing.T) {
758 env, err := NewEnv(tc.opts...)
759 if err != nil {
760 t.Fatalf("NewEnv() failed: %v", err)
761 }
762 ast, iss := env.Compile(expr)
763 if iss.Err() != nil {
764 t.Fatalf("Compile(%q) failed: %v", expr, iss.Err())
765 }
766 est, err := env.EstimateCost(ast, testCostEstimator{})
767 if err != nil {
768 t.Fatalf("EstimateCost() failed: %v", err)
769 }
770 if est != tc.wantEst {
771 t.Errorf("EstimateCost() = %v, want %v", est, tc.wantEst)
772 }
773 prg, err := env.Program(ast, CostTracking(nil))
774 if err != nil {
775 t.Fatalf("Program() failed: %v", err)
776 }
777 _, det, err := prg.Eval(NoVars())
778 if err != nil {
779 t.Fatalf("Eval() failed: %v", err)
780 }
781 if got := *det.ActualCost(); got != tc.want {
782 t.Errorf("ActualCost() = %d, want %d", got, tc.want)
783 }
784 })
785 }
786}
787
788// TestSinceModelVersionHandoff checks that a model with BeforeVersion replaces an overload's
789// earlier estimator and tracker from that revision on, in both estimation and tracking, while

Callers

nothing calls this directly

Calls 11

CompileMethod · 0.95
EstimateCostMethod · 0.95
ProgramMethod · 0.95
FixedCostEstimateFunction · 0.92
CostModelVersionFunction · 0.85
CostTrackingFunction · 0.85
NoVarsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
EvalMethod · 0.65
ActualCostMethod · 0.45

Tested by

no test coverage detected