- bun: scanonly+generated for GENERATED columns; scanonly+identity for ALWAYS identity (PK gets identity only) - gorm: <-:false+generated / identity, same primary key rule - dbml: write GENERATED/IDENTITY column notes and read them back into the model - pgsql: emit generation/identity clauses in migration create-table and add-column, no DEFAULT - readmes updated
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package bun
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
|
)
|
|
|
|
func TestBuildBunTag_GeneratedColumn(t *testing.T) {
|
|
mapper := NewTypeMapper("", "")
|
|
|
|
generated := models.InitColumn("full_name", "users", "public")
|
|
generated.Type = "text"
|
|
generated.Generated = true
|
|
generated.GenerationExpression = "first || ' ' || last"
|
|
|
|
plain := models.InitColumn("first", "users", "public")
|
|
plain.Type = "text"
|
|
|
|
tests := []struct {
|
|
name string
|
|
column *models.Column
|
|
want bool
|
|
}{
|
|
{"generated column is scanonly and marked", generated, true},
|
|
{"ordinary column is untouched", plain, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tag := mapper.BuildBunTag(tt.column, nil)
|
|
parts := strings.Split(tag, ",")
|
|
has := func(s string) bool {
|
|
for _, p := range parts {
|
|
if p == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
if has("scanonly") != tt.want || has("generated") != tt.want {
|
|
t.Errorf("tag %q: scanonly/generated present = %v/%v, want %v", tag, has("scanonly"), has("generated"), tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|