-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_map_test.go
62 lines (56 loc) · 1.38 KB
/
value_map_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gormcnm
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/yyle88/gormcnm/internal/utils"
"gorm.io/gorm"
)
func TestValuesMap_SetValue(t *testing.T) {
type Example struct {
Name string `gorm:"primary_key;type:varchar(100);"`
Type string `gorm:"column:type;"`
Rank int `gorm:"column:rank;"`
}
const (
columnName = ColumnName[string]("name")
columnType = ColumnName[string]("type")
columnRank = ColumnName[int]("rank")
)
utils.CaseRunInMemDB(func(db *gorm.DB) {
require.NoError(t, db.AutoMigrate(&Example{}))
require.NoError(t, db.Save(&Example{
Name: "aaa",
Type: "xxx",
Rank: 123,
}).Error)
require.NoError(t, db.Save(&Example{
Name: "bbb",
Type: "yyy",
Rank: 456,
}).Error)
{
result := db.Model(&Example{}).Where(
Qx(columnName.Eq("aaa")).
AND(
Qx(columnType.Eq("xxx")),
Qx(columnRank.Eq(123)),
).Qx3(),
).UpdateColumns(columnRank.Kw(100).Kw(columnType.Kv("zzz")).Kws())
require.NoError(t, result.Error)
require.Equal(t, int64(1), result.RowsAffected)
}
{
result := db.Model(&Example{}).Where(
Qx(
columnName.Eq("bbb"),
).AND1(
columnType.Eq("yyy"),
).AND1(
columnRank.Eq(456),
).Qx3(),
).UpdateColumns(columnRank.Kw(200).Kw(columnType.Kv("www")).Map())
require.NoError(t, result.Error)
require.Equal(t, int64(1), result.RowsAffected)
}
})
}