39
39
package tests
40
40
41
41
import (
42
- "errors"
43
42
"regexp"
44
- "sort"
45
43
"testing"
46
44
47
- "github.com/google/uuid"
48
45
"github.com/oracle-samples/gorm-oracle/oracle"
49
46
"gorm.io/gorm"
50
- "gorm.io/gorm/clause"
51
- "gorm.io/gorm/utils/tests"
52
47
)
53
48
54
- type FolderData struct {
55
- ID string `gorm:"primaryKey;column:folder_id"`
56
- Name string `gorm:"column:folder_nm"`
57
- Properties []FolderProperty `gorm:"foreignKey:ID;PRELOAD:false"`
49
+ type Student struct {
50
+ ID uint
51
+ Name string
58
52
}
59
53
60
- func (FolderData ) TableName () string {
61
- return "folder_data"
62
- }
63
-
64
- type FolderProperty struct {
65
- Seq uint64 `gorm:"autoIncrement"`
66
- ID string `gorm:"primaryKey;column:folder_id"`
67
- Key string `gorm:"primaryKey;unique"`
68
- Value string
69
- }
70
-
71
- func (FolderProperty ) TableName () string {
72
- return "folder_property"
73
- }
74
-
75
- func TestSkipQuoteIdentifiersMigrator (t * testing.T ) {
76
- db , err := openTestDBWithOptions (
77
- & oracle.Config {SkipQuoteIdentifiers : true },
78
- & gorm.Config {Logger : newLogger })
79
- if err != nil {
80
- t .Fatalf ("failed to connect database, got error %v" , err )
81
- }
82
-
83
- db .Migrator ().DropTable (& FolderData {}, & FolderProperty {})
84
- db .Migrator ().CreateTable (& FolderData {}, & FolderProperty {})
85
-
86
- folderDataTN := "FOLDER_DATA"
87
- if ! db .Migrator ().HasTable (folderDataTN ) {
88
- t .Errorf ("Failed to get table: %s" , folderDataTN )
89
- }
90
-
91
- if ! db .Migrator ().HasColumn (folderDataTN , "FOLDER_ID" ) {
92
- t .Errorf ("Failed to get column: FOLDER_ID" )
93
- }
94
-
95
- if ! db .Migrator ().HasColumn (folderDataTN , "FOLDER_NM" ) {
96
- t .Errorf ("Failed to get column: FOLDER_NM" )
97
- }
98
-
99
- folderPropertyTN := "FOLDER_PROPERTY"
100
- if ! db .Migrator ().HasTable (folderPropertyTN ) {
101
- t .Errorf ("Failed to get table: %s" , folderPropertyTN )
102
- }
103
-
104
- if ! db .Migrator ().HasColumn (folderPropertyTN , "SEQ" ) {
105
- t .Errorf ("Failed to get column: SEQ" )
106
- }
107
-
108
- if ! db .Migrator ().HasColumn (folderPropertyTN , "FOLDER_ID" ) {
109
- t .Errorf ("Failed to get column: FOLDER_ID" )
110
- }
111
-
112
- if ! db .Migrator ().HasColumn (folderPropertyTN , "KEY" ) {
113
- t .Errorf ("Failed to get column: KEY" )
114
- }
115
-
116
- if ! db .Migrator ().HasColumn (folderPropertyTN , "VALUE" ) {
117
- t .Errorf ("Failed to get column: VALUE" )
118
- }
54
+ func (s Student ) TableName () string {
55
+ return "STUDENTS"
119
56
}
120
57
121
58
func TestSkipQuoteIdentifiers (t * testing.T ) {
@@ -126,95 +63,41 @@ func TestSkipQuoteIdentifiers(t *testing.T) {
126
63
t .Fatalf ("failed to connect database, got error %v" , err )
127
64
}
128
65
129
- db .Migrator ().DropTable (& FolderData {}, & FolderProperty {})
130
- db .Migrator ().CreateTable (& FolderData {}, & FolderProperty {})
66
+ db .Migrator ().DropTable (& Student {})
67
+ db .Migrator ().CreateTable (& Student {})
131
68
132
- id := uuid .New ().String ()
133
- folder := FolderData {
134
- ID : id ,
135
- Name : "My Folder" ,
136
- Properties : []FolderProperty {
137
- {
138
- ID : id ,
139
- Key : "foo1" ,
140
- Value : "bar1" ,
141
- },
142
- {
143
- ID : id ,
144
- Key : "foo2" ,
145
- Value : "bar2" ,
146
- },
147
- },
69
+ if ! db .Migrator ().HasTable (& Student {}) {
70
+ t .Errorf ("Failed to get table: student" )
148
71
}
149
72
150
- if err := db .Create ( & folder ). Error ; err != nil {
151
- t .Errorf ("Failed to insert data, got %v" , err )
73
+ if ! db .Migrator (). HasColumn ( & Student {}, "ID" ) {
74
+ t .Errorf ("Failed to get column: id" )
152
75
}
153
76
154
- createdFolder := FolderData {}
155
- if err := db .Model (& FolderData {}).Preload ("Properties" ).First (& createdFolder ).Error ; err != nil {
156
- t .Errorf ("Failed to query data, got %v" , err )
77
+ if ! db .Migrator ().HasColumn (& Student {}, "NAME" ) {
78
+ t .Errorf ("Failed to get column: name" )
157
79
}
158
80
159
- CheckFolderData (t , createdFolder , folder )
160
-
161
- createdFolder .Properties [1 ].Value = "baz1"
162
- createdFolder .Properties = append (createdFolder .Properties , FolderProperty {
163
- ID : id ,
164
- Key : "foo3" ,
165
- Value : "bar3" ,
166
- })
167
- createdFolder .Properties = append (createdFolder .Properties , FolderProperty {
168
- ID : id ,
169
- Key : "foo4" ,
170
- Value : "bar4" ,
171
- })
172
- db .Save (& createdFolder )
173
-
174
- updatedFolder := FolderData {}
175
- if err := db .Model (& FolderData {}).Preload ("Properties" ).First (& updatedFolder ).Error ; err != nil {
176
- t .Errorf ("Failed to query data, got %v" , err )
81
+ student := Student {ID : 1 , Name : "John" }
82
+ if err := db .Model (& Student {}).Create (& student ).Error ; err != nil {
83
+ t .Errorf ("Failed to insert student, got %v" , err )
177
84
}
178
85
179
- CheckFolderData (t , updatedFolder , createdFolder )
180
-
181
- if err := db .Select (clause .Associations ).Delete (& createdFolder ).Error ; err != nil {
182
- t .Errorf ("Failed to delete data, got %v" , err )
86
+ var result Student
87
+ if err := db .First (& result ).Error ; err != nil {
88
+ t .Errorf ("Failed to query first student, got %v" , err )
183
89
}
184
90
185
- result := FolderData {}
186
- if err := db .Where ("folder_id = ?" , createdFolder .ID ).First (& result ).Error ; err == nil || ! errors .Is (err , gorm .ErrRecordNotFound ) {
187
- t .Errorf ("should returns record not found error, but got %v" , err )
91
+ if result .ID != student .ID {
92
+ t .Errorf ("id should be %v, but got %v" , student .ID , result .ID )
188
93
}
189
- }
190
-
191
- func CheckFolderData (t * testing.T , folderData FolderData , expect FolderData ) {
192
- tests .AssertObjEqual (t , folderData , expect , "ID" , "Name" )
193
- t .Run ("Properties" , func (t * testing.T ) {
194
- if len (folderData .Properties ) != len (expect .Properties ) {
195
- t .Fatalf ("properties should equal, expect: %v, got %v" , len (expect .Properties ), len (folderData .Properties ))
196
- }
197
-
198
- sort .Slice (folderData .Properties , func (i , j int ) bool {
199
- return folderData .Properties [i ].ID > folderData .Properties [j ].ID
200
- })
201
94
202
- sort .Slice (expect .Properties , func (i , j int ) bool {
203
- return expect .Properties [i ].ID > expect .Properties [j ].ID
204
- })
205
-
206
- for idx , property := range folderData .Properties {
207
- tests .AssertObjEqual (t , property , expect .Properties [idx ], "Seq" , "ID" , "Key" , "Value" )
208
- }
209
- })
95
+ if result .Name != student .Name {
96
+ t .Errorf ("name should be %v, but got %v" , student .Name , result .Name )
97
+ }
210
98
}
211
99
212
100
func TestSkipQuoteIdentifiersSQL (t * testing.T ) {
213
- type Student struct {
214
- ID uint
215
- Name string
216
- }
217
-
218
101
db , err := openTestDBWithOptions (
219
102
& oracle.Config {SkipQuoteIdentifiers : true },
220
103
& gorm.Config {Logger : newLogger })
@@ -226,34 +109,34 @@ func TestSkipQuoteIdentifiersSQL(t *testing.T) {
226
109
insertedStudent := Student {ID : 1 , Name : "John" }
227
110
result := dryrunDB .Model (& Student {}).Create (& insertedStudent )
228
111
229
- if ! regexp .MustCompile (`^INSERT INTO students \(name,id\) VALUES \(:1,:2\)$` ).MatchString (result .Statement .SQL .String ()) {
112
+ if ! regexp .MustCompile (`^INSERT INTO STUDENTS \(name,id\) VALUES \(:1,:2\)$` ).MatchString (result .Statement .SQL .String ()) {
230
113
t .Errorf ("invalid insert SQL, got %v" , result .Statement .SQL .String ())
231
114
}
232
115
233
116
// Test First
234
117
var firstStudent Student
235
118
result = dryrunDB .First (& firstStudent )
236
119
237
- if ! regexp .MustCompile (`^SELECT \* FROM students ORDER BY students \.id FETCH NEXT 1 ROW ONLY$` ).MatchString (result .Statement .SQL .String ()) {
120
+ if ! regexp .MustCompile (`^SELECT \* FROM STUDENTS ORDER BY STUDENTS \.id FETCH NEXT 1 ROW ONLY$` ).MatchString (result .Statement .SQL .String ()) {
238
121
t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
239
122
}
240
123
241
124
// Test Find
242
125
var foundStudent Student
243
126
result = dryrunDB .Find (foundStudent , "id = ?" , insertedStudent .ID )
244
- if ! regexp .MustCompile (`^SELECT \* FROM students WHERE id = :1$` ).MatchString (result .Statement .SQL .String ()) {
127
+ if ! regexp .MustCompile (`^SELECT \* FROM STUDENTS WHERE id = :1$` ).MatchString (result .Statement .SQL .String ()) {
245
128
t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
246
129
}
247
130
248
131
// Test Save
249
132
result = dryrunDB .Save (& Student {ID : 2 , Name : "Mary" })
250
- if ! regexp .MustCompile (`^UPDATE students SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
133
+ if ! regexp .MustCompile (`^UPDATE STUDENTS SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
251
134
t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
252
135
}
253
136
254
137
// Update with conditions
255
138
result = dryrunDB .Model (& Student {}).Where ("id = ?" , 1 ).Update ("name" , "hello" )
256
- if ! regexp .MustCompile (`^UPDATE students SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
139
+ if ! regexp .MustCompile (`^UPDATE STUDENTS SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
257
140
t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
258
141
}
259
142
}
0 commit comments