Skip to content

Commit

Permalink
Add unit test for getDbCreateSQL()
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhandatsjtu committed May 19, 2022
1 parent d150d85 commit a539289
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions client/orm/model_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 0 additions & 1 deletion client/orm/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit a539289

Please sign in to comment.