增加单元测试文件
This commit is contained in:
301
tool/json2go/parser_test.go
Normal file
301
tool/json2go/parser_test.go
Normal 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"`
|
||||
}
|
23
tool/sql2go/generate_dao_test.go
Normal file
23
tool/sql2go/generate_dao_test.go
Normal 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
70
tool/sql2go/go_test.go
Normal 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
|
||||
}
|
18
tool/sql2go/parser_test.go
Normal file
18
tool/sql2go/parser_test.go
Normal 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))
|
||||
}
|
Reference in New Issue
Block a user