增加单元测试文件

This commit is contained in:
白茶清欢 2021-11-23 16:33:01 +08:00
parent 64f7670559
commit ada41fa40a
17 changed files with 954 additions and 1 deletions

2
.gitignore vendored
View File

@ -18,5 +18,5 @@
# vendor/ # vendor/
.idea .idea
.vscode .vscode
*_test.go mail_test.go

38
balance/poll_test.go Normal file
View File

@ -0,0 +1,38 @@
// Package balance...
//
// Description : balance...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-19 2:39 下午
package balance
import (
"fmt"
"testing"
"git.zhangdeman.cn/zhangdeman/gopkg/balance/define"
)
// TestPoll_GetServerNode 轮询模式负载均衡单测
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:39 下午 2021/10/19
func TestPoll_GetServerNode(t *testing.T) {
t.Run("轮询模式负载均衡单元测试", func(t *testing.T) {
p, _ := NewPoll([]*define.ServerNode{
{HostIP: "127.0.0.1", Port: 80, Status: 0, Weight: 0},
{HostIP: "127.0.0.1", Port: 81, Status: 0, Weight: 0},
{HostIP: "127.0.0.1", Port: 82, Status: 0, Weight: 0},
})
for i := 0; i < 100; i++ {
got, err := p.GetServerNode()
if nil != err {
fmt.Println(err)
return
}
fmt.Printf("%s:%d\n", got.HostIP, got.Port)
}
})
}

35
cmd/execute_test.go Normal file
View File

@ -0,0 +1,35 @@
// Package cmd...
//
// Description : cmd...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-12 2:28 下午
package cmd
import (
"fmt"
"testing"
)
// TestExecute ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:28 下午 2021/11/12
func TestExecute(t *testing.T) {
r := Execute(Config{
WorkDir: "/tmp",
Command: "ls",
Script: "",
ParameterList: []Parameter{{"", "-l"}, {"", "-a"}},
})
fmt.Println(string(r.Output), r.Err, r.ExecuteCommand, r.WorkDir)
r = Execute(Config{
WorkDir: "",
Command: "sh",
Script: "test.sh",
ParameterList: []Parameter{},
})
fmt.Println(string(r.Output), r.Err, r.ExecuteCommand, r.WorkDir)
}

30
excel/create_test.go Normal file
View File

@ -0,0 +1,30 @@
// Package excel...
//
// Description : excel...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-19 2:05 下午
package excel
import (
"fmt"
"testing"
)
// TestCreate_GenerateSheet ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:05 下午 2021/11/19
func TestCreate_GenerateSheet(t *testing.T) {
e := NewExcel()
sheetList := []SheetData{
{true, "sheet11111", [][]interface{}{[]interface{}{123, 345, 678}}},
{false, "sheet2222", [][]interface{}{[]interface{}{123, 345, 678}}},
{false, "sheet3333", [][]interface{}{[]interface{}{123, 345, 678}}},
{false, "sheet44444", [][]interface{}{[]interface{}{123, 345, 678}}},
}
fmt.Println(e.GenerateSheet(sheetList))
fmt.Println(e.Save("./test.xlsx"))
}

54
excel/read_test.go Normal file
View File

@ -0,0 +1,54 @@
// Package excel...
//
// Description : excel...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-19 4:12 下午
package excel
import (
"fmt"
"testing"
)
// TestRead_GetAllData ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:12 下午 2021/11/19
func TestRead_GetAllData(t *testing.T) {
r, _ := NewRead("./test.xlsx", "")
fmt.Println(r.GetAllData())
}
// TestRead_GetAllDataToMap ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:12 下午 2021/11/19
func TestRead_GetAllDataToMap(t *testing.T) {
r, _ := NewRead("./test.xlsx", "")
fmt.Println(r.GetAllDataToMap([]string{"num1", "num2", "num3", "num4", "num5"}))
fmt.Println(r.GetAllDataToMap([]string{"num1", "num2"}))
}
// TestRead_ExtractAssignCol ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:12 下午 2021/11/19
func TestRead_ExtractAssignCol(t *testing.T) {
r, _ := NewRead("./test.xlsx", "")
fmt.Println(r.ExtractAssignCol([]int{0, 2, 8}))
}
// TestRead_ExtractAssignColToMap ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:12 下午 2021/11/19
func TestRead_ExtractAssignColToMap(t *testing.T) {
r, _ := NewRead("./test.xlsx", "")
fmt.Println(r.ExtractAssignColToMap(map[int]string{0: "num1", 2: "num3", 10: "num11"}))
}

