better conditional handling of ora reserved words in tests
This commit is contained in:
parent
c59387a25a
commit
6ad6d29578
@ -792,7 +792,12 @@ func TestRelated(t *testing.T) {
|
||||
|
||||
var creditcard CreditCard
|
||||
var user3 User
|
||||
DB.First(&creditcard, `"number" = ?`, "1234567890") // you have to properly quote number as a reserved word
|
||||
numberIs := "number = ?"
|
||||
if isOra(DB) {
|
||||
numberIs = `"number" = ?`
|
||||
}
|
||||
|
||||
DB.First(&creditcard, numberIs, "1234567890") // you have to properly quote number as a reserved word
|
||||
DB.Model(&creditcard).Related(&user3)
|
||||
if user3.Id != user.Id || user3.Name != user.Name {
|
||||
t.Errorf("Should get user from credit card correctly")
|
||||
|
@ -76,16 +76,21 @@ func TestSoftDeleteWithCustomizedDeletedAtColumnName(t *testing.T) {
|
||||
t.Errorf("CreditCard's DeletedAt's column name should be `deleted_time`")
|
||||
}
|
||||
|
||||
if DB.First(&CreditCard{}, `"number" = ?`, creditCard.Number).Error == nil {
|
||||
numberIs := "number = ?"
|
||||
if isOra(DB) {
|
||||
numberIs = `"number" = ?`
|
||||
}
|
||||
|
||||
if DB.First(&CreditCard{}, numberIs, creditCard.Number).Error == nil {
|
||||
t.Errorf("Can't find a soft deleted record")
|
||||
}
|
||||
|
||||
if err := DB.Unscoped().First(&CreditCard{}, `"number" = ?`, creditCard.Number).Error; err != nil {
|
||||
if err := DB.Unscoped().First(&CreditCard{}, numberIs, creditCard.Number).Error; err != nil {
|
||||
t.Errorf("Should be able to find soft deleted record with Unscoped, but err=%s", err)
|
||||
}
|
||||
|
||||
DB.Unscoped().Delete(&creditCard)
|
||||
if !DB.Unscoped().First(&CreditCard{}, `"number" = ?`, creditCard.Number).RecordNotFound() {
|
||||
if !DB.Unscoped().First(&CreditCard{}, numberIs, creditCard.Number).RecordNotFound() {
|
||||
t.Errorf("Can't find permanently deleted record")
|
||||
}
|
||||
}
|
||||
|
10
main_test.go
10
main_test.go
@ -772,19 +772,23 @@ func TestJoins(t *testing.T) {
|
||||
}
|
||||
|
||||
var users3 []User
|
||||
DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins("join credit_cards on credit_cards.user_id = users.id AND credit_cards.\"number\" = ?", "411111111111").Where("name = ?", "joins").First(&users3)
|
||||
join := "join credit_cards on credit_cards.user_id = users.id AND credit_cards.number = ?"
|
||||
if isOra(DB) {
|
||||
join = "join credit_cards on credit_cards.user_id = users.id AND credit_cards.\"number\" = ?"
|
||||
}
|
||||
DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins(join, "411111111111").Where("name = ?", "joins").First(&users3)
|
||||
if len(users3) != 1 {
|
||||
t.Errorf("should find one users using multiple left join conditions")
|
||||
}
|
||||
|
||||
var users4 []User
|
||||
DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins("join credit_cards on credit_cards.user_id = users.id AND credit_cards.\"number\" = ?", "422222222222").Where("name = ?", "joins").First(&users4)
|
||||
DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins(join, "422222222222").Where("name = ?", "joins").First(&users4)
|
||||
if len(users4) != 0 {
|
||||
t.Errorf("should find no user when searching with unexisting credit card")
|
||||
}
|
||||
|
||||
var users5 []User
|
||||
db5 := DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins("join credit_cards on credit_cards.user_id = users.id AND credit_cards.\"number\" = ?", "411111111111").Where(User{Id: 1}).Where(Email{Id: 1}).Not(Email{Id: 10}).First(&users5)
|
||||
db5 := DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins(join, "411111111111").Where(User{Id: 1}).Where(Email{Id: 1}).Not(Email{Id: 10}).First(&users5)
|
||||
if db5.Error != nil {
|
||||
t.Errorf("Should not raise error for join where identical fields in different tables. Error: %s", db5.Error.Error())
|
||||
}
|
||||
|
@ -760,8 +760,11 @@ func TestFindOrCreate(t *testing.T) {
|
||||
if DB.Where("email = ?", "jinzhu-2@assign_embedded_struct.com").First(&Email{}).RecordNotFound() {
|
||||
t.Errorf("embedded struct email should be saved")
|
||||
}
|
||||
|
||||
if DB.Where("\"number\" = ?", "1231231231").First(&CreditCard{}).RecordNotFound() {
|
||||
numberIs := "number = ?"
|
||||
if isOra(DB) {
|
||||
numberIs = "\"number\" = ?"
|
||||
}
|
||||
if DB.Where(numberIs, "1231231231").First(&CreditCard{}).RecordNotFound() {
|
||||
t.Errorf("embedded struct credit card should be saved")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user