mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 09:40:24 +08:00
24fb29a356
* change column to read from information_schema * reactor generate mode from datasource * reactor generate mode from datasource * add primary key check logic * resolve rebase conflicts * add naming style * add filename test case * resolve rebase conflicts * reactor test * add test case * change shell script to makefile * update rpc new * update gen_test.go * format code * format code * update test * generates alias
115 lines
2.1 KiB
Go
115 lines
2.1 KiB
Go
package generator
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
)
|
|
|
|
func TestGenTemplates(t *testing.T) {
|
|
_ = Clean()
|
|
err := GenTemplates(nil)
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestRevertTemplate(t *testing.T) {
|
|
_ = Clean()
|
|
err := GenTemplates(nil)
|
|
assert.Nil(t, err)
|
|
fp, err := util.GetTemplateDir(category)
|
|
if err != nil {
|
|
return
|
|
}
|
|
mainTpl := filepath.Join(fp, mainTemplateFile)
|
|
data, err := ioutil.ReadFile(mainTpl)
|
|
if err != nil {
|
|
return
|
|
}
|
|
assert.Equal(t, templates[mainTemplateFile], string(data))
|
|
|
|
err = RevertTemplate("test")
|
|
if err != nil {
|
|
assert.Equal(t, "test: no such file name", err.Error())
|
|
}
|
|
|
|
err = ioutil.WriteFile(mainTpl, []byte("modify"), os.ModePerm)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
data, err = ioutil.ReadFile(mainTpl)
|
|
if err != nil {
|
|
return
|
|
}
|
|
assert.Equal(t, "modify", string(data))
|
|
|
|
err = RevertTemplate(mainTemplateFile)
|
|
assert.Nil(t, err)
|
|
|
|
data, err = ioutil.ReadFile(mainTpl)
|
|
if err != nil {
|
|
return
|
|
}
|
|
assert.Equal(t, templates[mainTemplateFile], string(data))
|
|
}
|
|
|
|
func TestClean(t *testing.T) {
|
|
_ = Clean()
|
|
err := GenTemplates(nil)
|
|
assert.Nil(t, err)
|
|
fp, err := util.GetTemplateDir(category)
|
|
if err != nil {
|
|
return
|
|
}
|
|
mainTpl := filepath.Join(fp, mainTemplateFile)
|
|
_, err = os.Stat(mainTpl)
|
|
assert.Nil(t, err)
|
|
|
|
err = Clean()
|
|
assert.Nil(t, err)
|
|
|
|
_, err = os.Stat(mainTpl)
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
_ = Clean()
|
|
err := GenTemplates(nil)
|
|
assert.Nil(t, err)
|
|
fp, err := util.GetTemplateDir(category)
|
|
if err != nil {
|
|
return
|
|
}
|
|
mainTpl := filepath.Join(fp, mainTemplateFile)
|
|
|
|
err = ioutil.WriteFile(mainTpl, []byte("modify"), os.ModePerm)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(mainTpl)
|
|
if err != nil {
|
|
return
|
|
}
|
|
assert.Equal(t, "modify", string(data))
|
|
|
|
err = Update(category)
|
|
assert.Nil(t, err)
|
|
|
|
data, err = ioutil.ReadFile(mainTpl)
|
|
if err != nil {
|
|
return
|
|
}
|
|
assert.Equal(t, templates[mainTemplateFile], string(data))
|
|
}
|
|
|
|
func TestGetCategory(t *testing.T) {
|
|
_ = Clean()
|
|
result := GetCategory()
|
|
assert.Equal(t, category, result)
|
|
}
|