MCPcopy
hub / github.com/go-gorm/gorm / TestMigrateWithUniqueIndexAndUnique

Function TestMigrateWithUniqueIndexAndUnique

tests/migrate_test.go:1875–2088  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1873}
1874
1875func TestMigrateWithUniqueIndexAndUnique(t *testing.T) {
1876 const table = "unique_struct"
1877
1878 checkField := func(model interface{}, fieldName string, unique bool, uniqueIndex string) {
1879 stmt := &gorm.Statement{DB: DB}
1880 err := stmt.Parse(model)
1881 if err != nil {
1882 t.Fatalf("%v: failed to parse schema, got error: %v", utils.FileWithLineNum(), err)
1883 }
1884 _ = stmt.Schema.ParseIndexes()
1885 field := stmt.Schema.LookUpField(fieldName)
1886 if field == nil {
1887 t.Fatalf("%v: failed to find column %q", utils.FileWithLineNum(), fieldName)
1888 }
1889 if field.Unique != unique {
1890 t.Fatalf("%v: %q column %q unique should be %v but got %v", utils.FileWithLineNum(), stmt.Schema.Table, fieldName, unique, field.Unique)
1891 }
1892 if field.UniqueIndex != uniqueIndex {
1893 t.Fatalf("%v: %q column %q uniqueIndex should be %v but got %v", utils.FileWithLineNum(), stmt.Schema, fieldName, uniqueIndex, field.UniqueIndex)
1894 }
1895 }
1896
1897 type ( // not unique
1898 UniqueStruct1 struct {
1899 Name string `gorm:"size:10"`
1900 }
1901 UniqueStruct2 struct {
1902 Name string `gorm:"size:20"`
1903 }
1904 )
1905 checkField(&UniqueStruct1{}, "name", false, "")
1906 checkField(&UniqueStruct2{}, "name", false, "")
1907
1908 type ( // unique
1909 UniqueStruct3 struct {
1910 Name string `gorm:"size:30;unique"`
1911 }
1912 UniqueStruct4 struct {
1913 Name string `gorm:"size:40;unique"`
1914 }
1915 )
1916 checkField(&UniqueStruct3{}, "name", true, "")
1917 checkField(&UniqueStruct4{}, "name", true, "")
1918
1919 type ( // uniqueIndex
1920 UniqueStruct5 struct {
1921 Name string `gorm:"size:50;uniqueIndex"`
1922 }
1923 UniqueStruct6 struct {
1924 Name string `gorm:"size:60;uniqueIndex"`
1925 }
1926 UniqueStruct7 struct {
1927 Name string `gorm:"size:70;uniqueIndex:idx_us6_all_names"`
1928 NickName string `gorm:"size:70;uniqueIndex:idx_us6_all_names"`
1929 }
1930 )
1931 checkField(&UniqueStruct5{}, "name", false, "idx_unique_struct5_name")
1932 checkField(&UniqueStruct6{}, "name", false, "idx_unique_struct6_name")

Callers

nothing calls this directly

Calls 15

ParseMethod · 0.95
NameMethod · 0.95
UniqueMethod · 0.95
FileWithLineNumFunction · 0.92
checkFieldFunction · 0.85
ParseIndexesMethod · 0.80
LookUpFieldMethod · 0.80
ColumnTypesMethod · 0.65
MigratorMethod · 0.65
GetIndexesMethod · 0.65
IndexNameMethod · 0.65
UniqueNameMethod · 0.65

Tested by

no test coverage detected