-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d150d85
commit a539289
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters