go-sqlmock | Sql mock driver for golang to test database interactions | Unit Testing library
kandi X-RAY | go-sqlmock Summary
kandi X-RAY | go-sqlmock Summary
Sql mock driver for golang to test database interactions
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of go-sqlmock
go-sqlmock Key Features
go-sqlmock Examples and Code Snippets
Community Discussions
Trending Discussions on go-sqlmock
QUESTION
I am using go-sqlmock
for the first time and I am trying to write a test for post operation. I am using gorm
and gin
.
- The test is giving me an error where
s.mock.ExpectQuery(regexp.QuoteMeta(....
I am not what is the issue here. I have posted both the test and the output. - Also, (this has nothing to do with 1) in this test I really do not know what the
code
will be as it is randomly generated in the api controller. Is there a way to assign a generic number in thecode
field.
The test file
...ANSWER
Answered 2022-Jan-24 at 08:37Solution to the first issue:
when using testify/suite
, There are bunch of methods if created for the Suite
struct, they will be automatically executed when running the test. That being said, These methods will pass through an interface filter. In the case of .SetupSuite
, it has to have NO arguments and No return, in order to run.
Solution to the second issue:
There is a way in go-sqlmock
to match any kind of data by using sqlmock.AnyArg()
.
Fixed code:
QUESTION
I'm trying to use go-sqlmock
with gorm
for testing. I want to write a test for the initial database migration, but I've hit a panic: runtime error: invalid memory address or nil pointer dereference
and I've been having trouble figuring out why. Judging by the error stack, I think it's this statement that does it: db.AutoMigrate(&models.User{})
. I'm not sure why, as db
has allegedly started up successfully by this point and models.User
is defined and instantiated as an argument to db.AutoMigrate
. I have a feeling the error is in the mocks.NewDatabase
function, but I'm at a loss.
Not sure if anyone has the time or will to take a peak at the relevant code and help me out? I've noted in the code where the failures occur (they're in the final two blocks of code). Let me know if any additional context would help.
Relevant Codeproject/src/models/models.go
...ANSWER
Answered 2021-Oct-14 at 18:02Figured it out. It was a config option: &gorm.Config{ PrepareStmt: true }
. While this works in production, it does not work with sqlmock. Fixed it by changing it to: &gorm.Config{}
.
QUESTION
I have two Go test cases as shown below that test a gRPC function called MyEndpoint
.
MyEndpoint
is supposed to succeed when the database row it selects has Field1 == "A" and return an error otherwise.
I'm mocking out the database with the go-sqlmock package from Data-dog.
...ANSWER
Answered 2021-May-03 at 10:16It's because of an incorrect assignment to modifiedDBRow
slice in your first test. Remember that a slice does not store any data, it just describes a section of an underlying array.
So when you modified modifiedDBRow
in the first test, you've inherently modified the underlying array containing value at [0]
, from A
to Z
, which persists when you read the dbRow
in your next test. Either reset the value at the end of the first test or have a different set of variables to use across tests.
QUESTION
sqlmock needs to match the SQL in order. But if I have concurrent query in my code just like this:
...ANSWER
Answered 2021-Mar-16 at 11:29The MatchExpectationsInOrder
method disables in-order checking for exactly this scenario.
QUESTION
Go here. Trying to figure out how to use SQL mock v2.
Here's my interface:
...ANSWER
Answered 2020-Sep-25 at 14:22I decided to Go (no pun intended) with Java instead.
QUESTION
So I'm just getting to grips with Golang. I'm writing an application for funsies just to understand stuff and get my head around it.
I have a whole bunch of functions that will interact with a DB where I pass in *SQL.DB
for the function to use. I can test those easily enough using a mocked interface from sqlmock.
No problem there.
I'm now writing the Initialisation function for the application which will initiate the DB connection which will be attached to a struct and from there passed into utility functions.
However, I am struggling to find a way to easily test that connection without having the hassle of setting up an actual database.
So I guessing that I have probably either badly structured my app, or I've missed something, potentially pretty obvious.
So here is some example code to illustrate my predicament.
util.go
ANSWER
Answered 2020-Jun-29 at 00:11There is Dockertest which creates a Docker container running whatever you want (i.e. MySQL), and you can test against that. It's designed for being able to do proper integration testing, so it should be able to do what you want (you wont need sqlmock
anymore too).
QUESTION
repository.go
...ANSWER
Answered 2020-Apr-03 at 13:06the reason is that you are not escaping your regular expression of query string. Sqlmock expects regular expression in your expectation.
Now in case if you want to match exact query string, not parts of it. You can specify an sqlmock mock option, like this:
QUESTION
I'm using Gorm with the postgresql driver. I try to mock a database insert with go-sqlmock:
...ANSWER
Answered 2020-Feb-11 at 23:44There were a couple of issues with my code:
1) As @flimzy pointed out rightfully, there has to be a ExpectBegin()
(and ExpectCommit()
) statement. This gets more obvious if one turns on the GORM debugger that shows what exactly GORM is doing.
2) ExpectExec("INSERT INTO test").WithArgs("c")
does quite obviously not match myDB.Exec("INSERT INTO test(first_name) VALUES (?)", "c")
3) One has to escape the statement, as go-sqlmock takes a regex, here Go's https://godoc.org/regexp#QuoteMeta comes in handy.
Working code:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install go-sqlmock
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page