From 2e2bfe899df67e97626ce376f60d36de57edf31f Mon Sep 17 00:00:00 2001 From: Luca Steeb Date: Mon, 22 Mar 2021 22:29:11 +0700 Subject: [PATCH] test(types): add native type test (#446) --- test/types/native/default_test.go | 99 +++++++++++++++++++++++++++++++ test/types/native/schema.prisma | 18 ++++++ 2 files changed, 117 insertions(+) create mode 100644 test/types/native/default_test.go create mode 100644 test/types/native/schema.prisma diff --git a/test/types/native/default_test.go b/test/types/native/default_test.go new file mode 100644 index 00000000..7d560ce6 --- /dev/null +++ b/test/types/native/default_test.go @@ -0,0 +1,99 @@ +package db + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/prisma/prisma-client-go/test" +) + +type cx = context.Context +type Func func(t *testing.T, client *PrismaClient, ctx cx) + +func TestNative(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + before []string + run Func + }{{ + name: "native create", + run: func(t *testing.T, client *PrismaClient, ctx cx) { + a := "a" + b := "b" + created, err := client.User.CreateOne( + User.A.Set(a), + User.B.Set(b), + User.ID.Set("123"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + expected := &UserModel{ + InnerUser: InnerUser{ + ID: "123", + A: a, + B: &b, + }, + } + + assert.Equal(t, expected, created) + + actual, err := client.User.FindUnique(User.ID.Equals(created.ID)).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + assert.Equal(t, expected, actual) + }, + }, { + name: "native find by native field", + run: func(t *testing.T, client *PrismaClient, ctx cx) { + a := "a" + b := "b" + created, err := client.User.CreateOne( + User.A.Set(a), + User.B.Set(b), + User.ID.Set("123"), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + expected := &UserModel{ + InnerUser: InnerUser{ + ID: "123", + A: a, + B: &b, + }, + } + + assert.Equal(t, expected, created) + + actual, err := client.User.FindFirst( + User.A.Equals(a), + User.B.Equals(b), + ).Exec(ctx) + if err != nil { + t.Fatalf("fail %s", err) + } + + assert.Equal(t, expected, actual) + }, + }} + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + test.RunSerial(t, []test.Database{test.MySQL, test.PostgreSQL}, func(t *testing.T, db test.Database, ctx context.Context) { + client := NewClient() + mockDBName := test.Start(t, db, client.Engine, tt.before) + defer test.End(t, db, client.Engine, mockDBName) + tt.run(t, client, context.Background()) + }) + }) + } +} diff --git a/test/types/native/schema.prisma b/test/types/native/schema.prisma new file mode 100644 index 00000000..3f8eb2d1 --- /dev/null +++ b/test/types/native/schema.prisma @@ -0,0 +1,18 @@ +datasource db { + provider = "postgresql" + url = env("__REPLACE__") +} + +generator db { + provider = "go run github.com/prisma/prisma-client-go" + output = "." + disableGoBinaries = true + package = "db" + previewFeatures = ["nativeTypes"] +} + +model User { + id String @id @default(cuid()) + a String @db.VarChar(5) + b String? @db.VarChar(50) +}