/
model551_test.go
170 lines (142 loc) · 6.13 KB
/
model551_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package model551_test
import (
"github.com/go51/model551"
"testing"
)
func TestLoad(t *testing.T) {
m1 := model551.Load()
m2 := model551.Load()
if m1 == nil {
t.Errorf("インスタンスの生成に失敗しました。")
}
if m2 == nil {
t.Errorf("インスタンスの生成に失敗しました。")
}
}
func BenchmarkLoad(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = model551.Load()
}
}
type SampleModel struct {
Id int64
Name string
Description string
}
type SampleModelTableInfo struct {
Key int64 `db_table:"table_information" db_pk:"true" db:"id"`
Name string `db:"-"`
Description string
DeletedAt string `db_delete:"true"`
}
func NewSampleModel() interface{} {
return SampleModel{}
}
func NewSampleModelPointer() interface{} {
return &SampleModel{}
}
func NewSampleModelTableInfo() interface{} {
return SampleModelTableInfo{}
}
func TestAdd(t *testing.T) {
m := model551.Load()
m.Add(NewSampleModel, NewSampleModelPointer)
detail := m.Get("SampleModel")
if detail.ModelName != "SampleModel" {
t.Errorf("モデルの保持に失敗しました。")
}
if detail.ModelType.Name() != "SampleModel" {
t.Errorf("モデルの保持に失敗しました。")
}
}
func TestTableInfo(t *testing.T) {
m := model551.Load()
m.Add(NewSampleModelTableInfo, NewSampleModelPointer)
mSampleModel := m.Get("SampleModel")
mSampleModelTableInfo := m.Get("SampleModelTableInfo")
// table name
if mSampleModel.TableInformation.TableName != "sample_model" {
t.Errorf("テーブル名の解析に失敗しました。\n\"sample_model\" => \"%s\"\n", mSampleModel.TableInformation.TableName)
}
if mSampleModelTableInfo.TableInformation.TableName != "table_information" {
t.Errorf("テーブル名の解析に失敗しました。\n\"table_information\" => \"%s\"\n", mSampleModelTableInfo.TableInformation.TableName)
}
// primary key
if mSampleModel.TableInformation.PrimaryKey != "id" {
t.Errorf("プライマリーキーの解析に失敗しました。\n\"id\" => \"%s\"\n", mSampleModel.TableInformation.PrimaryKey)
}
if mSampleModelTableInfo.TableInformation.PrimaryKey != "id" {
t.Errorf("プライマリーキーの解析に失敗しました。\n\"id\" => \"%s\"\n", mSampleModelTableInfo.TableInformation.PrimaryKey)
}
// delete table
if mSampleModel.TableInformation.DeleteTable {
t.Errorf("論理削除の解析に失敗しました。\n\"false\" => \"ture\"\n")
}
if !mSampleModelTableInfo.TableInformation.DeleteTable {
t.Errorf("論理削除の解析に失敗しました。\n\"true\" => \"false\"\n")
}
if mSampleModelTableInfo.TableInformation.DeletedAtField != "deleted_at" {
t.Errorf("論理削除の解析に失敗しました。\n\"deleted_at\" => \"%s\"\n", mSampleModelTableInfo.TableInformation.DeletedAtField)
}
// Field
if len(mSampleModel.TableInformation.Fields) != 3 {
t.Errorf("フィールドの解析に失敗しました。\n\"3\" => \"%d\"\nDump: %#v\n", len(mSampleModel.TableInformation.Fields), mSampleModel.TableInformation.Fields)
}
if len(mSampleModelTableInfo.TableInformation.Fields) != 2 {
t.Errorf("フィールドの解析に失敗しました。\n\"2\" => \"%d\"\nDump: %#v\n", len(mSampleModelTableInfo.TableInformation.Fields), mSampleModelTableInfo.TableInformation.Fields)
}
if mSampleModelTableInfo.TableInformation.Fields[0] != "id" {
t.Errorf("フィールドの解析に失敗しました。\n\"id\" => \"%s\"\nDump: %#v\n", mSampleModelTableInfo.TableInformation.Fields[0], mSampleModelTableInfo.TableInformation.Fields)
}
}
func TestSql(t *testing.T) {
m := model551.Load()
mSampleModel := m.Get("SampleModel")
mSampleModelTableInfo := m.Get("SampleModelTableInfo")
// Insert
sql := "INSERT INTO `sample_model` (`name`, `description`) VALUES (?, ?)"
if mSampleModel.SqlInformation.Insert != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModel.SqlInformation.Insert)
}
sql = "INSERT INTO `table_information` (`description`) VALUES (?)"
if mSampleModelTableInfo.SqlInformation.Insert != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModelTableInfo.SqlInformation.Insert)
}
// Select
sql = "SELECT `id`, `name`, `description` FROM `sample_model` WHERE 1 = 1"
if mSampleModel.SqlInformation.Select != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModel.SqlInformation.Select)
}
sql = "SELECT `id`, `description` FROM `table_information` WHERE 1 = 1"
if mSampleModelTableInfo.SqlInformation.Select != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModelTableInfo.SqlInformation.Select)
}
// Update
sql = "UPDATE `sample_model` SET `name` = ?, `description` = ? WHERE `id` = ?"
if mSampleModel.SqlInformation.Update != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModel.SqlInformation.Update)
}
sql = "UPDATE `table_information` SET `description` = ? WHERE `id` = ?"
if mSampleModelTableInfo.SqlInformation.Update != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModelTableInfo.SqlInformation.Update)
}
// Delete
sql = "DELETE FROM `sample_model` WHERE `id` = ?"
if mSampleModel.SqlInformation.Delete != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModel.SqlInformation.Delete)
}
sql = "DELETE FROM `table_information` WHERE `id` = ?"
if mSampleModelTableInfo.SqlInformation.Delete != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModelTableInfo.SqlInformation.Delete)
}
// Delete
sql = ""
if mSampleModel.SqlInformation.LogicalDelete != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModel.SqlInformation.LogicalDelete)
}
sql = "INSERT INTO `table_information_delete` (`id`, `description`, `deleted_at`) VALUES (?, ?, ?)"
if mSampleModelTableInfo.SqlInformation.LogicalDelete != sql {
t.Errorf("SQLキャッシュが失敗しました。\nOK: %s\nNG: %s\n", sql, mSampleModelTableInfo.SqlInformation.LogicalDelete)
}
}