FAQ
Hello,

I'm trying to write a wrapper for database/sql/driver to be able to use it
in tests. The general idea for now is to be able to say to this wrapper
driver to fail Nth query. I have done proof of concept but I can't seem to
solve a problem of wrapping transaction. For now my code looks like this:

https://github.com/rzajac/goassert/blob/database_sql/dbwrap/conn.go#L13-L16

I can force Commit and Rollback to fail but have problems writing
passthrough methods for Query, Exec, ...

https://github.com/rzajac/goassert/blob/database_sql/dbwrap/conn.go#L65-L71

And this is how I initialize it in the code:

import (
"database/sql"
"database/sql/driver"
"github.com/rzajac/goassert/dbwrap"
"github.com/ziutek/mymysql/godrv"
)


var drv driver.Driver = &godrv.Driver{}
testDbWr = dbwrap.New("testing", drv)
// use the new name to connect
testDb, err = sql.Open("testing", connectString)

Do you guys have any suggestions how to do it?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tamás Gulácsi at Apr 5, 2014 at 5:50 am
    See what is mymysql doing in its init(): it registers a function for a name. You can do it also, for "testing".

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ralph Zajac at Apr 5, 2014 at 10:30 am
    Hey

    I'm already doing it. And my testing wrap already works. For most of the
    methods I can create pass through methods in my "fake" driver. The problem
    I have is with driver.Conn and Begin() (Tx, error) method. In my code I do
    this:

    func (this *conn) Begin() (driver.Tx, error) {
    txn, err := this.Conn.Begin()
    return &tx{txn}, err
    }

    Where I call proper driver's Begin() method but then I have only access to
    Commit() and Rollback methods to intercept in my tx struct.

    I was wondering if there is a way to return sql.Tx interface from my code
    that calls the original driver whatever it is.


    On Saturday, April 5, 2014 7:50:41 AM UTC+2, Tamás Gulácsi wrote:

    See what is mymysql doing in its init(): it registers a function for a
    name. You can do it also, for "testing".
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 4, '14 at 11:30p
activeApr 5, '14 at 10:30a
posts3
users2
websitegolang.org

2 users in discussion

Ralph Zajac: 2 posts Tamás Gulácsi: 1 post

People

Translate

site design / logo © 2021 Grokbase