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

     1  package datastore_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/authzed/spicedb/internal/datastore/crdb"
    11  	"github.com/authzed/spicedb/internal/datastore/mysql"
    12  	"github.com/authzed/spicedb/internal/datastore/postgres"
    13  	"github.com/authzed/spicedb/internal/datastore/spanner"
    14  	"github.com/authzed/spicedb/pkg/datastore"
    15  )
    16  
    17  func createEngine(engineID string, uri string) error {
    18  	ctx := context.Background()
    19  
    20  	switch engineID {
    21  	case "postgres":
    22  		_, err := postgres.NewPostgresDatastore(ctx, uri)
    23  		return err
    24  
    25  	case "mysql":
    26  		_, err := mysql.NewMySQLDatastore(ctx, uri)
    27  		return err
    28  
    29  	case "spanner":
    30  		_, err := spanner.NewSpannerDatastore(ctx, uri)
    31  		return err
    32  
    33  	case "cockroachdb":
    34  		_, err := crdb.NewCRDBDatastore(ctx, uri)
    35  		return err
    36  
    37  	default:
    38  		panic(fmt.Sprintf("missing create implementation for engine %s", engineID))
    39  	}
    40  }
    41  
    42  func TestDatastoreURIErrors(t *testing.T) {
    43  	tcs := map[string]string{
    44  		"some-wrong-uri":                                  "wrong",
    45  		"postgres://foo:bar:baz@someurl":                  "bar",
    46  		"postgres://spicedb:somepassword":                 "somepassword",
    47  		"postgres://spicedb:somepassword#@foo":            "somepassword",
    48  		"username=foo password=somepassword dsn=whatever": "somepassword",
    49  	}
    50  
    51  	for _, engineID := range datastore.Engines {
    52  		t.Run(engineID, func(t *testing.T) {
    53  			for tc, check := range tcs {
    54  				t.Run(tc, func(t *testing.T) {
    55  					err := createEngine(engineID, tc)
    56  					require.Error(t, err)
    57  					require.NotContains(t, err.Error(), check)
    58  				})
    59  			}
    60  		})
    61  	}
    62  }