Skip to content

Commit

Permalink
#36: Add coverage on Union/UnionAll + Time func in Schema, Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Aug 18, 2022
1 parent 2bdb01a commit 7268266
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 9 deletions.
6 changes: 4 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ func (r *DB) reset() {
r.Builder.limit = 0
r.Builder.join = []string{}
r.Builder.from = ""
r.Builder.union = []string{}
r.Builder.isUnionAll = false
r.Builder.lockForUpdate = nil
r.Builder.whereExists = ""
r.Builder.orderByRaw = nil
r.Builder.startBindingsAt = 1

if len(r.Builder.union) == 0 {
r.Builder.union = []string{}
}
}

// Select accepts columns to select from a table
Expand Down
83 changes: 78 additions & 5 deletions builder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package buildsqlx

import (
"errors"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -336,7 +337,7 @@ func TestDB_Union(t *testing.T) {
res, err := union.Table(UsersTable).Select("name", "points").Get()
assert.NoError(t, err)
for _, v := range res {
assert.Equal(t, v["points"], userForUnion["points"])
assert.Equal(t, v["baz"], userForUnion["points"])
}

_, err = db.Truncate(TestTable)
Expand All @@ -347,6 +348,78 @@ func TestDB_Union(t *testing.T) {
}

func TestDB_InTransaction(t *testing.T) {
var tests = map[string]struct {
dataMap map[string]interface{}
res interface{}
err error
}{
"transaction commit ok": {
dataMap: dataMap,
res: 1,
err: nil,
},
"transaction commit ok int64": {
dataMap: dataMap,
res: int64(1),
err: nil,
},
"transaction commit ok uint64": {
dataMap: dataMap,
res: uint64(1),
err: nil,
},
"transaction commit ok map[string]interface{}": {
dataMap: dataMap,
res: map[string]interface{}{"foo": "foo foo foo", "bar": "bar bar bar", "baz": int64(123)},
err: nil,
},
"transaction commit ok []map[string]interface{}": {
dataMap: dataMap,
res: []map[string]interface{}{
{
"foo": "foo foo foo", "bar": "bar bar bar", "baz": int64(123),
},
},
err: nil,
},
"transaction early exit err": {
dataMap: dataMap,
res: 0,
err: errors.New("some error"),
},
"transaction rollback": {
dataMap: dataMap,
res: 0,
err: nil,
},
}

for n, tt := range tests {
t.Run(n, func(t *testing.T) {
_, err := db.Truncate(TestTable)
assert.NoError(t, err)

defer func() {
_, err = db.Truncate(TestTable)
assert.NoError(t, err)
}()

err = db.InTransaction(func() (interface{}, error) {
err = db.Table(TestTable).Insert(tt.dataMap)

return tt.res, tt.err
})

if tt.err != nil {
assert.Error(t, tt.err, err)
} else {
assert.NoError(t, err)
}
})
}
}

func TestDB_InTransactionErr(t *testing.T) {
_, err := db.Truncate(TestTable)
assert.NoError(t, err)

Expand All @@ -358,9 +431,9 @@ func TestDB_InTransaction(t *testing.T) {
err = db.InTransaction(func() (interface{}, error) {
err = db.Table(TestTable).Insert(dataMap)

return 1, err
return 0, errors.New("some err")
})
assert.NoError(t, err)
assert.Error(t, err, errors.New("some err"))
}

func TestDB_HasTable(t *testing.T) {
Expand Down Expand Up @@ -908,9 +981,9 @@ func TestDB_UnionAll(t *testing.T) {
err = db.Table(UsersTable).InsertBatch(batchUsers)
assert.NoError(t, err)

res, err := db.Table(UsersTable).Select("name").UnionAll().Table(UsersTable).Get()
res, err := db.Table(UsersTable).Select("name").UnionAll().Table(UsersTable).Select("name").Get()
assert.NoError(t, err)
assert.Equal(t, len(res), len(batchUsers))
assert.Equal(t, len(res), len(batchUsers)*2)

_, err = db.Truncate(UsersTable)
assert.NoError(t, err)
Expand Down
11 changes: 11 additions & 0 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (r *DB) Get() ([]map[string]interface{}, error) {
}

query += builder.buildSelect()
// clean union (all) after ensuring selects are built
r.Builder.union = []string{}
r.Builder.isUnionAll = false
} else { // std builder
query = builder.buildSelect()
}
Expand Down Expand Up @@ -407,11 +410,19 @@ func (r *DB) InTransaction(fn func() (interface{}, error)) error {

res, err := fn()
if err != nil {
errTxn := txn.Rollback()
if errTxn != nil {
return err
}
return err
}

isOk := false
switch v := res.(type) {
case int:
if v > 0 {
isOk = true
}
case int64:
if v > 0 {
isOk = true
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module github.com/arthurkushman/buildsqlx

go 1.12
go 1.17

require (
github.com/lib/pq v1.2.0
github.com/stretchr/testify v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
1 change: 1 addition & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func TestTable_DateTime(t *testing.T) {
table.Json("settings")
table.Char("tag", 10)
table.Date("birthday", false)
table.Time("celebration_time", false)
table.DateTime("created_at", true)
table.DateTimeTz("updated_at", true)

Expand Down

0 comments on commit 7268266

Please sign in to comment.