github.com/openfga/openfga@v1.5.4-rc1/pkg/testfixtures/storage/storage.go (about)

     1  // Package storage contains containers that can be used to test all available data stores.
     2  package storage
     3  
     4  import (
     5  	"testing"
     6  )
     7  
     8  // DatastoreTestContainer represents a runnable container for testing specific datastore engines.
     9  type DatastoreTestContainer interface {
    10  
    11  	// GetConnectionURI returns a connection string to the datastore instance running inside
    12  	// the container.
    13  	GetConnectionURI(includeCredentials bool) string
    14  
    15  	// GetDatabaseSchemaVersion returns the last migration applied (e.g. 3) when the container was created
    16  	GetDatabaseSchemaVersion() int64
    17  
    18  	GetUsername() string
    19  	GetPassword() string
    20  }
    21  
    22  type memoryTestContainer struct{}
    23  
    24  func (m memoryTestContainer) GetConnectionURI(includeCredentials bool) string {
    25  	return ""
    26  }
    27  
    28  func (m memoryTestContainer) GetUsername() string {
    29  	return ""
    30  }
    31  
    32  func (m memoryTestContainer) GetPassword() string {
    33  	return ""
    34  }
    35  
    36  func (m memoryTestContainer) GetDatabaseSchemaVersion() int64 {
    37  	return 1
    38  }
    39  
    40  // RunDatastoreTestContainer constructs and runs a specific DatastoreTestContainer for the provided
    41  // datastore engine. If applicable, it also runs all existing database migrations.
    42  // The resources used by the test engine will be cleaned up after the test has finished.
    43  func RunDatastoreTestContainer(t testing.TB, engine string) DatastoreTestContainer {
    44  	switch engine {
    45  	case "mysql":
    46  		return NewMySQLTestContainer().RunMySQLTestContainer(t)
    47  	case "postgres":
    48  		return NewPostgresTestContainer().RunPostgresTestContainer(t)
    49  	case "memory":
    50  		return memoryTestContainer{}
    51  	default:
    52  		t.Fatalf("unsupported datastore engine: %q", engine)
    53  		return nil
    54  	}
    55  }