Skip to content
Open
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
25 changes: 21 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@ package main

import (
"testing"
"gorm.io/gorm"
)

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


func OrderByScope(db *gorm.DB) *gorm.DB {
return db.Order("name desc")
}

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

DB.Create(&user)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
var count int64

t.Run("ORDER BY + Count() works fine normally", func(t *testing.T) {
if err := DB.Model(&User{}).Order("name desc").Count(&count).Error; err != nil {
t.Errorf("Failed in normal .Order(), got error: %v", err)
}
t.Logf("Count: %d", count)
})

t.Run("ORDER BY in scope + Count() fails", func(t *testing.T) {
if err := DB.Model(&User{}).Scopes(OrderByScope).Count(&count).Error; err != nil {
t.Errorf("Failed with .Order() in a scope, got error: %v", err)
}
t.Logf("Count: %d", count)
})
}
Loading