corked

module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2021 License: MIT

README ¶

corked

Useful helpers for testing based on testcontainers-go

🔨 How to use

Postgres

To create a simple container with a database, just use this snippet:

c, teardown, err := New(ContainerRequest{})
if err != nil {
	t.Fatal(err)
}

defer teardown()

This creates a database based on the image postgres:12.4-alpine with a database "postgres", the user "postgres" and the password "password". Now you can easily get a DSN and start writing tests.

db, err := sql.Open("postgres", c.DSN())
if err != nil {
	t.Fatal(err)
}

rows, err := db.Query(...)
if err != nil {
    t.Fatal(err)
}

defer rows.Close()

for rows.Next() {
    ...
}

What database can do without migrations? You can simply create a database with migrations:

c, teardown, err := New(ContainerRequest{
	InitScripts: InitScripts{
		Inline: `
			begin;
			/* Your sql is here. */
			commit;
		`,
	},
})
if err != nil {
	t.Fatal(err)
}

defer teardown()

Migrations can also be from multiple files InitScripts.FromFiles or even from a directory InitScripts.FromDir. Migration priorities: Inline > FromFiles > FromDir.

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL