nikgalushko / corked

Useful helpers for testing based on testcontainers-go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

Useful helpers for testing based on testcontainers-go

License:MIT License


Languages

Language:Go 100.0%