gorm/schema/naming_test.go

220 lines
7.0 KiB
Go
Raw Normal View History

2020-01-31 09:17:02 +03:00
package schema
import (
"strings"
2020-01-31 09:17:02 +03:00
"testing"
)
func TestToDBName(t *testing.T) {
maps := map[string]string{
2020-01-31 09:17:02 +03:00
"": "",
"x": "x",
"X": "x",
"userRestrictions": "user_restrictions",
"ThisIsATest": "this_is_a_test",
"PFAndESI": "pf_and_esi",
"AbcAndJkl": "abc_and_jkl",
"EmployeeID": "employee_id",
"SKU_ID": "sku_id",
"FieldX": "field_x",
"HTTPAndSMTP": "http_and_smtp",
"HTTPServerHandlerForURLID": "http_server_handler_for_url_id",
"UUID": "uuid",
"HTTPURL": "http_url",
"HTTP_URL": "http_url",
"SHA256Hash": "sha256_hash",
"SHA256HASH": "sha256_hash",
"ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIdCanBeUsedAtTheEndAsID": "this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id",
}
ns := NamingStrategy{}
2020-01-31 09:17:02 +03:00
for key, value := range maps {
if ns.toDBName(key) != value {
t.Errorf("%v toName should equal %v, but got %v", key, value, ns.toDBName(key))
2020-01-31 09:17:02 +03:00
}
}
2021-08-11 11:20:21 +03:00
maps = map[string]string{
"x": "X",
"user_restrictions": "UserRestriction",
"this_is_a_test": "ThisIsATest",
"abc_and_jkl": "AbcAndJkl",
"employee_id": "EmployeeID",
"field_x": "FieldX",
"http_and_smtp": "HTTPAndSMTP",
"http_server_handler_for_url_id": "HTTPServerHandlerForURLID",
"uuid": "UUID",
"http_url": "HTTPURL",
"sha256_hash": "Sha256Hash",
"this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id": "ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIDCanBeUsedAtTheEndAsID",
}
for key, value := range maps {
if ns.SchemaName(key) != value {
t.Errorf("%v schema name should equal %v, but got %v", key, value, ns.SchemaName(key))
}
}
2020-01-31 09:17:02 +03:00
}
2020-09-28 05:55:27 +03:00
func TestNamingStrategy(t *testing.T) {
ns := NamingStrategy{
2020-09-28 05:55:27 +03:00
TablePrefix: "public.",
SingularTable: true,
NameReplacer: strings.NewReplacer("CID", "Cid"),
2020-09-28 05:55:27 +03:00
}
idxName := ns.IndexName("public.table", "name")
if idxName != "idx_public_table_name" {
t.Errorf("invalid index name generated, got %v", idxName)
}
2020-09-28 05:55:27 +03:00
chkName := ns.CheckerName("public.table", "name")
if chkName != "chk_public_table_name" {
t.Errorf("invalid checker name generated, got %v", chkName)
}
2020-09-28 05:55:27 +03:00
joinTable := ns.JoinTableName("user_languages")
if joinTable != "public.user_languages" {
t.Errorf("invalid join table generated, got %v", joinTable)
}
2020-09-28 05:55:27 +03:00
joinTable2 := ns.JoinTableName("UserLanguage")
if joinTable2 != "public.user_language" {
t.Errorf("invalid join table generated, got %v", joinTable2)
}
2020-09-28 05:55:27 +03:00
tableName := ns.TableName("Company")
if tableName != "public.company" {
t.Errorf("invalid table name generated, got %v", tableName)
}
columdName := ns.ColumnName("", "NameCID")
if columdName != "name_cid" {
t.Errorf("invalid column name generated, got %v", columdName)
}
}
type CustomReplacer struct {
f func(string) string
}
func (r CustomReplacer) Replace(name string) string {
return r.f(name)
}
func TestCustomReplacer(t *testing.T) {
ns := NamingStrategy{
TablePrefix: "public.",
SingularTable: true,
NameReplacer: CustomReplacer{
func(name string) string {
replaced := "REPLACED_" + strings.ToUpper(name)
return strings.NewReplacer("CID", "_Cid").Replace(replaced)
},
},
NoLowerCase: false,
}
idxName := ns.IndexName("public.table", "name")
if idxName != "idx_public_table_replaced_name" {
t.Errorf("invalid index name generated, got %v", idxName)
}
chkName := ns.CheckerName("public.table", "name")
if chkName != "chk_public_table_name" {
t.Errorf("invalid checker name generated, got %v", chkName)
}
joinTable := ns.JoinTableName("user_languages")
if joinTable != "public.user_languages" { // Seems like a bug in NamingStrategy to skip the Replacer when the name is lowercase here.
t.Errorf("invalid join table generated, got %v", joinTable)
}
joinTable2 := ns.JoinTableName("UserLanguage")
if joinTable2 != "public.replaced_userlanguage" {
t.Errorf("invalid join table generated, got %v", joinTable2)
}
tableName := ns.TableName("Company")
if tableName != "public.replaced_company" {
t.Errorf("invalid table name generated, got %v", tableName)
}
columdName := ns.ColumnName("", "NameCID")
if columdName != "replaced_name_cid" {
t.Errorf("invalid column name generated, got %v", columdName)
}
}
func TestCustomReplacerWithNoLowerCase(t *testing.T) {
ns := NamingStrategy{
TablePrefix: "public.",
SingularTable: true,
NameReplacer: CustomReplacer{
func(name string) string {
replaced := "REPLACED_" + strings.ToUpper(name)
return strings.NewReplacer("CID", "_Cid").Replace(replaced)
},
},
NoLowerCase: true,
}
idxName := ns.IndexName("public.table", "name")
if idxName != "idx_public_table_REPLACED_NAME" {
t.Errorf("invalid index name generated, got %v", idxName)
}
chkName := ns.CheckerName("public.table", "name")
if chkName != "chk_public_table_name" {
t.Errorf("invalid checker name generated, got %v", chkName)
}
joinTable := ns.JoinTableName("user_languages")
if joinTable != "public.REPLACED_USER_LANGUAGES" {
t.Errorf("invalid join table generated, got %v", joinTable)
}
joinTable2 := ns.JoinTableName("UserLanguage")
if joinTable2 != "public.REPLACED_USERLANGUAGE" {
t.Errorf("invalid join table generated, got %v", joinTable2)
}
tableName := ns.TableName("Company")
if tableName != "public.REPLACED_COMPANY" {
t.Errorf("invalid table name generated, got %v", tableName)
}
columdName := ns.ColumnName("", "NameCID")
if columdName != "REPLACED_NAME_Cid" {
t.Errorf("invalid column name generated, got %v", columdName)
}
}
max identifier length changed to 63 (#6337) * max identifier length changed to 63 * default maxIdentifierLength is 64 * renamed License to LICENSE (#6336) * Added support of "Violates Foreign Key Constraint" (#6329) * Added support of "Violates Foreign Key Constraint" Updated the translator and added the support of "foreign key constraint violation". For this, this error type is needed here. * changed the description of ErrForeignKeyViolated * refactor: error translator test (#6350) Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com> * fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements (#6220) * test: add nested transaction and prepareStmt coexist test case note: please test in the MySQL environment Change-Id: I0db32adc5f74b0d443e98943d3b182236583b959 Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> * fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements 1. SavetPoint SQL Statement not support in Prepared Statements e.g. see mysql8.0 doc: https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html Change-Id: I082012db9b140e8ec69764c633724665cc802692 Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> * revert(transaction_api): remove savepoint name pool,meaningless Change-Id: I84aa9924fc54612005a81c83d66fdf8968ee56ad Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> --------- Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com> * fix: save with hook (#6285) (#6294) --------- Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> Co-authored-by: Avinaba Bhattacharjee <avinababhattacharjee2002@gmail.com> Co-authored-by: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com> Co-authored-by: Saeid <sk.saeidee@yahoo.com> Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com> Co-authored-by: wangliuyang <54885906+wangliuyang520@users.noreply.github.com> Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com> Co-authored-by: black-06 <hello.bug@foxmail.com>
2023-05-30 05:00:48 +03:00
func TestFormatNameWithStringLongerThan63Characters(t *testing.T) {
ns := NamingStrategy{IdentifierMaxLength: 63}
formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString")
if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVer180f2c67" {
t.Errorf("invalid formatted name generated, got %v", formattedName)
}
}
func TestFormatNameWithStringLongerThan64Characters(t *testing.T) {
max identifier length changed to 63 (#6337) * max identifier length changed to 63 * default maxIdentifierLength is 64 * renamed License to LICENSE (#6336) * Added support of "Violates Foreign Key Constraint" (#6329) * Added support of "Violates Foreign Key Constraint" Updated the translator and added the support of "foreign key constraint violation". For this, this error type is needed here. * changed the description of ErrForeignKeyViolated * refactor: error translator test (#6350) Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com> * fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements (#6220) * test: add nested transaction and prepareStmt coexist test case note: please test in the MySQL environment Change-Id: I0db32adc5f74b0d443e98943d3b182236583b959 Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> * fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements 1. SavetPoint SQL Statement not support in Prepared Statements e.g. see mysql8.0 doc: https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html Change-Id: I082012db9b140e8ec69764c633724665cc802692 Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> * revert(transaction_api): remove savepoint name pool,meaningless Change-Id: I84aa9924fc54612005a81c83d66fdf8968ee56ad Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> --------- Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com> * fix: save with hook (#6285) (#6294) --------- Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> Co-authored-by: Avinaba Bhattacharjee <avinababhattacharjee2002@gmail.com> Co-authored-by: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com> Co-authored-by: Saeid <sk.saeidee@yahoo.com> Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com> Co-authored-by: wangliuyang <54885906+wangliuyang520@users.noreply.github.com> Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com> Co-authored-by: black-06 <hello.bug@foxmail.com>
2023-05-30 05:00:48 +03:00
ns := NamingStrategy{IdentifierMaxLength: 64}
formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString")
if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVery180f2c67" {
t.Errorf("invalid formatted name generated, got %v", formattedName)
}
}
func TestReplaceEmptyTableName(t *testing.T) {
ns := NamingStrategy{
SingularTable: true,
NameReplacer: strings.NewReplacer("Model", ""),
}
tableName := ns.TableName("Model")
if tableName != "Model" {
t.Errorf("invalid table name generated, got %v", tableName)
}
}