Skip to content

Commit

Permalink
add TestMapColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
molon committed Jun 22, 2024
1 parent 427f924 commit be276ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ func (stmt *Statement) clone() *Statement {
Distinct: stmt.Distinct,
Selects: stmt.Selects,
Omits: stmt.Omits,
ColumnMapping: stmt.ColumnMapping,
Preloads: map[string][]interface{}{},
ConnPool: stmt.ConnPool,
Schema: stmt.Schema,
Expand Down
24 changes: 22 additions & 2 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,28 @@ func TestOmitWithAllFields(t *testing.T) {
}
}

func TestMapColumns(t *testing.T) {
user := User{Name: "MapColumnsUser", Age: 12}
DB.Save(&user)

type result struct {
Name string
Nickname string
Age uint
}
var res result
DB.Table("users").Where("name = ?", user.Name).MapColumns(map[string]string{"name": "nickname"}).Scan(&res)
if res.Nickname != user.Name {
t.Errorf("Expected res.Nickname to be %s, but got %s", user.Name, res.Nickname)
}
if res.Name != "" {
t.Errorf("Expected res.Name to be empty, but got %s", res.Name)
}
if res.Age != user.Age {
t.Errorf("Expected res.Age to be %d, but got %d", user.Age, res.Age)
}
}

func TestPluckWithSelect(t *testing.T) {
users := []User{
{Name: "pluck_with_select_1", Age: 25},
Expand Down Expand Up @@ -1189,7 +1211,6 @@ func TestSubQueryWithRaw(t *testing.T) {
Where("age >= ? and name in (?)", 20, []string{"subquery_raw_1", "subquery_raw_3"}).
Group("name"),
).Count(&count).Error

if err != nil {
t.Errorf("Expected to get no errors, but got %v", err)
}
Expand All @@ -1205,7 +1226,6 @@ func TestSubQueryWithRaw(t *testing.T) {
Not("age <= ?", 10).Not("name IN (?)", []string{"subquery_raw_1", "subquery_raw_3"}).
Group("name"),
).Count(&count).Error

if err != nil {
t.Errorf("Expected to get no errors, but got %v", err)
}
Expand Down

0 comments on commit be276ba

Please sign in to comment.