123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- package gosql_test
- import (
- "context"
- "io/ioutil"
- "path/filepath"
- "testing"
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- "github.com/vladimirok5959/golang-sql/gosql"
- )
- var _ = Describe("gosql", func() {
- Context("Open", func() {
- var migrationsDir string
- var ctx = context.Background()
- var sql = "select id, name from users where id=$1"
- var (
- id int
- name string
- )
- BeforeEach(func() {
- var err error
- migrationsDir, err = filepath.Abs("../db/migrations")
- Expect(err).To(Succeed())
- // // Reset databases
- // // Note: uncomment for MySQL and PostgreSQL tests
- // var db common.Engine
- // // MySQL
- // db, err = gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", "", true, false)
- // Expect(err).To(Succeed())
- // _, _ = db.Exec(ctx, "DROP TABLE schema_migrations, users")
- // // PostgreSQL
- // db, err = gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", "", true, false)
- // Expect(err).To(Succeed())
- // _, _ = db.Exec(ctx, "DROP TABLE schema_migrations, users")
- })
- // // Note: you need to up MySQL server for this test case
- // Context("for MySQL", func() {
- // It("open connection, migrate and select data", func() {
- // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
- // Expect(err).To(Succeed())
- // Expect(id).To(Equal(1))
- // Expect(name).To(Equal("Alice"))
- // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
- // Expect(err).To(Succeed())
- // Expect(id).To(Equal(2))
- // Expect(name).To(Equal("Bob"))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and select by ID", func() {
- // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // err = db.QueryRowByID(ctx, 1, &rowUser)
- // Expect(err).To(Succeed())
- // Expect(rowUser.ID).To(Equal(int64(1)))
- // Expect(rowUser.Name).To(Equal("Alice"))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and check row", func() {
- // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
- // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
- // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
- // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
- // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and delete row", func() {
- // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // var size int
- // Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(1))
- // Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(0))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and insert row", func() {
- // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // var size int
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(2))
- // rowUser.Name = "James"
- // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(3))
- // rowUser.Name = "Robert"
- // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(4))
- // names := []string{}
- // err = db.Each(
- // ctx,
- // "SELECT 0, name FROM users ORDER BY name ASC",
- // func(ctx context.Context, rows *gosql.Rows) error {
- // if err := rows.Scans(&rowUser); err != nil {
- // return err
- // }
- // names = append(names, rowUser.Name)
- // return nil
- // },
- // )
- // Expect(err).To(Succeed())
- // Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and update row", func() {
- // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // _, err = db.Exec(ctx, "INSERT INTO users (id, name) VALUES ('3', 'Patrik')")
- // Expect(err).To(Succeed())
- // rowUser.ID = 3
- // rowUser.Name = "Robert"
- // Expect(db.UpdateRow(ctx, &rowUser)).To(Succeed())
- // err = db.QueryRowByID(ctx, 3, &rowUser)
- // Expect(err).To(Succeed())
- // Expect(rowUser.ID).To(Equal(int64(3)))
- // Expect(rowUser.Name).To(Equal("Robert"))
- // Expect(db.Close()).To(Succeed())
- // })
- // })
- // // Note: you need to up PostgreSQL server for this test case
- // Context("for PostgreSQL", func() {
- // It("open connection, migrate and select data", func() {
- // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
- // Expect(err).To(Succeed())
- // Expect(id).To(Equal(1))
- // Expect(name).To(Equal("Alice"))
- // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
- // Expect(err).To(Succeed())
- // Expect(id).To(Equal(2))
- // Expect(name).To(Equal("Bob"))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and select by ID", func() {
- // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // err = db.QueryRowByID(ctx, 1, &rowUser)
- // Expect(err).To(Succeed())
- // Expect(rowUser.ID).To(Equal(int64(1)))
- // Expect(rowUser.Name).To(Equal("Alice"))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and check row", func() {
- // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
- // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
- // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
- // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
- // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and delete row", func() {
- // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // var size int
- // Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(1))
- // Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(0))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and insert row", func() {
- // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // var size int
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(2))
- // rowUser.Name = "James"
- // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(3))
- // rowUser.Name = "Robert"
- // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
- // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- // Expect(err).To(Succeed())
- // Expect(size).To(Equal(4))
- // names := []string{}
- // err = db.Each(
- // ctx,
- // "SELECT 0, name FROM users ORDER BY name ASC",
- // func(ctx context.Context, rows *gosql.Rows) error {
- // if err := rows.Scans(&rowUser); err != nil {
- // return err
- // }
- // names = append(names, rowUser.Name)
- // return nil
- // },
- // )
- // Expect(err).To(Succeed())
- // Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
- // Expect(db.Close()).To(Succeed())
- // })
- // It("open connection, migrate and update row", func() {
- // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
- // Expect(err).To(Succeed())
- // var rowUser struct {
- // ID int64 `field:"id" table:"users"`
- // Name string `field:"name"`
- // }
- // _, err = db.Exec(ctx, "INSERT INTO users (id, name) VALUES ('3', 'Patrik')")
- // Expect(err).To(Succeed())
- // rowUser.ID = 3
- // rowUser.Name = "Robert"
- // Expect(db.UpdateRow(ctx, &rowUser)).To(Succeed())
- // err = db.QueryRowByID(ctx, 3, &rowUser)
- // Expect(err).To(Succeed())
- // Expect(rowUser.ID).To(Equal(int64(3)))
- // Expect(rowUser.Name).To(Equal("Robert"))
- // Expect(db.Close()).To(Succeed())
- // })
- // })
- Context("for SQLite", func() {
- It("open connection, migrate and select data", func() {
- f, err := ioutil.TempFile("", "go-sqlite-test-")
- Expect(err).To(Succeed())
- f.Close()
- db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
- Expect(err).To(Succeed())
- err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
- Expect(err).To(Succeed())
- Expect(id).To(Equal(1))
- Expect(name).To(Equal("Alice"))
- err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
- Expect(err).To(Succeed())
- Expect(id).To(Equal(2))
- Expect(name).To(Equal("Bob"))
- Expect(db.Close()).To(Succeed())
- })
- It("open connection, migrate and select by ID", func() {
- f, err := ioutil.TempFile("", "go-sqlite-test-")
- Expect(err).To(Succeed())
- f.Close()
- db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
- Expect(err).To(Succeed())
- var rowUser struct {
- ID int64 `field:"id" table:"users"`
- Name string `field:"name"`
- }
- err = db.QueryRowByID(ctx, 1, &rowUser)
- Expect(err).To(Succeed())
- Expect(rowUser.ID).To(Equal(int64(1)))
- Expect(rowUser.Name).To(Equal("Alice"))
- Expect(db.Close()).To(Succeed())
- })
- It("open connection, migrate and check row", func() {
- f, err := ioutil.TempFile("", "go-sqlite-test-")
- Expect(err).To(Succeed())
- f.Close()
- db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
- Expect(err).To(Succeed())
- var rowUser struct {
- ID int64 `field:"id" table:"users"`
- Name string `field:"name"`
- }
- Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
- Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
- Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
- Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
- Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
- Expect(db.Close()).To(Succeed())
- })
- It("open connection, migrate and delete row", func() {
- f, err := ioutil.TempFile("", "go-sqlite-test-")
- Expect(err).To(Succeed())
- f.Close()
- db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
- Expect(err).To(Succeed())
- var rowUser struct {
- ID int64 `field:"id" table:"users"`
- Name string `field:"name"`
- }
- var size int
- Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
- err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- Expect(err).To(Succeed())
- Expect(size).To(Equal(1))
- Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
- err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- Expect(err).To(Succeed())
- Expect(size).To(Equal(0))
- Expect(db.Close()).To(Succeed())
- })
- It("open connection, migrate and insert row", func() {
- f, err := ioutil.TempFile("", "go-sqlite-test-")
- Expect(err).To(Succeed())
- f.Close()
- db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
- Expect(err).To(Succeed())
- var rowUser struct {
- ID int64 `field:"id" table:"users"`
- Name string `field:"name"`
- }
- var size int
- err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- Expect(err).To(Succeed())
- Expect(size).To(Equal(2))
- rowUser.Name = "James"
- Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
- err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- Expect(err).To(Succeed())
- Expect(size).To(Equal(3))
- rowUser.Name = "Robert"
- Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
- err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- Expect(err).To(Succeed())
- Expect(size).To(Equal(4))
- names := []string{}
- err = db.Each(
- ctx,
- "SELECT 0, name FROM users ORDER BY name ASC",
- func(ctx context.Context, rows *gosql.Rows) error {
- if err := rows.Scans(&rowUser); err != nil {
- return err
- }
- names = append(names, rowUser.Name)
- return nil
- },
- )
- Expect(err).To(Succeed())
- Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
- Expect(db.Close()).To(Succeed())
- })
- It("open connection, migrate and update row", func() {
- f, err := ioutil.TempFile("", "go-sqlite-test-")
- Expect(err).To(Succeed())
- f.Close()
- db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
- Expect(err).To(Succeed())
- var rowUser struct {
- ID int64 `field:"id" table:"users"`
- Name string `field:"name"`
- }
- _, err = db.Exec(ctx, "INSERT INTO users (id, name) VALUES ('3', 'Patrik')")
- Expect(err).To(Succeed())
- rowUser.ID = 3
- rowUser.Name = "Robert"
- Expect(db.UpdateRow(ctx, &rowUser)).To(Succeed())
- err = db.QueryRowByID(ctx, 3, &rowUser)
- Expect(err).To(Succeed())
- Expect(rowUser.ID).To(Equal(int64(3)))
- Expect(rowUser.Name).To(Equal("Robert"))
- Expect(db.Close()).To(Succeed())
- })
- })
- It("open connection and skip migration", func() {
- f, err := ioutil.TempFile("", "go-sqlite-test-")
- Expect(err).To(Succeed())
- f.Close()
- db, err := gosql.Open("sqlite://"+f.Name(), "", true, false)
- Expect(err).To(Succeed())
- Expect(db.Ping(ctx)).To(Succeed())
- var size int
- err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
- Expect(err.Error()).To(Equal("no such table: users"))
- })
- })
- })
- func TestSuite(t *testing.T) {
- RegisterFailHandler(Fail)
- RunSpecs(t, "gosql")
- }
|