Skip to content

Commit

Permalink
Set EvalConfig.EvaluationTimestamp to time.Now() if it is empty (zero…
Browse files Browse the repository at this point in the history
… value), as described in the config comments.

PiperOrigin-RevId: 647481250
  • Loading branch information
suyashkumar authored and copybara-github committed Jun 27, 2024
1 parent 451661e commit 02c8251
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cql.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ type EvalConfig struct {
// should not be called from multiple goroutines on a single *ELM.
// Errors returned by Eval will always be a result.EngineError.
func (e *ELM) Eval(ctx context.Context, retriever retriever.Retriever, config EvalConfig) (result.Libraries, error) {
evalTS := config.EvaluationTimestamp
if config.EvaluationTimestamp.IsZero() {
evalTS = time.Now()
}
c := interpreter.Config{
DataModels: e.dataModels,
Parameters: e.parsedParams,
Retriever: retriever,
Terminology: config.Terminology,
EvaluationTimestamp: config.EvaluationTimestamp,
EvaluationTimestamp: evalTS,
ReturnPrivateDefs: config.ReturnPrivateDefs,
}

Expand Down
35 changes: 35 additions & 0 deletions cql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"testing"
"time"

"github.com/google/cql"
"github.com/google/cql/model"
Expand Down Expand Up @@ -115,6 +116,40 @@ func TestCQL(t *testing.T) {
}
}

func TestCQL_EvalEmptyTimestamp(t *testing.T) {
// This test checks that an empty EvalConfig.EvaluationTimestamp is set to time.Now() at the start
// of the eval request. We do this by sanity checking the result of Now() is within 2 mins of the
// test assertion time.
cqlSources := []string{dedent.Dedent(`
library TESTLIB version '1.0.0'
define TESTRESULT: Now()`)}
parserConfig := cql.ParseConfig{}
evalConfig := cql.EvalConfig{}

elm, err := cql.Parse(context.Background(), cqlSources, parserConfig)
if err != nil {
t.Fatalf("Parse returned unexpected error: %v", err)
}

results, err := elm.Eval(context.Background(), nil, evalConfig)
if err != nil {
t.Fatalf("Eval returned unexpected error: %v", err)
}

gotResult := getTESTRESULTWithSources(t, results)
dt, err := result.ToDateTime(gotResult)
if err != nil {
t.Errorf("returned result is not a DateTime, err: %v", err)
}

// Check that the dt is within [time.Now()-2min, time.Now()]
end := time.Now()
start := end.Add(-2 * time.Minute)
if dt.Date.Before(start) || dt.Date.After(end) {
t.Errorf("returned CQL Now() result is not within 2 mins of time.Now()")
}
}

func TestCQL_ParseErrors(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 02c8251

Please sign in to comment.