FAQ
Back in 2014 Andrew Gerrand made nice talk “Testing Techniques
<https://talks.golang.org/2014/testing.slide#1>” which advocates using
httptest fake http server implementation for your code testing. In scope of
business logic application the major dependency is SQL database. I found
testing part of go code that access data in database as problematic. The
Java word enjoys a very powerful H2 database
<http://www.h2database.com/html/quickstart.html> that can:

    - create database snapshot to the file
    - load the snapshot into in memory database instance
    - can simulate different dialects of sql

Is there any similar soft in go world for “database/sql/driver” capable of
running as embedded database engine?
P.S. I don’t see embedded versions of PostgreSQL or MySql. The SQLite is
not quite reach SQL implementation from enterprise point of view.

How do you test your data access in go code?

--
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

  • Shawn Milochik at Oct 8, 2015 at 8:29 pm
    If you use an interface instead of writing your code to speak SQL, you can
    create a dummy version that stores records in a map or something.

    This is demonstrated by @francesc in the first half of this demo:
    https://www.youtube.com/watch?v=wB1hoUJDbk0

    For example, you create an interface with Save, Search, Delete methods, and
    so on. Then you create a type that implements this against your database
    backend, and another that you use 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.
  • Matt Silverlock at Oct 9, 2015 at 12:11 pm
    https://robots.thoughtbot.com/interface-with-your-database-in-go is a good
    article that references what +Shawn Milochik mentioned.

        - Set up interfaces that other packages expect/deal with for DB
        operations
        - Have your postgres|mysql|whatever package satisfy that interface
        (effectively a driver for that interface)
        - In the packages that import your datastore interface, stub/mock the
        interface for testing
        - Keep the integration tests associated with the specific datastore
        implementation


    On Friday, October 9, 2015 at 4:30:14 AM UTC+8, Shawn Milochik wrote:

    If you use an interface instead of writing your code to speak SQL, you can
    create a dummy version that stores records in a map or something.

    This is demonstrated by @francesc in the first half of this demo:
    https://www.youtube.com/watch?v=wB1hoUJDbk0

    For example, you create an interface with Save, Search, Delete methods,
    and so on. Then you create a type that implements this against your
    database backend, and another that you use 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.
  • Eric Johnson at Oct 9, 2015 at 5:26 am
    Sqlite does support in-memory only database. I've used that successfully
    for testing purposes....

    Depends on your product's intended use. With databases, actually testing
    against different databases is going to be pretty critical if you intend to
    support running in production against multiple database engines. They all
    have their quirks, and you need to test for those. Perhaps adopt a pattern
    where your test cases look for an environment variable (or more than one),
    that defines how to connect to a database. Have your tests pre-populate
    your database, then run the tests you want to run.

    Eric.
    On Thursday, October 8, 2015 at 12:54:08 PM UTC-7, vkoch...@gmail.com wrote:

    Back in 2014 Andrew Gerrand made nice talk “Testing Techniques
    <https://talks.golang.org/2014/testing.slide#1>” which advocates using
    httptest fake http server implementation for your code testing. In scope of
    business logic application the major dependency is SQL database. I found
    testing part of go code that access data in database as problematic. The
    Java word enjoys a very powerful H2 database
    <http://www.h2database.com/html/quickstart.html> that can:

    - create database snapshot to the file
    - load the snapshot into in memory database instance
    - can simulate different dialects of sql

    Is there any similar soft in go world for “database/sql/driver” capable of
    running as embedded database engine?
    P.S. I don’t see embedded versions of PostgreSQL or MySql. The SQLite is
    not quite reach SQL implementation from enterprise point of view.

    How do you test your data access in go code?
    --
    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.
  • Hǎiliàng at Oct 9, 2015 at 7:58 am
    1. abstract database layer by interface (unit test)
    2. Docker (either unit test or integration test)
    3. a combination of 1 and 2

    Hǎiliàng

    --
    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.
  • Chris Hines at Oct 9, 2015 at 2:52 pm
    https://github.com/DATA-DOG/go-sqlmock can help.
    On Thursday, October 8, 2015 at 3:54:08 PM UTC-4, vkoch...@gmail.com wrote:

    Back in 2014 Andrew Gerrand made nice talk “Testing Techniques
    <https://talks.golang.org/2014/testing.slide#1>” which advocates using
    httptest fake http server implementation for your code testing. In scope of
    business logic application the major dependency is SQL database. I found
    testing part of go code that access data in database as problematic. The
    Java word enjoys a very powerful H2 database
    <http://www.h2database.com/html/quickstart.html> that can:

    - create database snapshot to the file
    - load the snapshot into in memory database instance
    - can simulate different dialects of sql

    Is there any similar soft in go world for “database/sql/driver” capable of
    running as embedded database engine?
    P.S. I don’t see embedded versions of PostgreSQL or MySql. The SQLite is
    not quite reach SQL implementation from enterprise point of view.

    How do you test your data access in go code?
    --
    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.
  • Kyle Wolfe at Oct 9, 2015 at 6:30 pm

    On Thursday, October 8, 2015 at 3:54:08 PM UTC-4, vkoch...@gmail.com wrote:

    P.S. I don’t see embedded versions of PostgreSQL or MySql. The SQLite is
    not quite reach SQL implementation from enterprise point of view.
    Maybe have a look at tidb <https://github.com/pingcap/tidb>? (
    https://github.com/pingcap/tidb/blob/master/docs/USAGE.md)

    --
    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
postedOct 8, '15 at 7:53p
activeOct 9, '15 at 6:30p
posts7
users7
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase