github.com/rajeev159/opa@v0.45.0/topdown/time_test.go (about)

     1  // Copyright 2020 The OPA Authors.  All rights reserved.
     2  // Use of this source code is governed by an Apache2
     3  // license that can be found in the LICENSE file.
     4  
     5  package topdown
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/open-policy-agent/opa/ast"
    14  )
    15  
    16  func TestTimeSeeding(t *testing.T) {
    17  
    18  	query := `time.now_ns(x)`
    19  	clock := time.Now()
    20  	q := NewQuery(ast.MustParseBody(query)).WithTime(clock).WithCompiler(ast.NewCompiler())
    21  
    22  	ctx := context.Background()
    23  
    24  	qrs, err := q.Run(ctx)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	} else if len(qrs) != 1 {
    28  		t.Fatal("expected exactly one result but got:", qrs)
    29  	}
    30  
    31  	exp := ast.MustParseTerm(fmt.Sprintf(`
    32  		{
    33  			{
    34  				x: %v
    35  			}
    36  		}
    37  	`, clock.UnixNano()))
    38  
    39  	result := queryResultSetToTerm(qrs)
    40  
    41  	if !result.Equal(exp) {
    42  		t.Fatalf("expected %v but got %v", exp, result)
    43  	}
    44  
    45  }