Skip to content

Commit

Permalink
Issue example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonçalo Amaral committed Jan 12, 2025
1 parent fabf3ff commit e3f1e54
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,68 @@
package main

import (
"fmt"
"sync"
"testing"

"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}

DB.Create(&user)
var users []User
var id uint = 1
// Works with a value
if err := DB.Table("users").Where("id = ?", id).Find(&users).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
// Works with a pointer
if err := DB.Table("users").Where("id = ?", &id).Find(&users).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
ids := []uint{1, 2, 3}
// Works with a slice
if err := DB.Table("users").Where("id IN (?)", ids).Find(&users).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
// Does not work with a slice pointer
if err := DB.Table("users").Where("id IN (?)", &ids).Find(&users).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
}

// From gorm/clause/expression_test.go
func TestExpr(t *testing.T) {
ids := []uint{1, 2, 3}
results := []struct {
SQL string
Result string
Vars []interface{}
}{{
SQL: "SELECT * FROM users WHERE users.id IN (?)",
Vars: []interface{}{ids},
Result: "SELECT * FROM users WHERE users.id IN (?,?,?)", // Correctly expanded
}, {
SQL: "SELECT * FROM users WHERE users.id IN (?)",
Vars: []interface{}{&ids},
Result: "SELECT * FROM users WHERE users.id IN (?,?,?)", // Expected this to expand aswell
}}

for idx, result := range results {
t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
user, _ := schema.Parse(&tests.User{}, &sync.Map{}, DB.NamingStrategy)
stmt := &gorm.Statement{DB: DB, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}}
clause.Expr{SQL: result.SQL, Vars: result.Vars}.Build(stmt)
if stmt.SQL.String() != result.Result {
t.Errorf("generated SQL is not equal, expects %v, but got %v", result.Result, stmt.SQL.String())
}
})
}
}

0 comments on commit e3f1e54

Please sign in to comment.