-
Notifications
You must be signed in to change notification settings - Fork 25
/
insert_test.go
107 lines (96 loc) · 3.13 KB
/
insert_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
// 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"
)
const (
uuid = "bfaf21ec-861e-4a7d-8739-7139588f0e00"
)
type TableStruct struct {
Id int `json:"id" primary:"true"`
UserId string `width:"128" charset:"ascii" nullable:"false"`
Name string `width:"16"`
Age int `nullable:"true"`
IsMale bool `nullalbe:"true"`
CreatedAt time.Time `created_at:"true"`
UpdatedAt time.Time `updated_at:"true"`
Version int64 `auto_version:"true"`
}
func (s *TableStruct) BeforeInsert() {
s.UserId = uuid
}
func (s *TableStruct) BeforeUpdate() {
if len(s.Name) > 16 {
s.Name = s.Name[:14] + ".."
}
}
func TestInsertSQL(t *testing.T) {
SetupMockDatabaseBackend()
table := NewTableSpecFromStruct(TableStruct{}, "testtable")
value := TableStruct{
Id: 12345,
Name: "John",
Age: 20,
IsMale: true,
}
results, err := table.InsertSqlPrep(&value, false)
if err != nil {
t.Fatalf("insertSqlPref fail %s", err)
}
want := "INSERT INTO `testtable` (`id`, `user_id`, `name`, `age`, `is_male`, `created_at`, `updated_at`, `version`) VALUES (?, ?, ?, ?, ?, UTC_NOW(), UTC_NOW(), ?)"
wantVars := 6
if results.Sql != want {
t.Errorf("SQL: want %s got %s", want, results.Sql)
}
if len(results.Values) != wantVars {
t.Errorf("VARs want %d got %d", wantVars, len(results.Values))
}
if value.UserId != uuid {
t.Errorf("want %s got %s", uuid, value.UserId)
}
}
type TableStruct2 struct {
Id int `json:"id" primary:"true"`
UserId string `width:"128" charset:"ascii" nullable:"false"`
Name string `width:"16"`
Age int `nullable:"true"`
IsMale bool `nullalbe:"true"`
CreatedAt time.Time `created_at:"true"`
UpdatedAt time.Time `updated_at:"true"`
Version int64 `auto_version:"true"`
}
func TestInsertUpdateSQL(t *testing.T) {
SetupMockDatabaseBackend()
table := NewTableSpecFromStruct(TableStruct2{}, "testtable2")
value := TableStruct2{
Id: 12345,
Name: "John",
Age: 20,
IsMale: true,
}
results, err := table.InsertSqlPrep(&value, true)
if err != nil {
t.Fatalf("insertSqlPref fail %s", err)
}
want := "INSERT INTO `testtable2` (`id`, `name`, `age`, `is_male`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, UTC_NOW(), UTC_NOW()) ON DUPLICATE KEY UPDATE `user_id` = NULL, `name` = ?, `age` = ?, `is_male` = ?, `updated_at` = UTC_NOW(), `version` = `version` + 1"
wantVars := 7
if results.Sql != want {
t.Errorf("SQL: want %s got %s", want, results.Sql)
}
if len(results.Values) != wantVars {
t.Errorf("VARs want %d got %d", wantVars, len(results.Values))
}
}