mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql/sqlgraph: add constraint error checks (#1316)
Initial work on #1310
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/entc/integration/ent"
|
||||
"entgo.io/ent/entc/integration/ent/enttest"
|
||||
"entgo.io/ent/entc/integration/ent/file"
|
||||
@@ -135,6 +136,7 @@ var (
|
||||
EagerLoading,
|
||||
Mutation,
|
||||
CreateBulk,
|
||||
ConstraintChecks,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1352,6 +1354,20 @@ func CreateBulk(t *testing.T, client *ent.Client) {
|
||||
require.False(t, pets[2].QueryOwner().ExistX(ctx))
|
||||
}
|
||||
|
||||
func ConstraintChecks(t *testing.T, client *ent.Client) {
|
||||
_, err := client.Pet.Create().SetName("orphan").SetOwnerID(0).Save(context.Background())
|
||||
require.True(t, sqlgraph.IsConstraintError(err))
|
||||
require.True(t, sqlgraph.IsForeignKeyConstraintError(err))
|
||||
require.False(t, sqlgraph.IsUniqueConstraintError(err))
|
||||
|
||||
client.FileType.Create().SetName("a unique name").SaveX(context.Background())
|
||||
_, err = client.FileType.Create().SetName("a unique name").Save(context.Background())
|
||||
t.Logf("err %T %+v", err, err)
|
||||
require.True(t, sqlgraph.IsConstraintError(err))
|
||||
require.False(t, sqlgraph.IsForeignKeyConstraintError(err))
|
||||
require.True(t, sqlgraph.IsUniqueConstraintError(err))
|
||||
}
|
||||
|
||||
func drop(t *testing.T, client *ent.Client) {
|
||||
t.Log("drop data from database")
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user