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

Mocking sqlx pkg for testing #204

Closed
sim4life opened this issue Feb 17, 2016 · 8 comments
Closed

Mocking sqlx pkg for testing #204

sim4life opened this issue Feb 17, 2016 · 8 comments

Comments

@sim4life
Copy link

Is there a straightforward way to mock sqlx package for unit testing?
I'm thinking of creating a mock wrapper of sqlx functions and call Data-Dog sqlmock pkg from those functions.
Is there a better approach?

@sim4life sim4life changed the title Mocking sqlx pkg Mocking sqlx pkg for testing Feb 17, 2016
@jmoiron
Copy link
Owner

jmoiron commented Feb 17, 2016

It should be trivial to mock sqlx.DB; in fact, internally, a lot of functionality which is shared between Stmt, Tx and DB use interfaces to reduce code duplication.

I'm not sure how how you'd mock other sql functions; presumably you'd have to resort to DI.

@sim4life
Copy link
Author

Yes, it was pretty straightforward.

mockDB, mock, err := sqlmock.New()
defer mockDB.Close()
sqlxDB = sqlx.NewDb(mockDB,"sqlmock")

Later on I used the sqlmock function:

mock.ExpectExec("INSERT INTO baskets").WillReturnResult(sqlmock.NewResult(newID, 1))

for the sqlx query:

sqlxDB.Exec("INSERT INTO baskets (user_id, name, created_at, updated_at) VALUES (?, ?, ?, ?)", basket.UserID, basket.Name, timeNow, timeNow)

and I used the sqlmock function:

rows := sqlmock.NewRows([]string{"id", "user_id", "name", "created_at", "updated_at"}).
            AddRow(1, userID, name, timeNow, timeNow)
mock.ExpectPrepare("^SELECT (.+) FROM baskets WHERE").ExpectQuery().WithArgs(userID).WillReturnRows(rows)

for the sqlx query:

sqlxDB.PrepareNamed("SELECT id, user_id , name, created_at, updated_at FROM baskets WHERE user_id = :user_id")

@gorilla001
Copy link

how to prepare insert query?

@djui
Copy link

djui commented Dec 15, 2016

@sim4life Is sqlmock DataDog's https://github.com/DATA-DOG/go-sqlmock ?

@nachogoca
Copy link

How could I mock Queryx without arguments?

I am getting error from sqlmock all expectations were already fulfilled, call to Query 'SELECT ...' with args [] was not expected" because Queryx adds args to Query call even when no args are give.

@xyzzzyx
Copy link

xyzzzyx commented Feb 25, 2020

Hi @sim4life @nachogoca Were you guys able to mock with Queryx?
I have the following that I am trying to mock a basic query with where clause:

stmt, err := c.DB.Preparex(query)
	if err != nil {
		log.Fatalln(err)
	}
	defer stmt.Close()

	rows, err := stmt.Queryx(ID)
	if err != nil {
		log.Fatalln(err)
	}
	defer rows.Close()

but keep on getting query mismatch. Do you have an example for above case? Also I do not see any issues with SELECT (+.) FROM Table.

@sim4life
Copy link
Author

sim4life commented May 4, 2020

@sim4life Is sqlmock DataDog's https://github.com/DATA-DOG/go-sqlmock ?

Yes

@sim4life
Copy link
Author

sim4life commented May 4, 2020

Hi @sim4life @nachogoca Were you guys able to mock with Queryx?
I have the following that I am trying to mock a basic query with where clause:

stmt, err := c.DB.Preparex(query)
	if err != nil {
		log.Fatalln(err)
	}
	defer stmt.Close()

	rows, err := stmt.Queryx(ID)
	if err != nil {
		log.Fatalln(err)
	}
	defer rows.Close()

but keep on getting query mismatch. Do you have an example for above case? Also I do not see any issues with SELECT (+.) FROM Table.

Yes, I managed to mock SQLMock .. but I'm NOT sure about QueryX.
Can you share your code and error on gitpod.io or something?

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

No branches or pull requests

6 participants