forked from nelsam/gorq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_map_test.go
130 lines (109 loc) · 3.18 KB
/
db_map_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gorq
import (
"context"
"database/sql"
"reflect"
"testing"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/outdoorsy/gorp"
"github.com/outdoorsy/gorq/interfaces"
"github.com/outdoorsy/gorq/plans"
"github.com/stretchr/testify/suite"
)
type ValidStruct struct {
ExportedValue string
}
type QueryTestSuite struct {
suite.Suite
Exec SqlExecutor
TypeName string
}
func (suite *QueryTestSuite) SetupSuite() {
dbMap := new(DbMap)
dbMap.Dialect = gorp.SqliteDialect{}
connection, err := sql.Open("sqlite3", "/tmp/gorptest.bin")
if !suite.NoError(err) {
suite.T().FailNow()
}
dbMap.Db = connection
dbMap.AddTable(ValidStruct{})
suite.Exec = dbMap
}
func (suite *QueryTestSuite) getQueryFor(structType interface{}) *plans.QueryPlan {
var ptr, val interface{}
valueOfStruct := reflect.ValueOf(structType)
if valueOfStruct.Kind() == reflect.Ptr {
ptr = structType
val = valueOfStruct.Elem().Interface()
} else {
ptr = reflect.New(valueOfStruct.Type()).Interface()
val = structType
}
q := suite.Exec.Query(val)
suite.Implements((*interfaces.Query)(nil), q)
if plan, ok := q.(*plans.QueryPlan); suite.True(ok) {
suite.NotEqual(0, len(plan.Errors),
"%s.Query(ref) should error if ref is not a pointer to a struct", suite.TypeName)
}
q = suite.Exec.Query(ptr)
if plan, ok := q.(*plans.QueryPlan); suite.True(ok) {
return plan
}
return nil
}
func (suite *QueryTestSuite) TestDbMapQuery_ValidStruct() {
q := suite.getQueryFor(ValidStruct{})
suite.Equal(0, len(q.Errors),
"%s.Query(ref) should not generate errors if ref is a pointer to a struct with exported fields", suite.TypeName)
}
type DbMapTestSuite struct {
QueryTestSuite
}
func TestDbMapSuite(t *testing.T) {
suite.Run(t, new(DbMapTestSuite))
}
func (suite *DbMapTestSuite) SetupSuite() {
suite.QueryTestSuite.SetupSuite()
suite.TypeName = "DbMap"
}
func (suite *DbMapTestSuite) TestBegin() {
// TODO: come back around and fix this some day
suite.T().Skip("Skipping due to lack of support for execution timeouts in Sqlite")
tx, err := suite.Exec.(*DbMap).Begin(1 * time.Second)
if suite.NoError(err) {
suite.IsType((*Transaction)(nil), tx)
}
}
func (suite *DbMapTestSuite) TestAttachContext() {
suite.T().Run("DbMap Attach Context", func(t *testing.T) {
ctx := context.Background()
withCtx := suite.Exec.AttachContext(ctx)
dbmWithCtx := withCtx.(*DbMap)
dbmWoutCtx := suite.Exec.(*DbMap)
suite.NotEqual(dbmWoutCtx, dbmWithCtx)
suite.NotEqual(dbmWithCtx.DbMap, dbmWoutCtx.DbMap)
})
suite.T().Run("Transaction AttachContext", func(t *testing.T) {
ctx := context.Background()
dbm := suite.Exec.(*DbMap)
txWoutCtx := &Transaction{Transaction: gorp.Transaction{}, dbmap: dbm}
txWithCtx := txWoutCtx.AttachContext(ctx).(*Transaction)
suite.NotEqual(txWoutCtx, txWithCtx)
suite.NotEqual(txWoutCtx.Transaction, txWithCtx.Transaction)
})
}
type TransactionTestSuite struct {
QueryTestSuite
}
func TestTransactionSuite(t *testing.T) {
suite.Run(t, new(TransactionTestSuite))
}
func (suite *TransactionTestSuite) SetupSuite() {
suite.QueryTestSuite.SetupSuite()
suite.TypeName = "Transaction"
dbMap := suite.Exec.(*DbMap)
trans := new(Transaction)
trans.dbmap = dbMap
suite.Exec = trans
}