Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dbfixture.New to Accept the IDB Interface #899

Closed
hirasawayuki opened this issue Sep 18, 2023 · 1 comment · Fixed by #900
Closed

Allow dbfixture.New to Accept the IDB Interface #899

hirasawayuki opened this issue Sep 18, 2023 · 1 comment · Fixed by #900

Comments

@hirasawayuki
Copy link
Contributor

hirasawayuki commented Sep 18, 2023

Description

Currently, the dbfixture.New function only accepts a pointer to bun.DB. However, both bun.DB and bun.TX implement the IDB interface. By allowing dbfixture.New to accept the IDB interface, it would provide more flexibility, especially during testing.

Motivation

By allowing dbfixture.New to accept IDB, we can easily roll back transactions during testing. This would eliminate the need to manually delete test data after the tests have run, ensuring a clean database state and more efficient testing.

Expected Usage
Here's how it could be utilized with the suggest change:

func ExampleTest(t *testing.T) {
	bdb := bun.NewDB(db, mysqldialect.New())
	tx, err := bdb.BeginTx(ctx, &sql.TxOptions{})
	if err != nil {
		t.Fatal(err.Error())
	}
	t.Cleanup(func() { _ = tx.Rollback() })

	fixture := dbfixture.New(tx)
	// ...
}

By making this change, we can leverage the power of transactions during testing, ensuring that our tests are both efficient and clean.

@vmihailenco
Copy link
Member

@hirasawayuki could you send a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants