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

@@ -8,7 +8,9 @@ package fieldtype
import (
"fmt"
"net"
"github.com/facebook/ent/dialect/sql"
"github.com/facebook/ent/entc/integration/ent/role"
)
@@ -193,8 +195,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

@@ -612,6 +612,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,8 +7,10 @@
package ent
import (
"net"
"time"
"github.com/facebook/ent/dialect/sql"
"github.com/facebook/ent/entc/integration/ent/card"
"github.com/facebook/ent/entc/integration/ent/fieldtype"
"github.com/facebook/ent/entc/integration/ent/file"
@@ -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.

View File

@@ -79,11 +79,17 @@ func (FieldType) Fields() []ent.Field {
GoType(http.Dir("ndir")),
field.String("str").
Optional().
GoType(&sql.NullString{}),
GoType(&sql.NullString{}).
DefaultFunc(func() sql.NullString {
return sql.NullString{String: "default", Valid: true}
}),
field.String("null_str").
Optional().
Nillable().
GoType(&sql.NullString{}),
GoType(&sql.NullString{}).
DefaultFunc(func() sql.NullString {
return sql.NullString{String: "default", Valid: true}
}),
field.String("link").
Optional().
NotEmpty().
@@ -107,7 +113,10 @@ func (FieldType) Fields() []ent.Field {
GoType(&sql.NullTime{}),
field.Bytes("ip").
Optional().
GoType(net.IP("127.0.0.1")),
GoType(net.IP("127.0.0.1")).
DefaultFunc(func() net.IP {
return net.IP("127.0.0.1")
}),
field.Int("null_int64").
Optional().
GoType(&sql.NullInt64{}),

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.

View File

@@ -61,8 +61,7 @@ func Types(t *testing.T, client *ent.Client) {
SetNillableInt64(math.MinInt64).
SetDir("dir").
SetNdir("ndir").
SetStr(sql.NullString{String: "str", Valid: true}).
SetNullStr(sql.NullString{String: "str", Valid: true}).
SetNullStr(sql.NullString{String: "not-default", Valid: true}).
SetLink(schema.Link{URL: link}).
SetNullLink(schema.Link{URL: link}).
SetRole(role.Admin).
@@ -79,10 +78,11 @@ func Types(t *testing.T, client *ent.Client) {
require.Equal(http.Dir("dir"), ft.Dir)
require.NotNil(*ft.Ndir)
require.Equal(http.Dir("ndir"), *ft.Ndir)
require.Equal("str", ft.Str.String)
require.Equal("str", ft.NullStr.String)
require.Equal("default", ft.Str.String)
require.Equal("not-default", ft.NullStr.String)
require.Equal("localhost", ft.Link.String())
require.Equal("localhost", ft.NullLink.String())
require.Equal(net.IP("127.0.0.1").String(), ft.IP.String())
mac, err := net.ParseMAC("3b:b3:6b:3c:10:79")
require.NoError(err)
dt, err := time.Parse(time.RFC3339, "1906-01-02T00:00:00+00:00")