Skip to content

Commit

Permalink
feat(client): introduce Decimal type (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Mar 22, 2021
1 parent 4bdbae9 commit e2ad981
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 2 deletions.
1 change: 1 addition & 0 deletions generator/templates/_header.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type DateTime = types.DateTime
type JSON = types.JSON
type Bytes = types.Bytes
type BigInt = types.BigInt
type Decimal = types.Decimal

type Direction = types.Direction

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
github.com/shopspring/decimal v1.2.0
github.com/stretchr/testify v1.4.0
github.com/takuoki/gocase v1.0.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 h1:ECW73yc9MY79
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
Expand Down
5 changes: 5 additions & 0 deletions runtime/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"strconv"
"time"

"github.com/shopspring/decimal"
)

const RFC3339Milli = "2006-01-02T15:04:05.999Z07:00"
Expand All @@ -17,6 +19,9 @@ type BatchResult struct {
// DateTime is a type alias for time.Time
type DateTime = time.Time

// Decimal points to github.com/shopspring/decimal.Decimal, as Go does not have a native decimal type
type Decimal = decimal.Decimal

// Bytes is a type alias for []byte
type Bytes = []byte

Expand Down
2 changes: 1 addition & 1 deletion test/types/bigint/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type cx = context.Context
type Func func(t *testing.T, client *PrismaClient, ctx cx)

func TestJSON(t *testing.T) {
func TestBigInt(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion test/types/bytes/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type cx = context.Context
type Func func(t *testing.T, client *PrismaClient, ctx cx)

func TestJSON(t *testing.T) {
func TestBytes(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down
100 changes: 100 additions & 0 deletions test/types/decimal/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package db

import (
"context"
"testing"

"github.com/shopspring/decimal"
"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 TestDecimal(t *testing.T) {
t.Parallel()

tests := []struct {
name string
before []string
run Func
}{{
name: "decimal create",
run: func(t *testing.T, client *PrismaClient, ctx cx) {
a := decimal.NewFromFloat(1.23456789)
b := decimal.NewFromFloat(2.34567891)
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: "decimal find by decimal field",
run: func(t *testing.T, client *PrismaClient, ctx cx) {
a := decimal.NewFromFloat(1.23456789)
b := decimal.NewFromFloat(2.34567891)
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())
})
})
}
}
17 changes: 17 additions & 0 deletions test/types/decimal/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
datasource db {
provider = "postgresql"
url = env("__REPLACE__")
}

generator db {
provider = "go run github.com/prisma/prisma-client-go"
output = "."
disableGoBinaries = true
package = "db"
}

model User {
id String @id @default(cuid())
a Decimal
b Decimal?
}

0 comments on commit e2ad981

Please sign in to comment.