Skip to content

Commit 610a82e

Browse files
committed
move TestSaveAssociationWithAutoIncrementField to association_test.go
1 parent 177b019 commit 610a82e

File tree

4 files changed

+128
-146
lines changed

4 files changed

+128
-146
lines changed

tests/associations_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
package tests
4040

4141
import (
42+
"errors"
4243
"strings"
4344
"testing"
4445

46+
"github.com/google/uuid"
4547
. "github.com/oracle-samples/gorm-oracle/tests/utils"
4648

4749
"gorm.io/gorm"
@@ -543,3 +545,66 @@ func TestBasicManyToManyAssociation(t *testing.T) {
543545

544546
AssertAssociationCount(t, user, "Languages", 0, "after clear")
545547
}
548+
549+
func TestSaveAssociationWithAutoIncrementField(t *testing.T) {
550+
DB.Migrator().DropTable(&FolderData{}, &FolderProperty{})
551+
DB.Migrator().CreateTable(&FolderData{}, &FolderProperty{})
552+
553+
id := uuid.New().String()
554+
folder := FolderData{
555+
ID: id,
556+
Name: "My Folder",
557+
Properties: []FolderProperty{
558+
{
559+
ID: id,
560+
Key: "foo1",
561+
Value: "bar1",
562+
},
563+
{
564+
ID: id,
565+
Key: "foo2",
566+
Value: "bar2",
567+
},
568+
},
569+
}
570+
571+
if err := DB.Create(&folder).Error; err != nil {
572+
t.Errorf("Failed to insert data, got %v", err)
573+
}
574+
575+
createdFolder := FolderData{}
576+
if err := DB.Model(&FolderData{}).Preload("Properties").First(&createdFolder).Error; err != nil {
577+
t.Errorf("Failed to query data, got %v", err)
578+
}
579+
580+
CheckFolderData(t, createdFolder, folder)
581+
582+
createdFolder.Properties[1].Value = "baz1"
583+
createdFolder.Properties = append(createdFolder.Properties, FolderProperty{
584+
ID: id,
585+
Key: "foo3",
586+
Value: "bar3",
587+
})
588+
createdFolder.Properties = append(createdFolder.Properties, FolderProperty{
589+
ID: id,
590+
Key: "foo4",
591+
Value: "bar4",
592+
})
593+
DB.Save(&createdFolder)
594+
595+
updatedFolder := FolderData{}
596+
if err := DB.Model(&FolderData{}).Preload("Properties").First(&updatedFolder).Error; err != nil {
597+
t.Errorf("Failed to query data, got %v", err)
598+
}
599+
600+
CheckFolderData(t, updatedFolder, createdFolder)
601+
602+
if err := DB.Select(clause.Associations).Delete(&createdFolder).Error; err != nil {
603+
t.Errorf("Failed to delete data, got %v", err)
604+
}
605+
606+
result := FolderData{}
607+
if err := DB.Where("\"folder_id\" = ?", createdFolder.ID).First(&result).Error; err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
608+
t.Errorf("should returns record not found error, but got %v", err)
609+
}
610+
}

tests/config_test.go

Lines changed: 29 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -39,83 +39,20 @@
3939
package tests
4040

4141
import (
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

12158
func 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

212100
func 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
}

tests/helper_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,24 @@ func db(unscoped bool) *gorm.DB {
357357
return DB
358358
}
359359
}
360+
361+
func CheckFolderData(t *testing.T, folderData FolderData, expect FolderData) {
362+
tests.AssertObjEqual(t, folderData, expect, "ID", "Name")
363+
t.Run("Properties", func(t *testing.T) {
364+
if len(folderData.Properties) != len(expect.Properties) {
365+
t.Fatalf("properties should equal, expect: %v, got %v", len(expect.Properties), len(folderData.Properties))
366+
}
367+
368+
sort.Slice(folderData.Properties, func(i, j int) bool {
369+
return folderData.Properties[i].ID > folderData.Properties[j].ID
370+
})
371+
372+
sort.Slice(expect.Properties, func(i, j int) bool {
373+
return expect.Properties[i].ID > expect.Properties[j].ID
374+
})
375+
376+
for idx, property := range folderData.Properties {
377+
tests.AssertObjEqual(t, property, expect.Properties[idx], "Seq", "ID", "Key", "Value")
378+
}
379+
})
380+
}

tests/utils/models.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,16 @@ type Child struct {
102102
ParentID *uint
103103
Parent *Parent
104104
}
105+
106+
type FolderProperty struct {
107+
Seq uint64 `gorm:"autoIncrement"`
108+
ID string `gorm:"primaryKey;column:folder_id"`
109+
Key string `gorm:"primaryKey;unique"`
110+
Value string
111+
}
112+
113+
type FolderData struct {
114+
ID string `gorm:"primaryKey;column:folder_id"`
115+
Name string `gorm:"column:folder_nm"`
116+
Properties []FolderProperty `gorm:"foreignKey:ID;PRELOAD:false"`
117+
}

0 commit comments

Comments
 (0)