86
json_tool/json_test.go Normal file
View File

@ -0,0 +1,86 @@
// Package json_tool...
//
// Description : json_tool...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-03-10 11:44 下午
package json_tool
import (
"fmt"
"testing"
)
// TestJSON ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:58 下午 2021/3/14
func TestJSON(t *testing.T) {
tree := NewDynamicJSON()
fmt.Println(tree.extraSliceIndex("[200]"))
tree.SetValue("extra.height.value", 180, false)
tree.SetValue("extra.height.unit.use", "cm", false)
tree.SetValue("extra.height.unit.open", "1", false)
tree.SetValue("name", "zhangdeman", false)
tree.SetValue("good.name", "good", false)
tree.SetValue("work", "111", false)
tree.SetValue("good.price", "180", false)
tree.SetValue("good.unit", "$", false)
tree.SetValue("slice.[0].name", "zhang", false)
tree.SetValue("slice.[0].age", 30, false)
tree.SetValue("slice.[1].name", "de", false)
tree.SetValue("slice.[2].name", "man", false)
tree.SetValue("slice.[3]", "zhangdeman", false)
fmt.Println(tree.String())
tree = NewDynamicJSON()
tree.SetValue("[0]", "zhang", false)
tree.SetValue("[1]", "de", false)
tree.SetValue("[2]", "man", false)
fmt.Println(tree.String())
}
// TestType 判断数据类型断言
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:59 下午 2021/3/14
func TestType(t *testing.T) {
}
// TestSelect 测试动态选择字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:47 下午 2021/4/13
func TestSelect(t *testing.T) {
source := map[string]interface{}{
"name": "zhangdeman",
"extra": map[string]interface{}{
"age": 18,
"height": 180,
"slice": []int{1, 2, 3},
},
"slice": []int{1, 2, 3},
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4},
"table": []map[string]interface{}{
{"name": "alex", "age": 18, "number": 1},
{"name": "bob", "age": 28, "number": 2},
},
}
rule := map[string]string{
"name": "user_name",
"extra.age": "user_age",
"extra.height": "user_height",
"table.[].name": "slice.[].name",
}
filter := NewFilter(source, rule)
d, e := filter.Result()
if nil != e {
fmt.Println(e)
return
}
fmt.Println(d.String())
}

View File

