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

Unit testing service logic when using - *DB.RunInTx func #587

Closed
GuyKomari opened this issue Jun 20, 2022 · 3 comments
Closed

Unit testing service logic when using - *DB.RunInTx func #587

GuyKomari opened this issue Jun 20, 2022 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@GuyKomari
Copy link

GuyKomari commented Jun 20, 2022

I have a service with *bun.DB instance and a method that handles a transaction using RunInTx func.
I would like to add a unit test for this method but in order to do that, I need to use an abstraction over *bun.DB
which seems to be - bun.IDB interface.
However, bun.IDB is missing RunInTx func and other "transaction-related" methods like - BeginTx, Begin, etc.

simple code example:

type MyService struct {
	db            *bun.DB
	myRepo        MyRepo
}

func (s *MyService) DoSmt(ctx context.Context) error {
	err := s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
	        // Get for update
                // Some logic
                // Update
               
               return nil
	})
	
        return err
}

type MyRepo interface { 
     Get(ctx context.Context, db bun.IDB) interface{}, error
     Update(ctx context.Context, db bun.IDB, interface{}) interface{}, error
}

Currently, to overcome this issue, I have created this wrapper that can be used instead of *bun.DB:

type DbWrapper interface {
	bun.IDB
	RunInTx(ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context, tx bun.Tx) error) error
}

However, it would be nice if it was part of bun.DBI.
Would it be possible to add the "transaction-related" methods to bun.DBI (same as in go-pg)?
Is there a better approach to handle this case?

TIA, Guy

@GuyKomari GuyKomari changed the title unit testing using service when using - *DB.RunInTx func unit testing service logic when using - *DB.RunInTx func Jun 20, 2022
@GuyKomari GuyKomari changed the title unit testing service logic when using - *DB.RunInTx func Unit testing service logic when using - *DB.RunInTx func Jun 20, 2022
@vmihailenco
Copy link
Member

IDB is a common interface for *bun.DB, bun.Conn, and bun.Tx. I don't think we can extend it with Begin and RunInTx.

But we probably can add another interface, for example, ITx in addition to IConn. Then we can probably create ITxDB in addition to IDB. Not sure if it is worth it and makes your task much easier...

@isgj
Copy link
Contributor

isgj commented Jun 25, 2022

Some other ORMs offer the same interface also for the transaction, and when you are already in a tx they use SAVEPOINT.

The bun.Tx can be extended with Begin, RunInTx.. which will work on save-points. In this case the interface IDB can be extended and other than solving this issue it will make it possible for bun.DB and bun.Tx to be used interchangeably (helpful in some scenarios).

@vmihailenco
Copy link
Member

@isgj yes, that is an option too. If there is any interest, please send a PR.

@vmihailenco vmihailenco added enhancement New feature or request help wanted Extra attention is needed labels Jun 25, 2022
isgj added a commit to isgj/bun that referenced this issue Jun 26, 2022
isgj added a commit to isgj/bun that referenced this issue Jun 28, 2022
isgj added a commit to isgj/bun that referenced this issue Jun 28, 2022
isgj added a commit to isgj/bun that referenced this issue Jul 1, 2022
isgj added a commit to isgj/bun that referenced this issue Jul 1, 2022
isgj added a commit to isgj/bun that referenced this issue Jul 1, 2022
vmihailenco added a commit that referenced this issue Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants