增加单元测试文件
This commit is contained in:
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