Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hashicorp/dbassert
Go database assert package
https://github.com/hashicorp/dbassert
db golang testing
Last synced: about 1 month ago
JSON representation
Go database assert package
- Host: GitHub
- URL: https://github.com/hashicorp/dbassert
- Owner: hashicorp
- License: mpl-2.0
- Created: 2020-05-30T20:38:42.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-30T15:39:29.000Z (about 1 year ago)
- Last Synced: 2024-09-28T12:42:09.160Z (about 1 month ago)
- Topics: db, golang, testing
- Language: Go
- Homepage:
- Size: 75.2 KB
- Stars: 16
- Watchers: 11
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - hashicorp/dbassert - Go database assert package (Go)
README
# dbassert
The `dbassert` package provides some helpful functions to help you write better
tests when writing Go database applications. The package supports both sql.DB
and Gorm assertions.### Example sql.DB asserts usage:
```go
package your_brilliant_pkgimport (
"testing"
dbassert "github.com/hashicorp/dbassert"
)func TestSomeDb(t *testing.T) {
conn, err := sql.Open("postgres", "postgres://postgres:secret@localhost:%s?sslmode=disable")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
dbassert := dbassert.New(t, conn, "postgres")
// assert that the db column is nullable
dbassert.Nullable("some_table", "some_column")// assert that the db column is a particular domain type
dbassert.Domain("test_table_dbasserts", "public_id", "dbasserts_public_id")}
```
### Example Gorm asserts usage:```go
package your_brilliant_pkgimport (
"testing"
dbassert "github.com/hashicorp/dbassert/gorm"
)func TestSomeGormModel(t *testing.T) {
conn, err := sql.Open("postgres", "postgres://postgres:secret@localhost:%s?sslmode=disable")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
db, err := gorm.Open("postgres", conn)
m := testModel{}
if err = db.Create(&m).Error; err != nil {
t.Fatal(err)
}
dbassert := dbassert.New(t, conn, "postgres")
// assert that the db field is null
dbassert.IsNull(&someModel, "SomeField")// assert that the db field is not null
dbassert.NotNull(&someModel, "SomeField")// assert that the db field nullable
dbassert.Nullable(&someModel, "SomeField")// assert that the db field is a particular domain type
dbassert.Domain(&someModel, "SomeField", "some_domain_type")
}
```