@ -0,0 +1,116 @@
// Package etcd...
//
// Description : etcd...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-23 12:27 下午
package etcd
import (
"fmt"
"testing"
"time"
"go.etcd.io/etcd/clientv3"
)
func init() {
err := InitEtcdClient(clientv3.Config{
Endpoints: []string{"localhost:2379"},
})
if nil != err {
panic(err.Error())
}
}
// TestPut ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:27 下午 2021/11/23
func TestPut(t *testing.T) {
fmt.Println(Put(nil, "name", "zhangdeman", 0))
}
// TestGet ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:30 下午 2021/11/23
func TestGet(t *testing.T) {
fmt.Println(Get(nil, "name", 0))
}
// TestWatchKey ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:06 下午 2021/11/23
func TestWatchKey(t *testing.T) {
key := "name"
dealFunc := func(data *clientv3.Event) {
fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision)
}
go func() {
for i := 0; i < 30; i++ {
_ = Put(nil, key, fmt.Sprintf("test-%d", i), 0)
time.Sleep(time.Second)
}
}()
WatchKey(nil, key, dealFunc)
}
// TestWatchKeyWithCancel ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:57 下午 2021/11/23
func TestWatchKeyWithCancel(t *testing.T) {
key := "name"
dealFunc := func(data *clientv3.Event) {
fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision)
}
cancelFunc := func(key string, data interface{}) {
fmt.Println("取消监听 : ", key, data)
}
cancelChan := make(chan interface{}, 1)
go func() {
for i := 0; i < 30; i++ {
_ = Put(nil, key, fmt.Sprintf("test-%d", i), 0)
time.Sleep(time.Second)
}
time.Sleep(10 * time.Second)
cancelChan <- "Hello World"
}()
WatchKeyWithCancel(nil, key, dealFunc, cancelChan, cancelFunc)
}
// TestWatchKeyWithCancelByChangeCallback ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:27 下午 2021/11/23
func TestWatchKeyWithCancelByChangeCallback(t *testing.T) {
key := "name"
cancelChan := make(chan interface{}, 1)
dealFunc := func(data *clientv3.Event) {
fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision)
if string(data.Kv.Value) == "test-29" {
cancelChan <- "Hello World!"
}
}
cancelFunc := func(key string, data interface{}) {
fmt.Println("取消监听 : ", key, data)
}
go func() {
for i := 0; i < 30; i++ {
_ = Put(nil, key, fmt.Sprintf("test-%d", i), 0)
time.Sleep(time.Second)
}
}()
WatchKeyWithCancel(nil, key, dealFunc, cancelChan, cancelFunc)
}

25
system/cpu_test.go Normal file
View File

@ -0,0 +1,25 @@
// Package system...
//
// Description : system...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-12 5:58 下午
package system
import (
"encoding/json"
"fmt"
"testing"
)
// TestGetCPUInfo ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:58 下午 2021/10/12
func TestGetCPUInfo(t *testing.T) {
r, _ := GetCPUInfo()
byteData, _ := json.Marshal(r)
fmt.Println(string(byteData))
}

25
system/disk_test.go Normal file
View File

@ -0,0 +1,25 @@
// Package system...
//
// Description : system...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-12 9:04 下午
package system
import (
"encoding/json"
"fmt"
"testing"
)
// TestGetDiskInfo ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:04 下午 2021/10/12
func TestGetDiskInfo(t *testing.T) {
r, _ := GetDiskInfo()
byteData, _ := json.Marshal(r)
fmt.Println(string(byteData))
}

25
system/host_test.go Normal file
View File

@ -0,0 +1,25 @@
// Package system...
//
// Description : system...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-12 7:58 下午
package system
import (
"encoding/json"
"fmt"
"testing"
)
// TestGetHostInfo ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 7:59 下午 2021/10/12
func TestGetHostInfo(t *testing.T) {
r, _ := GetHostInfo()
byteData, _ := json.Marshal(r)
fmt.Println(string(byteData))
}

25
system/memory_test.go Normal file
View File

@ -0,0 +1,25 @@
// Package system...
//
// Description : system...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-12 7:09 下午
package system
import (
"encoding/json"
"fmt"
"testing"
)
// TestGetMemoryInfo ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 7:09 下午 2021/10/12
func TestGetMemoryInfo(t *testing.T) {
r, _ := GetMemoryInfo()
byteData, _ := json.Marshal(r)
fmt.Println(string(byteData))
}

301
tool/json2go/parser_test.go Normal file
View File

