You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First off, I just wanted to thank you for creating such a wonderfully useful library! It has been a huge help to me.
Although I've managed to figure out most of the scenarios I've needed to test, the following one has been completely stumping me for the last week. Hopefully, somebody here will be able to help me out. Below is my code:
funcUpsertRecords(records []Record) error {
tx, err:=DB.Begin()
iferr!=nil {
log.Printf("Begin Error")
}
txOK:=falsedeferfunc() {
if!txOK {
tx.Rollback()
}
}()
_, err=tx.Exec("CREATE TEMPORARY TABLE temp_records "+"(serial VARCHAR, model VARCHAR) "+"ON COMMIT DROP")
iferr!=nil {
log.Printf("TempTable Error")
}
sql, err:=tx.Prepare(pq.CopyIn("temp_records", "serial", "model"))
for_, val:=rangerecords {
_, err=sql.Exec(val.Serial, val.Model, true) // I get a Panic on this lineiferr!=nil {
log.Printf("Copy Error")
}
}
_, err=sql.Exec()
iferr!=nil {
log.Printf("Flush Error")
}
err=sql.Close()
iferr!=nil {
log.Printf("Close Error")
}
_, err=tx.Exec("INSERT INTO records (serial, model) "+"SELECT serial, model FROM temp_records "+"ON CONFLICT (serial) DO UPDATE "+"SET model = EXCLUDED.model")
iferr!=nil {
log.Printf("Move Error")
}
err=tx.Commit()
iferr!=nil {
log.Printf("Commit Error")
}
txOK=truereturnnil
}
And, here's my test below:
funcTestShould_UpsertRecord (t*testing.T) {
mockDB, mock, err:=sqlmock.New()
require.NoError(t, err, "Error - %v", err)
defermockDB.Close()
DB=sqlx.NewDb(mockDB,"sqlmock")
deferDB.Close()
mock.ExpectBegin()
mock.ExpectExec("CREATE TEMPORARY TABLE temp_records .*").
WillReturnResult(sqlmock.NewResult(0,0))
mock.ExpectPrepare("COPY temp_devices .*")
ExpectExec().
WillReturnResult(sqlmock.NewResult(0, 2))
.
.
. /* This is where I get stuck and where I can't figure out how to handle the rest... */
.
.
mock.ExpectCommit()
mock.ExpectClose()
assert.NoError(t, err)
}
I've tried so many things that I've now pretty much hit a brick wall and don't know where to go from here.
Anybody out there know how I can complete the above?
Thanks in advance!
R
The text was updated successfully, but these errors were encountered:
Hi,
First off, I just wanted to thank you for creating such a wonderfully useful library! It has been a huge help to me.
Although I've managed to figure out most of the scenarios I've needed to test, the following one has been completely stumping me for the last week. Hopefully, somebody here will be able to help me out. Below is my code:
And, here's my test below:
I've tried so many things that I've now pretty much hit a brick wall and don't know where to go from here.
Anybody out there know how I can complete the above?
Thanks in advance!
R
The text was updated successfully, but these errors were encountered: