Coder Social home page Coder Social logo

go-testdb's People

Contributors

bencalegari avatar bhcleek avatar cwang-pivotal avatar damnwidget avatar erikstmartin avatar naxhh avatar paulbellamy avatar quii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-testdb's Issues

How to stub out `Count`?

We have a func that uses sth like db.DbClient.Model(&Countable{}).Where("filter = ?", filter).Count(&count), any way to set query that could help to test with testdb?

Exec

need to implement the Exec method

Support for returning sql.NullString

At present either a string or time.Time is returned for the driver.Value within RowsFromCSVString. It would be nice to add support returning a sql.NullString.

It could look something like:

type testDriver struct {
    // as is
    enableNullParsing bool
}

// Within RowsFromCSVString...
if d.enableTimeParsing {
    // as is
} else if d.enableNullParsing && v == "" {
    row[i] = sql.NullString{}
} else {
    row[i] = v
}

Happy to knock this up into a PR if it's something you'd like to see added.

Complete coverage of SQL code including rows.Err()

I found it quite hard to trigger the unexpected error condition below. How did you guys managed to please go test -cover in your SQL code?

        // Initialize SQL query
        rows, err := q.Query(SomeSQLQuery)
        if err != nil {
                panic("failed to initialize SQL query")
        }
        defer rows.Close()

        // Execute SQL query
        var a, b, c sql.NullString
        for rows.Next() {
                err = rows.Scan(&a, &b, &c)
                if err != nil {
                        panic("failed to get SQL results")
                }
                // ...
        }

        if rows.Err() != nil {
                panic("unexpected SQL error")
        }

No License

Would you mind providing a license for this? BSD would be a good choice. Otherwise, we have to assume you reserve all rights, and nobody can use it. If that's what you want, it's probably worth explicitly stating that anyway.

calling the same query more than once

Does go-testdb support calling the same query more than once?

Something like this :

func TestStubQueryRowTwice(t *testing.T) {
    defer Reset()

    db, _ := sql.Open("testdb", "")

    values := []int{5, 6}

    query := "select bar from foo"
    columns := []string{"bar"}
    var result string

    for _, value := range values {
        result += strconv.Itoa(value) + "\n"
    }

    StubQuery(query, RowsFromCSVString(columns, result))
    StubQuery(query, RowsFromCSVString(columns, result))

    for i := 0; i < 2; i++ {
        rows, errQuery := db.Query(query)

        if rows == nil {
            t.Fatal("stub query should have returned rows")
        }

        if errQuery == sql.ErrNoRows {
            t.Fatal("stub query should have returned rows")
        }

        var j int
        for rows.Next() {
            var count int
            err := rows.Scan(&count)

            if err != nil {
                t.Fatal(err)
            }

            if count != values[j] {
                t.Fatal("failed to return count")
            }

            j += 1
        }

        if j != 2 {
            t.Fatal("2 rows expected")
        }
    }
}

Interaction With Race Detector

This library is very handy, but when used in tests of concurrent code will trigger the go race detector.

A very simple case is if you have two tests that both call something like testdb.SetQueryFunc. The first test to run uses the query function in a goroutine, the call to set the query function in the second test can be detected as a race.

The race detector is logically correct; a value was read from and written to by different goroutines with no synchronization. However, this should be safe usage of the testdb library, as the two tests are never run concurrently.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.