Test NamedPolymorphic
This commit is contained in:
		
							parent
							
								
									6d555ef8d5
								
							
						
					
					
						commit
						aa959ec383
					
				| @ -34,7 +34,7 @@ func preload(db *gorm.DB, rels []*schema.Relationship, conds []interface{}) { | |||||||
| 				joinForeignFields = append(joinForeignFields, ref.ForeignKey) | 				joinForeignFields = append(joinForeignFields, ref.ForeignKey) | ||||||
| 				foreignFields = append(foreignFields, ref.PrimaryKey) | 				foreignFields = append(foreignFields, ref.PrimaryKey) | ||||||
| 			} else if ref.PrimaryValue != "" { | 			} else if ref.PrimaryValue != "" { | ||||||
| 				tx.Where(clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue}) | 				tx = tx.Where(clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue}) | ||||||
| 			} else { | 			} else { | ||||||
| 				joinRelForeignFields = append(joinRelForeignFields, ref.ForeignKey) | 				joinRelForeignFields = append(joinRelForeignFields, ref.ForeignKey) | ||||||
| 				relForeignKeys = append(relForeignKeys, ref.PrimaryKey.DBName) | 				relForeignKeys = append(relForeignKeys, ref.PrimaryKey.DBName) | ||||||
| @ -76,7 +76,7 @@ func preload(db *gorm.DB, rels []*schema.Relationship, conds []interface{}) { | |||||||
| 				relForeignFields = append(relForeignFields, ref.ForeignKey) | 				relForeignFields = append(relForeignFields, ref.ForeignKey) | ||||||
| 				foreignFields = append(foreignFields, ref.PrimaryKey) | 				foreignFields = append(foreignFields, ref.PrimaryKey) | ||||||
| 			} else if ref.PrimaryValue != "" { | 			} else if ref.PrimaryValue != "" { | ||||||
| 				tx.Where(clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue}) | 				tx = tx.Where(clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue}) | ||||||
| 			} else { | 			} else { | ||||||
| 				relForeignKeys = append(relForeignKeys, ref.PrimaryKey.DBName) | 				relForeignKeys = append(relForeignKeys, ref.PrimaryKey.DBName) | ||||||
| 				relForeignFields = append(relForeignFields, ref.PrimaryKey) | 				relForeignFields = append(relForeignFields, ref.PrimaryKey) | ||||||
|  | |||||||
							
								
								
									
										146
									
								
								tests/named_polymorphic_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								tests/named_polymorphic_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,146 @@ | |||||||
|  | package tests_test | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	. "github.com/jinzhu/gorm/tests" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type Hamster struct { | ||||||
|  | 	Id           int | ||||||
|  | 	Name         string | ||||||
|  | 	PreferredToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_preferred"` | ||||||
|  | 	OtherToy     Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_other"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestNamedPolymorphic(t *testing.T) { | ||||||
|  | 	DB.AutoMigrate(&Hamster{}) | ||||||
|  | 
 | ||||||
|  | 	hamster := Hamster{Name: "Mr. Hammond", PreferredToy: Toy{Name: "bike"}, OtherToy: Toy{Name: "treadmill"}} | ||||||
|  | 	DB.Save(&hamster) | ||||||
|  | 
 | ||||||
|  | 	hamster2 := Hamster{} | ||||||
|  | 	DB.Debug().Preload("PreferredToy").Preload("OtherToy").Find(&hamster2, hamster.Id) | ||||||
|  | 
 | ||||||
|  | 	if hamster2.PreferredToy.ID != hamster.PreferredToy.ID || hamster2.PreferredToy.Name != hamster.PreferredToy.Name { | ||||||
|  | 		t.Errorf("Hamster's preferred toy failed to preload") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if hamster2.OtherToy.ID != hamster.OtherToy.ID || hamster2.OtherToy.Name != hamster.OtherToy.Name { | ||||||
|  | 		t.Errorf("Hamster's other toy failed to preload") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// clear to omit Toy.ID in count
 | ||||||
|  | 	hamster2.PreferredToy = Toy{} | ||||||
|  | 	hamster2.OtherToy = Toy{} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 { | ||||||
|  | 		t.Errorf("Hamster's preferred toy count should be 1") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { | ||||||
|  | 		t.Errorf("Hamster's other toy count should be 1") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Query
 | ||||||
|  | 	hamsterToy := Toy{} | ||||||
|  | 	DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy) | ||||||
|  | 	if hamsterToy.Name != hamster.PreferredToy.Name { | ||||||
|  | 		t.Errorf("Should find has one polymorphic association") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	hamsterToy = Toy{} | ||||||
|  | 	DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy) | ||||||
|  | 	if hamsterToy.Name != hamster.OtherToy.Name { | ||||||
|  | 		t.Errorf("Should find has one polymorphic association") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Append
 | ||||||
|  | 	DB.Model(&hamster).Association("PreferredToy").Append(&Toy{ | ||||||
|  | 		Name: "bike 2", | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	DB.Model(&hamster).Association("OtherToy").Append(&Toy{ | ||||||
|  | 		Name: "treadmill 2", | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	hamsterToy = Toy{} | ||||||
|  | 	DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy) | ||||||
|  | 	if hamsterToy.Name != "bike 2" { | ||||||
|  | 		t.Errorf("Should update has one polymorphic association with Append") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	hamsterToy = Toy{} | ||||||
|  | 	DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy) | ||||||
|  | 	if hamsterToy.Name != "treadmill 2" { | ||||||
|  | 		t.Errorf("Should update has one polymorphic association with Append") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 { | ||||||
|  | 		t.Errorf("Hamster's toys count should be 1 after Append") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { | ||||||
|  | 		t.Errorf("Hamster's toys count should be 1 after Append") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Replace
 | ||||||
|  | 	DB.Model(&hamster).Association("PreferredToy").Replace(&Toy{ | ||||||
|  | 		Name: "bike 3", | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	DB.Model(&hamster).Association("OtherToy").Replace(&Toy{ | ||||||
|  | 		Name: "treadmill 3", | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	hamsterToy = Toy{} | ||||||
|  | 	DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy) | ||||||
|  | 	if hamsterToy.Name != "bike 3" { | ||||||
|  | 		t.Errorf("Should update has one polymorphic association with Replace") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	hamsterToy = Toy{} | ||||||
|  | 	DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy) | ||||||
|  | 	if hamsterToy.Name != "treadmill 3" { | ||||||
|  | 		t.Errorf("Should update has one polymorphic association with Replace") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 { | ||||||
|  | 		t.Errorf("hamster's toys count should be 1 after Replace") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { | ||||||
|  | 		t.Errorf("hamster's toys count should be 1 after Replace") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Clear
 | ||||||
|  | 	DB.Model(&hamster).Association("PreferredToy").Append(&Toy{ | ||||||
|  | 		Name: "bike 2", | ||||||
|  | 	}) | ||||||
|  | 	DB.Model(&hamster).Association("OtherToy").Append(&Toy{ | ||||||
|  | 		Name: "treadmill 2", | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster).Association("PreferredToy").Count() != 1 { | ||||||
|  | 		t.Errorf("Hamster's toys should be added with Append") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster).Association("OtherToy").Count() != 1 { | ||||||
|  | 		t.Errorf("Hamster's toys should be added with Append") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	DB.Model(&hamster).Association("PreferredToy").Clear() | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("PreferredToy").Count() != 0 { | ||||||
|  | 		t.Errorf("Hamster's preferred toy should be cleared with Clear") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { | ||||||
|  | 		t.Errorf("Hamster's other toy should be still available") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	DB.Model(&hamster).Association("OtherToy").Clear() | ||||||
|  | 	if DB.Model(&hamster).Association("OtherToy").Count() != 0 { | ||||||
|  | 		t.Errorf("Hamster's other toy should be cleared with Clear") | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu