Skip to content

Commit

Permalink
test(types): add native type test (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Mar 22, 2021
1 parent e2ad981 commit 2e2bfe8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
99 changes: 99 additions & 0 deletions test/types/native/default_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})
}
}
18 changes: 18 additions & 0 deletions test/types/native/schema.prisma
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2e2bfe8

Please sign in to comment.