50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package gorm
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
|
)
|
|
|
|
func TestBuildGormTag_IdentityColumn(t *testing.T) {
|
|
mapper := NewTypeMapper("")
|
|
|
|
build := func(pk, identity bool, generation string) *models.Column {
|
|
col := models.InitColumn("id", "users", "public")
|
|
col.Type = "bigint"
|
|
col.IsPrimaryKey = pk
|
|
col.Identity = identity
|
|
col.IdentityGeneration = generation
|
|
return col
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
column *models.Column
|
|
wantFlag bool
|
|
wantMarker bool
|
|
}{
|
|
{"always identity non-pk is write-blocked", build(false, true, "ALWAYS"), true, true},
|
|
{"always identity pk is only marked", build(true, true, "ALWAYS"), false, true},
|
|
{"by default identity is writable", build(false, true, "BY DEFAULT"), false, false},
|
|
{"non-identity is untouched", build(false, false, ""), false, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tag := mapper.BuildGormTag(tt.column, nil)
|
|
has := func(s string) bool {
|
|
for _, p := range strings.Split(tag, ";") {
|
|
if p == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
if has("<-:false") != tt.wantFlag || has("identity") != tt.wantMarker {
|
|
t.Errorf("tag %q: <-:false/identity = %v/%v, want %v/%v", tag, has("<-:false"), has("identity"), tt.wantFlag, tt.wantMarker)
|
|
}
|
|
})
|
|
}
|
|
}
|