| 1 |
package schema |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestToDBName(t *testing.T) { |
| 9 |
maps := map[string]string{ |
| 10 |
"": "", |
| 11 |
"x": "x", |
| 12 |
"X": "x", |
| 13 |
"userRestrictions": "user_restrictions", |
| 14 |
"ThisIsATest": "this_is_a_test", |
| 15 |
"PFAndESI": "pf_and_esi", |
| 16 |
"AbcAndJkl": "abc_and_jkl", |
| 17 |
"EmployeeID": "employee_id", |
| 18 |
"SKU_ID": "sku_id", |
| 19 |
"FieldX": "field_x", |
| 20 |
"HTTPAndSMTP": "http_and_smtp", |
| 21 |
"HTTPServerHandlerForURLID": "http_server_handler_for_url_id", |
| 22 |
"UUID": "uuid", |
| 23 |
"HTTPURL": "http_url", |
| 24 |
"HTTP_URL": "http_url", |
| 25 |
"SHA256Hash": "sha256_hash", |
| 26 |
"SHA256HASH": "sha256_hash", |
| 27 |
"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", |
| 28 |
} |
| 29 |
|
| 30 |
ns := NamingStrategy{} |
| 31 |
for key, value := range maps { |
| 32 |
if ns.toDBName(key) != value { |
| 33 |
t.Errorf("%v toName should equal %v, but got %v", key, value, ns.toDBName(key)) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
maps = map[string]string{ |
| 38 |
"x": "X", |
| 39 |
"user_restrictions": "UserRestriction", |
| 40 |
"this_is_a_test": "ThisIsATest", |
| 41 |
"abc_and_jkl": "AbcAndJkl", |
| 42 |
"employee_id": "EmployeeID", |
| 43 |
"field_x": "FieldX", |
| 44 |
"http_and_smtp": "HTTPAndSMTP", |
| 45 |
"http_server_handler_for_url_id": "HTTPServerHandlerForURLID", |
| 46 |
"uuid": "UUID", |
| 47 |
"http_url": "HTTPURL", |
| 48 |
"sha256_hash": "Sha256Hash", |
| 49 |
"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", |
| 50 |
} |
| 51 |
for key, value := range maps { |
| 52 |
if ns.SchemaName(key) != value { |
| 53 |
t.Errorf("%v schema name should equal %v, but got %v", key, value, ns.SchemaName(key)) |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestNamingStrategy(t *testing.T) { |
| 59 |
ns := NamingStrategy{ |
| 60 |
TablePrefix: "public.", |
| 61 |
SingularTable: true, |
| 62 |
NameReplacer: strings.NewReplacer("CID", "Cid"), |
| 63 |
} |
| 64 |
idxName := ns.IndexName("public.table", "name") |
| 65 |
|
| 66 |
if idxName != "idx_public_table_name" { |
| 67 |
t.Errorf("invalid index name generated, got %v", idxName) |
| 68 |
} |
| 69 |
|
| 70 |
chkName := ns.CheckerName("public.table", "name") |
| 71 |
if chkName != "chk_public_table_name" { |
| 72 |
t.Errorf("invalid checker name generated, got %v", chkName) |
| 73 |
} |
| 74 |
|
| 75 |
joinTable := ns.JoinTableName("user_languages") |
| 76 |
if joinTable != "public.user_languages" { |
| 77 |
t.Errorf("invalid join table generated, got %v", joinTable) |
| 78 |
} |
| 79 |
|
| 80 |
joinTable2 := ns.JoinTableName("UserLanguage") |
| 81 |
if joinTable2 != "public.user_language" { |
| 82 |
t.Errorf("invalid join table generated, got %v", joinTable2) |
| 83 |
} |
| 84 |
|
| 85 |
tableName := ns.TableName("Company") |
| 86 |
if tableName != "public.company" { |
| 87 |
t.Errorf("invalid table name generated, got %v", tableName) |
| 88 |
} |
| 89 |
|
| 90 |
columdName := ns.ColumnName("", "NameCID") |
| 91 |
if columdName != "name_cid" { |
| 92 |
t.Errorf("invalid column name generated, got %v", columdName) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
type CustomReplacer struct { |
| 97 |
f func(string) string |
| 98 |
} |
| 99 |
|
| 100 |
func (r CustomReplacer) Replace(name string) string { |
| 101 |
return r.f(name) |
| 102 |
} |
| 103 |
|
| 104 |
func TestCustomReplacer(t *testing.T) { |
| 105 |
ns := NamingStrategy{ |
| 106 |
TablePrefix: "public.", |
| 107 |
SingularTable: true, |
| 108 |
NameReplacer: CustomReplacer{ |
| 109 |
func(name string) string { |
| 110 |
replaced := "REPLACED_" + strings.ToUpper(name) |
| 111 |
return strings.NewReplacer("CID", "_Cid").Replace(replaced) |
| 112 |
}, |
| 113 |
}, |
| 114 |
NoLowerCase: false, |
| 115 |
} |
| 116 |
|
| 117 |
idxName := ns.IndexName("public.table", "name") |
| 118 |
if idxName != "idx_public_table_replaced_name" { |
| 119 |
t.Errorf("invalid index name generated, got %v", idxName) |
| 120 |
} |
| 121 |
|
| 122 |
chkName := ns.CheckerName("public.table", "name") |
| 123 |
if chkName != "chk_public_table_name" { |
| 124 |
t.Errorf("invalid checker name generated, got %v", chkName) |
| 125 |
} |
| 126 |
|
| 127 |
joinTable := ns.JoinTableName("user_languages") |
| 128 |
if joinTable != "public.user_languages" { // Seems like a bug in NamingStrategy to skip the Replacer when the name is lowercase here. |
| 129 |
t.Errorf("invalid join table generated, got %v", joinTable) |
| 130 |
} |
| 131 |
|
| 132 |
joinTable2 := ns.JoinTableName("UserLanguage") |
| 133 |
if joinTable2 != "public.replaced_userlanguage" { |
| 134 |
t.Errorf("invalid join table generated, got %v", joinTable2) |
| 135 |
} |
| 136 |
|
| 137 |
tableName := ns.TableName("Company") |
| 138 |
if tableName != "public.replaced_company" { |
| 139 |
t.Errorf("invalid table name generated, got %v", tableName) |
| 140 |
} |
| 141 |
|
| 142 |
columdName := ns.ColumnName("", "NameCID") |
| 143 |
if columdName != "replaced_name_cid" { |
| 144 |
t.Errorf("invalid column name generated, got %v", columdName) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestCustomReplacerWithNoLowerCase(t *testing.T) { |
| 149 |
ns := NamingStrategy{ |
| 150 |
TablePrefix: "public.", |
| 151 |
SingularTable: true, |
| 152 |
NameReplacer: CustomReplacer{ |
| 153 |
func(name string) string { |
| 154 |
replaced := "REPLACED_" + strings.ToUpper(name) |
| 155 |
return strings.NewReplacer("CID", "_Cid").Replace(replaced) |
| 156 |
}, |
| 157 |
}, |
| 158 |
NoLowerCase: true, |
| 159 |
} |
| 160 |
|
| 161 |
idxName := ns.IndexName("public.table", "name") |
| 162 |
if idxName != "idx_public_table_REPLACED_NAME" { |
| 163 |
t.Errorf("invalid index name generated, got %v", idxName) |
| 164 |
} |
| 165 |
|
| 166 |
chkName := ns.CheckerName("public.table", "name") |
| 167 |
if chkName != "chk_public_table_name" { |
| 168 |
t.Errorf("invalid checker name generated, got %v", chkName) |
| 169 |
} |
| 170 |
|
| 171 |
joinTable := ns.JoinTableName("user_languages") |
| 172 |
if joinTable != "public.REPLACED_USER_LANGUAGES" { |
| 173 |
t.Errorf("invalid join table generated, got %v", joinTable) |
| 174 |
} |
| 175 |
|
| 176 |
joinTable2 := ns.JoinTableName("UserLanguage") |
| 177 |
if joinTable2 != "public.REPLACED_USERLANGUAGE" { |
| 178 |
t.Errorf("invalid join table generated, got %v", joinTable2) |
| 179 |
} |
| 180 |
|
| 181 |
tableName := ns.TableName("Company") |
| 182 |
if tableName != "public.REPLACED_COMPANY" { |
| 183 |
t.Errorf("invalid table name generated, got %v", tableName) |
| 184 |
} |
| 185 |
|
| 186 |
columdName := ns.ColumnName("", "NameCID") |
| 187 |
if columdName != "REPLACED_NAME_Cid" { |
| 188 |
t.Errorf("invalid column name generated, got %v", columdName) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestFormatNameWithStringLongerThan63Characters(t *testing.T) { |
| 193 |
ns := NamingStrategy{IdentifierMaxLength: 63} |
| 194 |
|
| 195 |
formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString") |
| 196 |
if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVer180f2c67" { |
| 197 |
t.Errorf("invalid formatted name generated, got %v", formattedName) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func TestFormatNameWithStringLongerThan64Characters(t *testing.T) { |
| 202 |
ns := NamingStrategy{IdentifierMaxLength: 64} |
| 203 |
|
| 204 |
formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString") |
| 205 |
if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVery180f2c67" { |
| 206 |
t.Errorf("invalid formatted name generated, got %v", formattedName) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestReplaceEmptyTableName(t *testing.T) { |
| 211 |
ns := NamingStrategy{ |
| 212 |
SingularTable: true, |
| 213 |
NameReplacer: strings.NewReplacer("Model", ""), |
| 214 |
} |
| 215 |
tableName := ns.TableName("Model") |
| 216 |
if tableName != "Model" { |
| 217 |
t.Errorf("invalid table name generated, got %v", tableName) |
| 218 |
} |
| 219 |
} |