3939package  tests
4040
4141import  (
42+ 	"errors" 
4243	"regexp" 
44+ 	"sort" 
4345	"testing" 
4446
47+ 	"github.com/google/uuid" 
4548	"github.com/oracle-samples/gorm-oracle/oracle" 
4649	"gorm.io/gorm" 
50+ 	"gorm.io/gorm/clause" 
51+ 	"gorm.io/gorm/utils/tests" 
4752)
4853
49- type  Student  struct  {
50- 	ID    uint 
51- 	Name  string 
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"` 
5258}
5359
54- func  (s   Student ) TableName () string  {
55- 	return  "STUDENTS " 
60+ func  (FolderData ) TableName () string  {
61+ 	return  "folder_data " 
5662}
5763
58- func  TestSkipQuoteIdentifiers (t  * testing.T ) {
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 ) {
5976	db , err  :=  openTestDBWithOptions (
6077		& oracle.Config {SkipQuoteIdentifiers : true },
6178		& gorm.Config {Logger : newLogger })
6279	if  err  !=  nil  {
6380		t .Fatalf ("failed to connect database, got error %v" , err )
6481	}
6582
66- 	db .Migrator ().DropTable (& Student {})
67- 	db .Migrator ().CreateTable (& Student {})
83+ 	db .Migrator ().DropTable (& FolderData {},  & FolderProperty {})
84+ 	db .Migrator ().CreateTable (& FolderData {},  & FolderProperty {})
6885
69- 	if  ! db .Migrator ().HasTable (& Student {}) {
70- 		t .Errorf ("Failed to get table: student" )
86+ 	folderDataTN  :=  "FOLDER_DATA" 
87+ 	if  ! db .Migrator ().HasTable (folderDataTN ) {
88+ 		t .Errorf ("Failed to get table: %s" , folderDataTN )
7189	}
7290
73- 	if  ! db .Migrator ().HasColumn (& Student {} , "ID " ) {
74- 		t .Errorf ("Failed to get column: id " )
91+ 	if  ! db .Migrator ().HasColumn (folderDataTN , "FOLDER_ID " ) {
92+ 		t .Errorf ("Failed to get column: FOLDER_ID " )
7593	}
7694
77- 	if  ! db .Migrator ().HasColumn (& Student {} , "NAME " ) {
78- 		t .Errorf ("Failed to get column: name " )
95+ 	if  ! db .Migrator ().HasColumn (folderDataTN , "FOLDER_NM " ) {
96+ 		t .Errorf ("Failed to get column: FOLDER_NM " )
7997	}
8098
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 )
99+ 	folderPropertyTN  :=  "FOLDER_PROPERTY" 
100+ 	if  ! db .Migrator (). HasTable ( folderPropertyTN )  {
101+ 		t .Errorf ("Failed to get table: %s " , folderPropertyTN )
84102	}
85103
86- 	var  result  Student 
87- 	if  err  :=  db .First (& result ).Error ; err  !=  nil  {
88- 		t .Errorf ("Failed to query first student, got %v" , err )
104+ 	if  ! db .Migrator ().HasColumn (folderPropertyTN , "SEQ" ) {
105+ 		t .Errorf ("Failed to get column: SEQ" )
89106	}
90107
91- 	if  result . ID   !=   student . ID  {
92- 		t .Errorf ("id should be %v, but got %v"  ,  student . ID ,  result . ID )
108+ 	if  ! db . Migrator (). HasColumn ( folderPropertyTN ,  "FOLDER_ID" )  {
109+ 		t .Errorf ("Failed to get column: FOLDER_ID"  )
93110	}
94111
95- 	if  result .Name  !=  student .Name  {
96- 		t .Errorf ("name should be %v, but got %v" , student .Name , result .Name )
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" )
97118	}
98119}
99120
121+ func  TestSkipQuoteIdentifiers (t  * testing.T ) {
122+ 	db , err  :=  openTestDBWithOptions (
123+ 		& oracle.Config {SkipQuoteIdentifiers : true },
124+ 		& gorm.Config {Logger : newLogger })
125+ 	if  err  !=  nil  {
126+ 		t .Fatalf ("failed to connect database, got error %v" , err )
127+ 	}
128+ 
129+ 	db .Migrator ().DropTable (& FolderData {}, & FolderProperty {})
130+ 	db .Migrator ().CreateTable (& FolderData {}, & FolderProperty {})
131+ 
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+ 		},
148+ 	}
149+ 
150+ 	if  err  :=  db .Create (& folder ).Error ; err  !=  nil  {
151+ 		t .Errorf ("Failed to insert data, got %v" , err )
152+ 	}
153+ 
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 )
157+ 	}
158+ 
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 )
177+ 	}
178+ 
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 )
183+ 	}
184+ 
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 )
188+ 	}
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+ 
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+ 	})
210+ }
211+ 
100212func  TestSkipQuoteIdentifiersSQL (t  * testing.T ) {
213+ 	type  Student  struct  {
214+ 		ID    uint 
215+ 		Name  string 
216+ 	}
217+ 
101218	db , err  :=  openTestDBWithOptions (
102219		& oracle.Config {SkipQuoteIdentifiers : true },
103220		& gorm.Config {Logger : newLogger })
@@ -109,34 +226,34 @@ func TestSkipQuoteIdentifiersSQL(t *testing.T) {
109226	insertedStudent  :=  Student {ID : 1 , Name : "John" }
110227	result  :=  dryrunDB .Model (& Student {}).Create (& insertedStudent )
111228
112- 	if  ! regexp .MustCompile (`^INSERT INTO STUDENTS  \(name,id\) VALUES \(:1,:2\)$` ).MatchString (result .Statement .SQL .String ()) {
229+ 	if  ! regexp .MustCompile (`^INSERT INTO students  \(name,id\) VALUES \(:1,:2\)$` ).MatchString (result .Statement .SQL .String ()) {
113230		t .Errorf ("invalid insert SQL, got %v" , result .Statement .SQL .String ())
114231	}
115232
116233	// Test First 
117234	var  firstStudent  Student 
118235	result  =  dryrunDB .First (& firstStudent )
119236
120- 	if  ! regexp .MustCompile (`^SELECT \* FROM STUDENTS  ORDER BY STUDENTS \.id FETCH NEXT 1 ROW ONLY$` ).MatchString (result .Statement .SQL .String ()) {
237+ 	if  ! regexp .MustCompile (`^SELECT \* FROM students  ORDER BY students \.id FETCH NEXT 1 ROW ONLY$` ).MatchString (result .Statement .SQL .String ()) {
121238		t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
122239	}
123240
124241	// Test Find 
125242	var  foundStudent  Student 
126243	result  =  dryrunDB .Find (foundStudent , "id = ?" , insertedStudent .ID )
127- 	if  ! regexp .MustCompile (`^SELECT \* FROM STUDENTS  WHERE id = :1$` ).MatchString (result .Statement .SQL .String ()) {
244+ 	if  ! regexp .MustCompile (`^SELECT \* FROM students  WHERE id = :1$` ).MatchString (result .Statement .SQL .String ()) {
128245		t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
129246	}
130247
131248	// Test Save 
132249	result  =  dryrunDB .Save (& Student {ID : 2 , Name : "Mary" })
133- 	if  ! regexp .MustCompile (`^UPDATE STUDENTS  SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
250+ 	if  ! regexp .MustCompile (`^UPDATE students  SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
134251		t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
135252	}
136253
137254	// Update with conditions 
138255	result  =  dryrunDB .Model (& Student {}).Where ("id = ?" , 1 ).Update ("name" , "hello" )
139- 	if  ! regexp .MustCompile (`^UPDATE STUDENTS  SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
256+ 	if  ! regexp .MustCompile (`^UPDATE students  SET name=:1 WHERE id = :2$` ).MatchString (result .Statement .SQL .String ()) {
140257		t .Fatalf ("SQL should include selected names, but got %v" , result .Statement .SQL .String ())
141258	}
142259}
0 commit comments