@ -0,0 +1,301 @@
// Package json2go...
//
// Description : json2go...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-09 4:21 下午
package json2go
import (
"fmt"
"testing"
)
func TestParse(t *testing.T) {
testJSON := `{
"name": "zhang",
"test": {
"name": "aa",
"age": 18,
"third": {"a":"1", "b":"like"},
"result": true,
"result_fail":false
},
"list": [1,2,3,4],
"list1": [2.2, 3, 4, 5],
"list_object":[{"name":"zhang"}, {"age": 18}]
}`
testJSON = `{
"ref": "refs/heads/master",
"before": "4c8219477db1e93e5540c9af4889f2633a9ca9e9",
"after": "5888c985a9afd9f1b25807fade69c53938aef485",
"compare_url": "https://git.zhangdeman.cn/zhangdeman/manager-job/compare/4c8219477db1e93e5540c9af4889f2633a9ca9e9...5888c985a9afd9f1b25807fade69c53938aef485",
"commits": [
{
"id": "5888c985a9afd9f1b25807fade69c53938aef485",
"message": "更新文案\n",
"url": "https://git.zhangdeman.cn/zhangdeman/manager-job/commit/5888c985a9afd9f1b25807fade69c53938aef485",
"author": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"committer": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"verification": null,
"timestamp": "2021-11-11T17:54:40+08:00",
"added": [],
"removed": [],
"modified": [
"publish_admin.sh"
]
}
],
"head_commit": {
"id": "5888c985a9afd9f1b25807fade69c53938aef485",
"message": "更新文案\n",
"url": "https://git.zhangdeman.cn/zhangdeman/manager-job/commit/5888c985a9afd9f1b25807fade69c53938aef485",
"author": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"committer": {
"name": "白茶清欢",
"email": "go_developer@163.com",
"username": "zhangdeman"
},
"verification": null,
"timestamp": "2021-11-11T17:54:40+08:00",
"added": [],
"removed": [],
"modified": [
"publish_admin.sh"
]
},
"repository": {
"id": 24,
"owner": {"id":1,"login":"zhangdeman","full_name":"","email":"go_developer@163.com","avatar_url":"https://git.zhangdeman.cn/user/avatar/zhangdeman/-1","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2021-05-04T12:58:50+08:00","restricted":false,"active":false,"prohibit_login":false,"location":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"zhangdeman"},
"name": "manager-job",
"full_name": "zhangdeman/manager-job",
"description": "各种管理JOB",
"empty": false,
"private": false,
"fork": false,
"template": false,
"parent": null,
"mirror": false,
"size": 24,
"html_url": "https://git.zhangdeman.cn/zhangdeman/manager-job",
"ssh_url": "www@git.zhangdeman.cn:zhangdeman/manager-job.git",
"clone_url": "https://git.zhangdeman.cn/zhangdeman/manager-job.git",
"original_url": "",
"website": "",
"stars_count": 0,
"forks_count": 0,
"watchers_count": 1,
"open_issues_count": 0,
"open_pr_counter": 0,
"release_counter": 0,
"default_branch": "master",
"archived": false,
"created_at": "2021-11-11T16:54:10+08:00",
"updated_at": "2021-11-11T17:02:22+08:00",
"permissions": {
"admin": true,
"push": true,
"pull": true
},
"has_issues": true,
"internal_tracker": {
"enable_time_tracker": false,
"allow_only_contributors_to_track_time": true,
"enable_issue_dependencies": true
},
"has_wiki": true,
"has_pull_requests": true,
"has_projects": true,
"ignore_whitespace_conflicts": false,
"allow_merge_commits": true,
"allow_rebase": true,
"allow_rebase_explicit": true,
"allow_squash_merge": true,
"default_merge_style": "merge",
"avatar_url": "",
"internal": false,
"mirror_interval": ""
},
"pusher": {"id":1,"login":"zhangdeman","full_name":"","email":"go_developer@163.com","avatar_url":"https://git.zhangdeman.cn/user/avatar/zhangdeman/-1","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2021-05-04T12:58:50+08:00","restricted":false,"active":false,"prohibit_login":false,"location":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"zhangdeman"},
"sender": {"id":1,"login":"zhangdeman","full_name":"","email":"go_developer@163.com","avatar_url":"https://git.zhangdeman.cn/user/avatar/zhangdeman/-1","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2021-05-04T12:58:50+08:00","restricted":false,"active":false,"prohibit_login":false,"location":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"zhangdeman"}
}
`
fmt.Println(NewJSON2GO("DemoJSON").Parse(testJSON))
}
type DemoJSON struct {
Repository struct {
Mirror bool `json:"mirror"`
OriginalUrl string `json:"original_url"`
StarsCount int64 `json:"stars_count"`
IgnoreWhitespaceConflicts bool `json:"ignore_whitespace_conflicts"`
FullName string `json:"full_name"`
Fork bool `json:"fork"`
CreatedAt string `json:"created_at"`
Internal bool `json:"internal"`
AllowRebaseExplicit bool `json:"allow_rebase_explicit"`
MirrorInterval string `json:"mirror_interval"`
CloneUrl string `json:"clone_url"`
WatchersCount int64 `json:"watchers_count"`
OpenIssuesCount int64 `json:"open_issues_count"`
ReleaseCounter int64 `json:"release_counter"`
InternalTracker struct {
EnableTimeTracker bool `json:"enable_time_tracker"`
AllowOnlyContributorsToTrackTime bool `json:"allow_only_contributors_to_track_time"`
EnableIssueDependencies bool `json:"enable_issue_dependencies"`
} `json:"internal_tracker"`
Name string `json:"name"`
Empty bool `json:"empty"`
SshUrl string `json:"ssh_url"`
ForksCount int64 `json:"forks_count"`
Permissions struct {
Pull bool `json:"pull"`
Admin bool `json:"admin"`
Push bool `json:"push"`
} `json:"permissions"`
Private bool `json:"private"`
Size int64 `json:"size"`
HasWiki bool `json:"has_wiki"`
HasProjects bool `json:"has_projects"`
AllowMergeCommits bool `json:"allow_merge_commits"`
AvatarUrl string `json:"avatar_url"`
Id int64 `json:"id"`
Description string `json:"description"`
Website string `json:"website"`
UpdatedAt string `json:"updated_at"`
HasPullRequests bool `json:"has_pull_requests"`
Parent map[string]interface{} `json:"parent"`
OpenPrCounter int64 `json:"open_pr_counter"`
HasIssues bool `json:"has_issues"`
AllowRebase bool `json:"allow_rebase"`
AllowSquashMerge bool `json:"allow_squash_merge"`
Owner struct {
IsAdmin bool `json:"is_admin"`
Website string `json:"website"`
Email string `json:"email"`
AvatarUrl string `json:"avatar_url"`
ProhibitLogin bool `json:"prohibit_login"`
Description string `json:"description"`
FollowingCount int64 `json:"following_count"`
StarredReposCount int64 `json:"starred_repos_count"`
Username string `json:"username"`
Login string `json:"login"`
Created string `json:"created"`
Visibility string `json:"visibility"`
FullName string `json:"full_name"`
LastLogin string `json:"last_login"`
Restricted bool `json:"restricted"`
Active bool `json:"active"`
Location string `json:"location"`
FollowersCount int64 `json:"followers_count"`
Id int64 `json:"id"`
Language string `json:"language"`
} `json:"owner"`
Template bool `json:"template"`
HtmlUrl string `json:"html_url"`
DefaultBranch string `json:"default_branch"`
Archived bool `json:"archived"`
DefaultMergeStyle string `json:"default_merge_style"`
} `json:"repository"`
Sender struct {
Created string `json:"created"`
Website string `json:"website"`
FollowingCount int64 `json:"following_count"`
StarredReposCount int64 `json:"starred_repos_count"`
FullName string `json:"full_name"`
Language string `json:"language"`
LastLogin string `json:"last_login"`
Visibility string `json:"visibility"`
FollowersCount int64 `json:"followers_count"`
Username string `json:"username"`
IsAdmin bool `json:"is_admin"`
Active bool `json:"active"`
Location string `json:"location"`
Restricted bool `json:"restricted"`
Description string `json:"description"`
AvatarUrl string `json:"avatar_url"`
ProhibitLogin bool `json:"prohibit_login"`
Id int64 `json:"id"`
Login string `json:"login"`
Email string `json:"email"`
} `json:"sender"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
HeadCommit struct {
Message string `json:"message"`
Url string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Verification map[string]interface{} `json:"verification"`
Timestamp string `json:"timestamp"`
Id string `json:"id"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Added []interface{} `json:"added"`
Removed []interface{} `json:"removed"`
Modified []string `json:"modified"`
} `json:"head_commit"`
CompareUrl string `json:"compare_url"`
Commits []struct {
Url string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Username string `json:"username"`
Name string `json:"name"`
Email string `json:"email"`
} `json:"committer"`
Verification map[string]interface{} `json:"verification"`
Removed []interface{} `json:"removed"`
Id string `json:"id"`
Message string `json:"message"`
Modified []string `json:"modified"`
Timestamp string `json:"timestamp"`
Added []interface{} `json:"added"`
} `json:"commits"`
Pusher struct {
Language string `json:"language"`
Website string `json:"website"`
Id int64 `json:"id"`
Email string `json:"email"`
Active bool `json:"active"`
Description string `json:"description"`
Visibility string `json:"visibility"`
Login string `json:"login"`
Restricted bool `json:"restricted"`
Location string `json:"location"`
StarredReposCount int64 `json:"starred_repos_count"`
Username string `json:"username"`
IsAdmin bool `json:"is_admin"`
ProhibitLogin bool `json:"prohibit_login"`
LastLogin string `json:"last_login"`
Created string `json:"created"`
FollowersCount int64 `json:"followers_count"`
FollowingCount int64 `json:"following_count"`
FullName string `json:"full_name"`
AvatarUrl string `json:"avatar_url"`
} `json:"pusher"`
}

