Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New creates a fresh SQLite database and connects. This database is created by cloning a database migrated by the provided migrator. It is safe to call concurrently, but running the same migrations across multiple packages at the same time may race. If there is an error creating the database, the test will be immediately failed with testing.TB.Fatalf.
The [Config.Database] field may be left blank, as a new database will be created.
If this methods succeeds, it will call testing.TB.Log with the SQLite URI of the test database, so that you may open the database manually and see what failed.
If this method succeeds and your test succeeds, the database will be removed as part of the test cleanup process.
Example ¶
ExampleNew should be called "TestNew" in your code, but is renamed here for GoDoc.
package main import ( "testing" _ "github.com/mattn/go-sqlite3" "github.com/terinjokes/sqlitestdb" ) func main() { t := &testing.T{} // sqlitestdb is concurrency safe, enjoy yourself, run a lot of tests at once. t.Parallel() // You do not need to provide a database name when calling [New] or [Custom]. conf := sqlitestdb.Config{Driver: "sqlite3"} // You'll want to use a real migrator, this is just an example. migrator := sqlitestdb.NoopMigrator{} db := sqlitestdb.New(t, conf, migrator) // If there was any error creating a template or instance database the // test would have failed with [testing.TB.Fatalf]. var message string err := db.QueryRow("SELECT 'hellorld!'").Scan(&message) if err != nil { t.Fatalf("expected nil error: %+v\n", err) } if message != "hellord!" { t.Fatalf("expected message to be 'hellord!'") } }
Output:
Types ¶
type Config ¶
type Config struct { Driver string // The driver name used in sql.Open(). "sqlite3" (mattn/go-sqlite3), "sqlite" (modernc), or "libsql" (LibSQL) Database string // The path to the database file. }
Config contains the details needed to handle a SQLite database.
func Custom ¶
Custom is like New but after creating the new database instance, it closes any connections and returns the configuration details od the test database, so that you can connect to it explicitly, potentnially via a different SQL interface.
Example ¶
package main import ( "context" "testing" "github.com/jmoiron/sqlx" _ "github.com/mattn/go-sqlite3" "github.com/terinjokes/sqlitestdb" ) func main() { t := &testing.T{} t.Parallel() ctx := context.Background() conf := sqlitestdb.Custom(t, sqlitestdb.Config{Driver: "sqlite3"}, sqlitestdb.NoopMigrator{}) db, err := sqlx.Connect("sqlite3", conf.URI()) if err != nil { t.Fatalf("unexpected error: %+v", err) } defer db.Close() var message string if err = db.GetContext(ctx, &message, "SELECT 'hellord!'"); err != nil { t.Fatalf("unexpected error: %+v", err) } if message != "hellord!" { t.Fatalf("expected message to be 'hellord!'") } }
Output:
func (Config) URI ¶
URI returns a URI string needed to open the SQLite database.
"file:/path/to/database.sql?options=..."
This should be a subset of the URIs defined by SQLite URIs, but may contain driver-specific options.
type NoopMigrator ¶
type NoopMigrator struct{}
NoopMigrator fulfills the Migrator interface, but it does absolutely nothing. You can use this to get empty databases in your tests, or if you're trying out sqlitestdb (hello!) and aren't sure which migrator to use yet.
func (NoopMigrator) Hash ¶
func (m NoopMigrator) Hash() (string, error)
Directories ¶
Path | Synopsis |
---|---|
migrators
|
|
golangmigrator
Module
|
|
once contains helpers for constructing type-safe, concurrency-safe values that are only ever initialized once, and can potentially return an error.
|
once contains helpers for constructing type-safe, concurrency-safe values that are only ever initialized once, and can potentially return an error. |