github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/spanner/caveat_test.go (about)

     1  package spanner
     2  
     3  import (
     4  	"testing"
     5  
     6  	"cloud.google.com/go/spanner"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestContextualizedCaveatFrom(t *testing.T) {
    11  	res, err := ContextualizedCaveatFrom(spanner.NullString{StringVal: "", Valid: false}, spanner.NullJSON{Value: nil, Valid: true})
    12  	require.NoError(t, err)
    13  	require.Nil(t, res)
    14  
    15  	res, err = ContextualizedCaveatFrom(spanner.NullString{StringVal: "", Valid: true}, spanner.NullJSON{Value: nil, Valid: false})
    16  	require.NoError(t, err)
    17  	require.Nil(t, res)
    18  
    19  	res, err = ContextualizedCaveatFrom(spanner.NullString{StringVal: "", Valid: true}, spanner.NullJSON{Value: nil, Valid: true})
    20  	require.NoError(t, err)
    21  	require.Nil(t, res)
    22  
    23  	res, err = ContextualizedCaveatFrom(spanner.NullString{StringVal: "test", Valid: true}, spanner.NullJSON{Value: nil, Valid: true})
    24  	require.NoError(t, err)
    25  	require.NotNil(t, res)
    26  	require.Equal(t, "test", res.CaveatName)
    27  	require.NotNil(t, res.Context)
    28  	require.Empty(t, res.Context.Fields)
    29  
    30  	res, err = ContextualizedCaveatFrom(
    31  		spanner.NullString{StringVal: "test", Valid: true},
    32  		spanner.NullJSON{Value: map[string]any{"key": "val"}, Valid: true})
    33  	require.NoError(t, err)
    34  	require.NotNil(t, res)
    35  	require.Equal(t, "test", res.CaveatName)
    36  	require.NotNil(t, res.Context)
    37  	require.Len(t, res.Context.Fields, 1)
    38  	require.Equal(t, "val", res.Context.Fields["key"].GetStringValue())
    39  }