-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathupdate_test.go
108 lines (95 loc) · 3.01 KB
/
update_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlchemy
import (
"testing"
"time"
)
func TestUpdateSQL(t *testing.T) {
SetupMockDatabaseBackend()
table := NewTableSpecFromStruct(TableStruct{}, "testtable")
dt := TableStruct{
Id: 12345,
Name: "john",
Age: 20,
IsMale: true,
}
session, err := table.PrepareUpdate(&dt)
if err != nil {
t.Fatalf("prepareUpdate fail %s", err)
}
dt.Name = "johny"
result, err := session.SaveUpdateSql(&dt)
if err != nil {
t.Fatalf("saveUpdateSql fail %s", err)
}
want := "UPDATE `testtable` SET `name` = ?, `version` = `version` + 1, `updated_at` = UTC_NOW() WHERE `id` = ?"
wantVars := 2
if want != result.Sql {
t.Fatalf("SQL: want %s got %s", want, result.Sql)
}
if wantVars != len(result.Vars) {
t.Fatalf("Vars want %d got %d", wantVars, len(result.Vars))
}
}
type SMetadata struct {
// 资源类型
// example: network
ObjType string `width:"40" charset:"ascii" index:"true" list:"user" get:"user"`
// 资源ID
// example: 87321a70-1ecb-422a-8b0c-c9aa632a46a7
ObjId string `width:"88" charset:"ascii" index:"true" list:"user" get:"user"`
// 资源组合ID
// example: network::87321a70-1ecb-422a-8b0c-c9aa632a46a7
Id string `width:"128" charset:"ascii" primary:"true" list:"user" get:"user"`
// 标签KEY
// exmaple: 部门
Key string `width:"64" charset:"utf8" primary:"true" list:"user" get:"user"`
// 标签值
// example: 技术部
Value string `charset:"utf8" list:"user" get:"user"`
// 更新时间
UpdatedAt time.Time `nullable:"false" updated_at:"true"`
// 是否被删除
Deleted bool `nullable:"false" default:"false" index:"true"`
}
func TestUpdatePrimaryKey(t *testing.T) {
SetupMockDatabaseBackend()
table := NewTableSpecFromStruct(SMetadata{}, "metadata_tbl")
dt := SMetadata{
ObjType: "server",
ObjId: "0911ae37-4bcd-4bdd-8942-1ab9a4280ab5",
Id: "server::0911ae37-4bcd-4bdd-8942-1ab9a4280ab5",
Key: "projname",
Value: "hwtest",
}
session, err := table.PrepareUpdate(&dt)
if err != nil {
t.Fatalf("prepareUpdate fail %s", err)
}
dt.Key = "projName"
dt.Value = "testhw"
result, err := session.SaveUpdateSql(&dt)
if err != nil {
t.Fatalf("saveUpdateSql fail %s", err)
}
want := "UPDATE `metadata_tbl` SET `key` = ?, `value` = ?, `updated_at` = UTC_NOW() WHERE `id` = ? AND `key` = ?"
wantVars := 4
if want != result.Sql {
t.Fatalf("SQL: want %s got %s", want, result.Sql)
}
if wantVars != len(result.Vars) {
t.Fatalf("Vars want %d got %d", wantVars, len(result.Vars))
}
}