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 14, 2024
1 parent e5208c4 commit 5719e54
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ gorm
go.sum
.*
*.db
go-sqlcmd
sqlcmd
24 changes: 13 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ module gorm.io/playground
go 1.20

require (
gorm.io/driver/mysql v1.5.1
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/driver/sqlserver v1.5.1
gorm.io/gorm v1.25.4
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
gorm.io/driver/sqlite v1.5.4
gorm.io/driver/sqlserver v1.5.2
gorm.io/gorm v1.25.5
)

require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.2 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

replace gorm.io/gorm => ./gorm
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())
}
})
}
}
23 changes: 19 additions & 4 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,25 @@ if [[ -z $GITHUB_ACTION ]]; then
if [[ $(uname -a) == *" arm64" ]]; then
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker-compose up --detach --quiet-pull || true
echo "starting"
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" > /dev/null || true
if [[ $(which sqlcmd) == "" ]] && ! [ -x "./sqlcmd" ]; then
echo "sqlcmd not found; installing"
git clone --depth 1 https://github.com/microsoft/go-sqlcmd || true
cd go-sqlcmd
go build -o ../sqlcmd ./cmd/modern
cd ..
sqlcmd() {
"./sqlcmd" "$@"
}
else
if [ -x "./sqlcmd" ]; then
sqlcmd() {
"./sqlcmd" "$@"
}
fi
fi
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" >/dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" >/dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" >/dev/null || true
else
docker-compose up --detach --quiet-pull
echo "starting..."
Expand Down

0 comments on commit 5719e54

Please sign in to comment.