View File

@ -0,0 +1,23 @@
// Package sql2go...
//
// Description : sql2go...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-17 11:46 上午
package sql2go
import (
"fmt"
"testing"
)
// TestGenerateDao ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:05 下午 2021/11/17
func TestGenerateDao(t *testing.T) {
sql := "CREATE TABLE `app` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',\n `code` varchar(128) NOT NULL DEFAULT '' COMMENT '分配的app_code',\n `secret` varchar(64) NOT NULL DEFAULT '' COMMENT '分配的私钥',\n `status` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '当前状态 o - 初始化 1- 使用中 2 - 禁用 3 - 删除',\n `description` varchar(256) NOT NULL DEFAULT '' COMMENT '描述信息',\n `apply_user_name` varchar(64) NOT NULL DEFAULT '' COMMENT '申请人姓名',\n `apply_user_contact` varchar(128) NOT NULL DEFAULT '' COMMENT '申请人联系方式',\n `create_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '创建人ID',\n `modify_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '修改人ID',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniq_code` (`code`)\n) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8mb4 COMMENT='APP信息表'"
fmt.Println(GenerateDao(sql, "sql2go"))
}

70
tool/sql2go/go_test.go Normal file
View File

@ -0,0 +1,70 @@
// Package dao ...
//
// Description : dao ...
package sql2go
import (
"errors"
"gorm.io/gorm"
)
// App 数据库表的数据结构
type App struct {
ID int64 `json:"id" gorm:"column:id;default:;NOT NULL"` // id 主键ID
Code string `json:"code" gorm:"column:code;default:;NOT NULL"` // code 分配的app_code
Secret string `json:"secret" gorm:"column:secret;default:;NOT NULL"` // secret 分配的私钥
Status int `json:"status" gorm:"column:status;default:0;NOT NULL"` // status 当前状态 o - 初始化 1- 使用中 2 - 禁用 3 - 删除
Description string `json:"description" gorm:"column:description;default:;NOT NULL"` // description 描述信息
ApplyUserName string `json:"apply_user_name" gorm:"column:apply_user_name;default:;NOT NULL"` // apply_user_name 申请人姓名
ApplyUserContact string `json:"apply_user_contact" gorm:"column:apply_user_contact;default:;NOT NULL"` // apply_user_contact 申请人联系方式
CreateUserID string `json:"create_user_id" gorm:"column:create_user_id;default:;NOT NULL"` // create_user_id 创建人ID
ModifyUserID string `json:"modify_user_id" gorm:"column:modify_user_id;default:;NOT NULL"` // modify_user_id 修改人ID
}
// NewAppDao 获取DAO实例
func NewAppDao() *AppDao {
return &AppDao{
table: "app",
}
}
// AppDao sql2go tool generate
type AppDao struct {
table string
}
// GetDetailByID 根据主键ID获取详情
func (ad *AppDao) GetDetailByID(dbInstance *gorm.DB, ID int64) (*App, error) {
var (
err error
detail App
)
if err = dbInstance.Table(ad.table).Where("{PRIMARY_KEY_FIELD} = ?", ID).Limit(1).First(&detail).Error; nil != err {
return nil, err
}
return &detail, nil
}
// GetList 获取数据列表
func (ad *AppDao) GetList(dbInstance *gorm.DB, condition map[string]interface{}, limit int, offset int) ([]App, error) {
if nil == condition {
condition = make(map[string]interface{})
}
var (
err error
list []App
)
if err = dbInstance.Table(ad.table).Where(condition).Limit(limit).Offset(offset).Find(&list).Error; nil != err {
return make([]App, 0), err
}
return list, nil
}
// Create 创建数据
func (ad *AppDao) Create(dbInstance *gorm.DB, data *App) error {
if nil == data {
return errors.New("data is nil")
}
return dbInstance.Table(ad.table).Create(data).Error
}

