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

Function TestCostModel

cel/cel_test.go:5737–5774  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

5735}
5736
5737func TestCostModel(t *testing.T) {
5738 model1 := cost.MemberOverload(overloads.StartsWithString,
5739 cost.EvalCost(cost.Scale(cost.Arg(0), 0.1)),
5740 )
5741 model2 := cost.MemberOverload(overloads.EndsWithString,
5742 cost.EvalCost(cost.Scale(cost.Arg(0), 0.1)),
5743 )
5744 env, err := NewEnv(
5745 CostModel(model1),
5746 CostModel(model2),
5747 CostSizingStrategy(cost.AggregateSizingStrategy()),
5748 Variable("str", StringType),
5749 )
5750 if err != nil {
5751 t.Fatalf("NewEnv failed: %v", err)
5752 }
5753 ast, iss := env.Compile(`str.startsWith("prefix") && str.endsWith("suffix")`)
5754 if iss.Err() != nil {
5755 t.Fatalf("Compile failed: %v", iss.Err())
5756 }
5757 prg, err := env.Program(ast, CostTracking(nil))
5758 if err != nil {
5759 t.Fatalf("Program failed: %v", err)
5760 }
5761 res, valDetails, err := prg.Eval(map[string]any{
5762 "str": "prefix_middle_suffix",
5763 })
5764 if err != nil {
5765 t.Fatalf("Eval failed: %v", err)
5766 }
5767 if res != types.True {
5768 t.Errorf("got %v, want true", res)
5769 }
5770 actualCost := *valDetails.ActualCost()
5771 if actualCost == 0 {
5772 t.Errorf("expected non-zero actual cost, got %d", actualCost)
5773 }
5774}
5775
5776// TestCostModelValidation covers rejection of a model whose projection has no runtime counterpart.
5777// Environment construction is the last point at which such a model can be reported.

Callers

nothing calls this directly

Calls 15

CompileMethod · 0.95
ProgramMethod · 0.95
MemberOverloadFunction · 0.92
EvalCostFunction · 0.92
ScaleFunction · 0.92
ArgFunction · 0.92
AggregateSizingStrategyFunction · 0.92
CostModelFunction · 0.85
CostSizingStrategyFunction · 0.85
CostTrackingFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70

Tested by

no test coverage detected