github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/cmd/datastore/datastore_test.go (about)

     1  package datastore
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/spf13/pflag"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestDefaults(t *testing.T) {
    13  	f := pflag.FlagSet{}
    14  	expected := Config{}
    15  	err := RegisterDatastoreFlagsWithPrefix(&f, "", &expected)
    16  	require.NoError(t, err)
    17  	received := DefaultDatastoreConfig()
    18  	require.Equal(t, expected, *received)
    19  }
    20  
    21  func TestLoadDatastoreFromFileContents(t *testing.T) {
    22  	ctx := context.Background()
    23  	ds, err := NewDatastore(ctx,
    24  		SetBootstrapFileContents(map[string][]byte{"test": []byte("schema: definition user{}")}),
    25  		WithEngine(MemoryEngine))
    26  	require.NoError(t, err)
    27  
    28  	revision, err := ds.HeadRevision(ctx)
    29  	require.NoError(t, err)
    30  
    31  	namespaces, err := ds.SnapshotReader(revision).ListAllNamespaces(ctx)
    32  	require.NoError(t, err)
    33  	require.Len(t, namespaces, 1)
    34  	require.Equal(t, "user", namespaces[0].Definition.Name)
    35  }
    36  
    37  func TestLoadDatastoreFromFile(t *testing.T) {
    38  	file, err := os.CreateTemp("", "")
    39  	require.NoError(t, err)
    40  	_, err = file.Write([]byte("schema: definition user{}"))
    41  	require.NoError(t, err)
    42  
    43  	ctx := context.Background()
    44  	ds, err := NewDatastore(ctx,
    45  		SetBootstrapFiles([]string{file.Name()}),
    46  		WithEngine(MemoryEngine))
    47  	require.NoError(t, err)
    48  
    49  	revision, err := ds.HeadRevision(ctx)
    50  	require.NoError(t, err)
    51  
    52  	namespaces, err := ds.SnapshotReader(revision).ListAllNamespaces(ctx)
    53  	require.NoError(t, err)
    54  	require.Len(t, namespaces, 1)
    55  	require.Equal(t, "user", namespaces[0].Definition.Name)
    56  }
    57  
    58  func TestLoadDatastoreFromFileAndContents(t *testing.T) {
    59  	file, err := os.CreateTemp("", "")
    60  	require.NoError(t, err)
    61  	_, err = file.Write([]byte("schema: definition repository{}"))
    62  	require.NoError(t, err)
    63  
    64  	ctx := context.Background()
    65  	ds, err := NewDatastore(ctx,
    66  		SetBootstrapFiles([]string{file.Name()}),
    67  		SetBootstrapFileContents(map[string][]byte{"test": []byte("schema: definition user{}")}),
    68  		WithEngine(MemoryEngine))
    69  	require.NoError(t, err)
    70  
    71  	revision, err := ds.HeadRevision(ctx)
    72  	require.NoError(t, err)
    73  
    74  	namespaces, err := ds.SnapshotReader(revision).ListAllNamespaces(ctx)
    75  	require.NoError(t, err)
    76  	require.Len(t, namespaces, 2)
    77  	namespaceNames := []string{namespaces[0].Definition.Name, namespaces[1].Definition.Name}
    78  	require.Contains(t, namespaceNames, "user")
    79  	require.Contains(t, namespaceNames, "repository")
    80  }