View File

@ -0,0 +1,18 @@
// Package sql2go...
//
// Description : sql2go...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-25 5:02 下午
package sql2go
import (
"fmt"
"testing"
)
func TestParseSql(t *testing.T) {
sql := "CREATE TABLE `app` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',\n `code` varchar(128) NOT NULL DEFAULT '' COMMENT '分配的app_code',\n `secret` varchar(64) NOT NULL DEFAULT '' COMMENT '分配的私钥',\n `status` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '当前状态 o - 初始化 1- 使用中 2 - 禁用 3 - 删除',\n `description` varchar(256) NOT NULL DEFAULT '' COMMENT '描述信息',\n `apply_user_name` varchar(64) NOT NULL DEFAULT '' COMMENT '申请人姓名',\n `apply_user_contact` varchar(128) NOT NULL DEFAULT '' COMMENT '申请人联系方式',\n `create_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '创建人ID',\n `modify_user_id` varchar(128) NOT NULL DEFAULT '' COMMENT '修改人ID',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniq_code` (`code`)\n) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8mb4 COMMENT='APP信息表'"
fmt.Println(ParseCreateTableSql(sql))
}

22
util/json_test.go Normal file
View File

@ -0,0 +1,22 @@
// Package util...
//
// Description : util...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-05 6:10 下午
package util
import (
"testing"
)
// TestJSONMarshal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:12 下午 2021/11/5
func TestJSONMarshal(t *testing.T) {
data := `{"name":"zhangdeman", "age":18, "height": 180}`
JSONConsoleOutput(data)
}

60
util/url_test.go Normal file
View File

@ -0,0 +1,60 @@
// Package util...
//
// Description : util...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-04 2:58 下午
package util
import (
"encoding/json"
"fmt"
"testing"
)
// TestBuildQueryURL ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:04 下午 2021/11/4
func TestBuildQueryURL(t *testing.T) {
url := "https://ww.baidu.com/api/detail"
parameter := map[string]string{
"name": "zhang",
"age": "18",
}
fmt.Println(BuildQueryURL(url, parameter))
url = "https://ww.baidu.com/api/detail?tag=1"
fmt.Println(BuildQueryURL(url, parameter))
}
// TestURLDecode ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:04 下午 2021/11/4
func TestURLDecode(t *testing.T) {
}
// TestURLEncode ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:05 下午 2021/11/4
func TestURLEncode(t *testing.T) {
}
// TestURLParse 测试URL解析
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:00 下午 2021/11/4
func TestURLParse(t *testing.T) {
inputURL := "https://www.baidu.com/api/detail?a=1&b=2&a=3&d=qwe"
r, _ := URLParse(inputURL)
byteData, _ := json.Marshal(r)
fmt.Println(string(byteData))
}