3939package tests
4040
4141import (
42- "errors"
4342 "regexp"
44- "sort"
4543 "testing"
4644
47- "github.com/google/uuid"
4845 "github.com/oracle-samples/gorm-oracle/oracle"
4946 "gorm.io/gorm"
50- "gorm.io/gorm/clause"
51- "gorm.io/gorm/utils/tests"
5247)
5348
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
5852}
5953
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"
11956}
12057
12158func TestSkipQuoteIdentifiers (t * testing.T ) {
@@ -126,95 +63,41 @@ func TestSkipQuoteIdentifiers(t *testing.T) {
12663 t .Fatalf ("failed to connect database, got error %v" , err )
12764 }
12865
129- db .Migrator ().DropTable (& FolderData {}, & FolderProperty {})
130- db .Migrator ().CreateTable (& FolderData {}, & FolderProperty {})
66+ db .Migrator ().DropTable (& Student {})
67+ db .Migrator ().CreateTable (& Student {})
13168
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" )
14871 }
14972
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" )
15275 }
15376
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" )
15779 }
15880
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 )
17784 }
17885
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 )
18389 }
18490
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 )
18893 }
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- })
20194
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+ }
21098}
21199
212100func TestSkipQuoteIdentifiersSQL (t * testing.T ) {
213- type Student struct {
214- ID uint
215- Name string
216- }
217-
218101 db , err := openTestDBWithOptions (
219102 & oracle.Config {SkipQuoteIdentifiers : true },
220103 & gorm.Config {Logger : newLogger })
@@ -226,34 +109,34 @@ func TestSkipQuoteIdentifiersSQL(t *testing.T) {
226109 insertedStudent := Student {ID : 1 , Name : "John" }
227110 result := dryrunDB .Model (& Student {}).Create (& insertedStudent )
228111
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 ()) {
230113 t .Errorf ("invalid insert SQL, got %v" , result .Statement .SQL .String ())
231114 }
232115
233116 // Test First
234117 var firstStudent Student
235118 result = dryrunDB .First (& firstStudent )
236119
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 ()) {
238121 t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
239122 }
240123
241124 // Test Find
242125 var foundStudent Student
243126 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 ()) {
245128 t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
246129 }
247130
248131 // Test Save
249132 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 ()) {
251134 t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
252135 }
253136
254137 // Update with conditions
255138 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 ()) {
257140 t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
258141 }
259142}
0 commit comments