forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialect_oracle_test.go
195 lines (165 loc) · 5.49 KB
/
dialect_oracle_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build !integration
// +build !integration
package gorp_test
import (
"database/sql"
"github.com/go-gorp/gorp/v3/mocks"
"github.com/stretchr/testify/mock"
"reflect"
"testing"
"time"
"github.com/go-gorp/gorp/v3"
"github.com/stretchr/testify/suite"
)
type OracleDialectSuite struct {
suite.Suite
dialect gorp.OracleDialect
}
func (s *OracleDialectSuite) SetupTest() {
s.dialect = gorp.OracleDialect{}
}
func TestOracleDialectSuite(t *testing.T) {
suite.Run(t, new(OracleDialectSuite))
}
func (s *OracleDialectSuite) TestType() {
s.Equal(gorp.Oracle, s.dialect.Type())
}
func (s *OracleDialectSuite) TestQuerySuffix() {
s.Equal("", s.dialect.QuerySuffix())
}
func (s *OracleDialectSuite) TestToSqlType() {
tests := []struct {
name string
value interface{}
maxSize int
autoIncr bool
expected string
}{
{"bool", true, 0, false, "NUMBER(1)"},
{"int8", int8(1), 0, false, "NUMBER(3)"},
{"int16", int16(1), 0, false, "NUMBER(5)"},
{"int32", int32(1), 0, false, "NUMBER(10)"},
{"int32 auto-increment", int32(1), 0, true, "NUMBER GENERATED BY DEFAULT AS IDENTITY"},
{"int64", int64(1), 0, false, "NUMBER(19)"},
{"int64 auto-increment", int64(1), 0, true, "NUMBER GENERATED BY DEFAULT AS IDENTITY"},
{"uint8", uint8(1), 0, false, "NUMBER(19)"},
{"uint16", uint16(1), 0, false, "NUMBER(19)"},
{"uint32", uint32(1), 0, false, "NUMBER(19)"},
{"uint64", uint64(1), 0, false, "NUMBER(19)"},
{"float32", float32(1), 0, false, "BINARY_FLOAT"},
{"float64", float64(1), 0, false, "BINARY_DOUBLE"},
{"[]uint8", []uint8{1}, 0, false, "BLOB"},
{"NullInt64", sql.NullInt64{}, 0, false, "NUMBER(19)"},
{"NullFloat64", sql.NullFloat64{}, 0, false, "BINARY_DOUBLE"},
{"NullBool", sql.NullBool{}, 0, false, "NUMBER(1)"},
{"Time", time.Time{}, 0, false, "TIMESTAMP WITH TIME ZONE"},
{"default-size string", "", 0, false, "CLOB"},
{"sized string", "", 50, false, "VARCHAR2(50)"},
{"large string", "", 1024, false, "VARCHAR2(1024)"},
}
for _, tt := range tests {
s.Run(tt.name, func() {
typ := reflect.TypeOf(tt.value)
sqlType := s.dialect.ToSqlType(typ, gorp.ColumnOptions{
MaxSize: tt.maxSize,
IsAutoIncr: tt.autoIncr,
})
s.Equal(tt.expected, sqlType)
})
}
// Test pointer types
s.Run("pointer types", func() {
var i int
sqlType := s.dialect.ToSqlType(reflect.TypeOf(&i), gorp.ColumnOptions{})
s.Equal("NUMBER(19)", sqlType)
})
}
func (s *OracleDialectSuite) TestAutoIncrStr() {
s.Equal("", s.dialect.AutoIncrStr())
}
func (s *OracleDialectSuite) TestAutoIncrBindValue() {
s.Equal("NULL", s.dialect.AutoIncrBindValue())
}
func (s *OracleDialectSuite) TestAutoIncrInsertSuffix() {
s.Equal("", s.dialect.AutoIncrInsertSuffix(nil))
}
func (s *OracleDialectSuite) TestCreateTableSuffix() {
s.Equal("", s.dialect.CreateTableSuffix())
}
func (s *OracleDialectSuite) TestCreateIndexSuffix() {
s.Equal("", s.dialect.CreateIndexSuffix())
}
func (s *OracleDialectSuite) TestDropIndexSuffix() {
s.Equal("", s.dialect.DropIndexSuffix())
}
func (s *OracleDialectSuite) TestTruncateClause() {
s.Equal("truncate", s.dialect.TruncateClause())
}
func (s *OracleDialectSuite) TestSleepClause() {
s.Equal("dbms_lock.sleep(1)", s.dialect.SleepClause(1*time.Second))
s.Equal("dbms_lock.sleep(0)", s.dialect.SleepClause(100*time.Millisecond))
}
func (s *OracleDialectSuite) TestBindVar() {
s.Equal(":1", s.dialect.BindVar(0))
s.Equal(":2", s.dialect.BindVar(1))
s.Equal(":10", s.dialect.BindVar(9))
}
func (s *OracleDialectSuite) TestQuoteField() {
s.Equal(`"FOO"`, s.dialect.QuoteField("foo"))
s.Equal(`"BAR"`, s.dialect.QuoteField("BAR"))
s.Equal(`"MIXED_CASE"`, s.dialect.QuoteField("Mixed_Case"))
}
func (s *OracleDialectSuite) TestQuotedTableForQuery() {
s.Run("using the default schema", func() {
s.Equal(`"FOO"`, s.dialect.QuotedTableForQuery("", "foo"))
})
s.Run("with a supplied schema", func() {
s.Equal(`myschema."BAR"`, s.dialect.QuotedTableForQuery("myschema", "bar"))
})
}
func (s *OracleDialectSuite) TestIfSchemaNotExists() {
s.Equal("CREATE if not exists", s.dialect.IfSchemaNotExists("CREATE", "myschema"))
}
func (s *OracleDialectSuite) TestIfTableExists() {
s.Equal("DROP if exists", s.dialect.IfTableExists("DROP", "myschema", "mytable"))
}
func (s *OracleDialectSuite) TestIfTableNotExists() {
s.Equal("CREATE if not exists", s.dialect.IfTableNotExists("CREATE", "myschema", "mytable"))
}
func (s *OracleDialectSuite) TestInsertQueryToTarget() {
tests := []struct {
name string
target interface{}
expectedErr string
}{
{"int64 pointer", new(int64), ""},
{"int32 pointer", new(int32), ""},
{"int pointer", new(int), "Id field can be int, int32 or int64"},
{"string pointer", new(string), "Id field can be int, int32 or int64"},
}
for _, tt := range tests {
s.Run(tt.name, func() {
// Create a new mock SqlExecutor
mockExec := new(mocks.SqlExecutor)
mockExec.On("Exec", mock.Anything, mock.Anything).Return(sql.Result(nil), nil)
mockExec.On("SelectInt", mock.Anything).Return(int64(42), nil)
err := s.dialect.InsertQueryToTarget(mockExec, "INSERT", "SELECT", tt.target)
if tt.expectedErr != "" {
s.EqualError(err, tt.expectedErr)
} else {
s.NoError(err)
switch v := tt.target.(type) {
case *int64:
s.Equal(int64(42), *v)
case *int32:
s.Equal(int32(42), *v)
case *int:
s.Fail("int type should not be supported")
}
}
})
}
}