Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set EvalConfig.EvaluationTimestamp to time.Now() if it is empty (zero value), as described in the config comments. #34

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading