uptrace / bun

SQL-first Golang ORM
https://bun.uptrace.dev
BSD 2-Clause "Simplified" License
3.47k stars 210 forks source link

Allow dbfixture.New to Accept the IDB Interface #899

Closed hirasawayuki closed 9 months ago

hirasawayuki commented 9 months ago

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 commented 9 months ago

@hirasawayuki could you send a PR?