Skip to content

Fix miscellaneous tests, and remove passed-tests.txt #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jobs:
run-tests:
runs-on: ubuntu-latest
env:
DEBUG: "true"
GORM_ORACLEDB_USER: ${{ secrets.GORM_ORACLEDB_USER }}
GORM_ORACLEDB_PASSWORD: ${{ secrets.GORM_ORACLEDB_PASSWORD }}
GORM_ORACLEDB_CONNECTSTRING: ${{ secrets.GORM_ORACLEDB_CONNECTSTRING }}
Expand Down Expand Up @@ -47,4 +48,4 @@ jobs:
cd tests
go get -t github.com/oracle-samples/gorm-oracle/tests
go get .
go test -failfast
go test -failfast
6 changes: 2 additions & 4 deletions tests/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func TestCreateFromMap(t *testing.T) {
}

func TestCreateWithAssociations(t *testing.T) {
t.Skip()
user := *GetUser("create_with_associations", Config{
Account: true,
Pets: 2,
Expand All @@ -200,7 +199,7 @@ func TestCreateWithAssociations(t *testing.T) {
CheckUser(t, user, user)

var user2 User
DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").Find(&user2, "id = ?", user.ID)
DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").Find(&user2, "\"id\" = ?", user.ID)
CheckUser(t, user2, user)
}

Expand Down Expand Up @@ -265,7 +264,6 @@ func TestBulkCreatePtrDataWithAssociations(t *testing.T) {
}

func TestPolymorphicHasOne(t *testing.T) {
t.Skip()
t.Run("Struct", func(t *testing.T) {
pet := Pet{
Name: "PolymorphicHasOne",
Expand All @@ -279,7 +277,7 @@ func TestPolymorphicHasOne(t *testing.T) {
CheckPet(t, pet, pet)

var pet2 Pet
DB.Preload("Toy").Find(&pet2, "id = ?", pet.ID)
DB.Preload("Toy").Find(&pet2, "\"id\" = ?", pet.ID)
CheckPet(t, pet2, pet)
})

Expand Down
16 changes: 9 additions & 7 deletions tests/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,14 @@ func (s Product2) BeforeCreate(tx *gorm.DB) (err error) {
}

func (s *Product2) BeforeUpdate(tx *gorm.DB) (err error) {
tx.Statement.Where("\"owner\" != ?", "admin")
// In Oracle DB, when owner is NULL, the condition "owner" != 'admin' will
// always returns false.
// Hence, we use NVL("owner", 'none') to substitute 'none' when owner is NULL.
tx.Statement.Where("NVL(\"owner\", 'none') != ?", "admin")
return
}

func TestUseDBInHooks(t *testing.T) {
t.Skip()
DB.Migrator().DropTable(&Product2{})
DB.AutoMigrate(&Product2{})

Expand All @@ -296,12 +298,12 @@ func TestUseDBInHooks(t *testing.T) {
}

var result Product2
if err := DB.First(&result, "name = ?", "Nice").Error; err != nil {
if err := DB.First(&result, "\"name\" = ?", "Nice").Error; err != nil {
t.Fatalf("Failed to query product, got error: %v", err)
}

var resultClone Product2
if err := DB.First(&resultClone, "name = ?", "Nice_clone").Error; err != nil {
if err := DB.First(&resultClone, "\"name\" = ?", "Nice_clone").Error; err != nil {
t.Fatalf("Failed to find cloned product, got error: %v", err)
}

Expand All @@ -311,7 +313,7 @@ func TestUseDBInHooks(t *testing.T) {

DB.Model(&result).Update("Price", 500)
var result2 Product2
DB.First(&result2, "name = ?", "Nice")
DB.First(&result2, "\"name\" = ?", "Nice")

if result2.Price != 500 {
t.Errorf("Failed to update product's price, expects: %v, got %v", 500, result2.Price)
Expand All @@ -323,13 +325,13 @@ func TestUseDBInHooks(t *testing.T) {
}

var result3 Product2
if err := DB.First(&result3, "name = ?", "Nice2").Error; err != nil {
if err := DB.First(&result3, "\"name\" = ?", "Nice2").Error; err != nil {
t.Fatalf("Failed to query product, got error: %v", err)
}

DB.Model(&result3).Update("Price", 800)
var result4 Product2
DB.First(&result4, "name = ?", "Nice2")
DB.First(&result4, "\"name\" = ?", "Nice2")

if result4.Price != 600 {
t.Errorf("Admin product's price should not be changed, expects: %v, got %v", 600, result4.Price)
Expand Down
15 changes: 9 additions & 6 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@
package tests

import (
"fmt"
"strings"
"testing"
"time"
"strings"

. "github.com/oracle-samples/gorm-oracle/tests/utils"
)
Expand All @@ -54,9 +53,6 @@ func TestExceptionsWithInvalidSql(t *testing.T) {
t.Errorf("Should got error with invalid SQL")
}

tx := DB.Model(&User{}).Where("name = ?", "sd;;;aa").Pluck("name", &columns)
fmt.Println(tx.Error)

if DB.Model(&User{}).Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
t.Errorf("Should got error with invalid SQL")
}
Expand All @@ -65,13 +61,20 @@ func TestExceptionsWithInvalidSql(t *testing.T) {
t.Errorf("Should got error with invalid SQL")
}

// Create a user
user := *GetUser("user-for-test-exceptions-with-invalid-sql", Config{})

if results := DB.Create(&user); results.Error != nil {
t.Errorf("errors happened when create: %v", results.Error)
}

var count1, count2 int64
DB.Model(&User{}).Count(&count1)
if count1 <= 0 {
t.Errorf("Should find some users")
}

if DB.Where("name = ?", "jinzhu; delete * from users").First(&User{}).Error == nil {
if DB.Where("\"name\" = ?", "jinzhu; delete * from users").First(&User{}).Error == nil {
t.Errorf("Should got error with invalid SQL")
}

Expand Down
Loading
Loading