entc/integ: add tests for default-func

This commit is contained in:
Ariel Mashraki
2021-01-04 11:45:36 +02:00
parent d514d12dab
commit 2ab0c682db
10 changed files with 86 additions and 11 deletions

View File

@@ -7,7 +7,9 @@
package fieldtype
import (
"database/sql"
"fmt"
"net"
"github.com/facebook/ent/entc/integration/ent/role"
)
@@ -118,8 +120,14 @@ var (
ValidateOptionalInt32Validator func(int32) error
// NdirValidator is a validator for the "ndir" field. It is called by the builders before save.
NdirValidator func(string) error
// DefaultStr holds the default value on creation for the str field.
DefaultStr func() sql.NullString
// DefaultNullStr holds the default value on creation for the null_str field.
DefaultNullStr func() sql.NullString
// LinkValidator is a validator for the "link" field. It is called by the builders before save.
LinkValidator func(string) error
// DefaultIP holds the default value on creation for the ip field.
DefaultIP func() net.IP
// MACValidator is a validator for the "mac" field. It is called by the builders before save.
MACValidator func(string) error
)

View File

@@ -613,6 +613,18 @@ func (ftc *FieldTypeCreate) SaveX(ctx context.Context) *FieldType {
// defaults sets the default values of the builder before save.
func (ftc *FieldTypeCreate) defaults() {
if _, ok := ftc.mutation.Str(); !ok {
v := fieldtype.DefaultStr()
ftc.mutation.SetStr(v)
}
if _, ok := ftc.mutation.NullStr(); !ok {
v := fieldtype.DefaultNullStr()
ftc.mutation.SetNullStr(v)
}
if _, ok := ftc.mutation.IP(); !ok {
v := fieldtype.DefaultIP()
ftc.mutation.SetIP(v)
}
if _, ok := ftc.mutation.Role(); !ok {
v := fieldtype.DefaultRole
ftc.mutation.SetRole(v)

View File

@@ -7,6 +7,8 @@
package ent
import (
"database/sql"
"net"
"time"
"github.com/facebook/ent/entc/integration/ent/schema"
@@ -56,10 +58,22 @@ func init() {
fieldtypeDescNdir := fieldtypeFields[27].Descriptor()
// fieldtype.NdirValidator is a validator for the "ndir" field. It is called by the builders before save.
fieldtype.NdirValidator = fieldtypeDescNdir.Validators[0].(func(string) error)
// fieldtypeDescStr is the schema descriptor for str field.
fieldtypeDescStr := fieldtypeFields[28].Descriptor()
// fieldtype.DefaultStr holds the default value on creation for the str field.
fieldtype.DefaultStr = fieldtypeDescStr.Default.(func() sql.NullString)
// fieldtypeDescNullStr is the schema descriptor for null_str field.
fieldtypeDescNullStr := fieldtypeFields[29].Descriptor()
// fieldtype.DefaultNullStr holds the default value on creation for the null_str field.
fieldtype.DefaultNullStr = fieldtypeDescNullStr.Default.(func() sql.NullString)
// fieldtypeDescLink is the schema descriptor for link field.
fieldtypeDescLink := fieldtypeFields[30].Descriptor()
// fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save.
fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error)
// fieldtypeDescIP is the schema descriptor for ip field.
fieldtypeDescIP := fieldtypeFields[36].Descriptor()
// fieldtype.DefaultIP holds the default value on creation for the ip field.
fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP)
// fieldtypeDescMAC is the schema descriptor for mac field.
fieldtypeDescMAC := fieldtypeFields[45].Descriptor()
// fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save.