Add unit test for getDbCreateSQL()
This commit is contained in:
parent
d150d85bfd
commit
a539289b20
@ -6,6 +6,7 @@
|
||||
- [Add httplib OpenTelemetry Filter](https://github.com/beego/beego/pull/4888, https://github.com/beego/beego/pull/4915)
|
||||
- [Support NewBeegoRequestWithCtx in httplib](https://github.com/beego/beego/pull/4895)
|
||||
- [Support lifecycle callback](https://github.com/beego/beego/pull/4918)
|
||||
- [Append column comments to create table sentence when using postgres](https://github.com/beego/beego/pull/4940)
|
||||
|
||||
# v2.0.2
|
||||
See v2.0.2-beta.1
|
||||
|
||||
69
client/orm/model_test.go
Normal file
69
client/orm/model_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
// Copyright 2022
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package orm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type ModelWithComments struct {
|
||||
Id int `orm:"description(user id)"`
|
||||
UserName string `orm:"size(30);unique;description(user name)"`
|
||||
Email string `orm:"size(100)"`
|
||||
Password string `orm:"size(100)"`
|
||||
}
|
||||
|
||||
func TestGetDbCreateSQLWithComment(t *testing.T) {
|
||||
al := getDbAlias("default")
|
||||
modelCache.clean()
|
||||
RegisterModel(&ModelWithComments{})
|
||||
queries, _, _ := modelCache.getDbCreateSQL(al)
|
||||
header := `-- --------------------------------------------------
|
||||
-- Table Structure for ` + fmt.Sprintf("`%s`", modelCache.allOrdered()[0].fullName) + `
|
||||
-- --------------------------------------------------`
|
||||
if al.Driver == DRPostgres {
|
||||
assert.Equal(t, queries, []string{header + `
|
||||
CREATE TABLE IF NOT EXISTS "model_with_comments" (
|
||||
"id" serial NOT NULL PRIMARY KEY,
|
||||
"user_name" varchar(30) NOT NULL DEFAULT '' UNIQUE,
|
||||
"email" varchar(100) NOT NULL DEFAULT '' ,
|
||||
"password" varchar(100) NOT NULL DEFAULT ''
|
||||
);
|
||||
COMMENT ON COLUMN "model_with_comments"."id" is 'user id';
|
||||
COMMENT ON COLUMN "model_with_comments"."user_name" is 'user name';`,
|
||||
})
|
||||
} else if al.Driver == DRMySQL {
|
||||
assert.Equal(t, queries, []string{header + `
|
||||
CREATE TABLE IF NOT EXISTS ` + "`model_with_comments`" + ` (
|
||||
` + "`id`" + ` integer AUTO_INCREMENT NOT NULL PRIMARY KEY COMMENT 'user id',
|
||||
` + "`user_name`" + ` varchar(30) NOT NULL DEFAULT '' UNIQUE COMMENT 'user name',
|
||||
` + "`email`" + ` varchar(100) NOT NULL DEFAULT '' ,
|
||||
` + "`password`" + ` varchar(100) NOT NULL DEFAULT ''
|
||||
) ENGINE=INNODB;`,
|
||||
})
|
||||
} else if al.Driver == DRSqlite {
|
||||
assert.Equal(t, queries, []string{header + `
|
||||
CREATE TABLE IF NOT EXISTS ` + "`model_with_comments`" + ` (
|
||||
` + "`id`" + ` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
` + "`user_name`" + ` varchar(30) NOT NULL DEFAULT '' UNIQUE,
|
||||
` + "`email`" + ` varchar(100) NOT NULL DEFAULT '' ,
|
||||
` + "`password`" + ` varchar(100) NOT NULL DEFAULT ''
|
||||
);`,
|
||||
})
|
||||
}
|
||||
modelCache.clean()
|
||||
}
|
||||
@ -435,7 +435,6 @@ func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes
|
||||
var commentIndexes []int // store comment indexes for postgres
|
||||
|
||||
for i, fi := range mi.fields.fieldsDB {
|
||||
|
||||
column := fmt.Sprintf(" %s%s%s ", Q, fi.column, Q)
|
||||
col := getColumnTyp(al, fi)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user