-
Notifications
You must be signed in to change notification settings - Fork 5
/
prisma_test.go
99 lines (81 loc) · 2.09 KB
/
prisma_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package goprisma
import (
"fmt"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
queryString = `postgresql://admin:admin@localhost:54322/example?schema=public&connection_limit=20&pool_timeout=5`
)
func TestIntrospect(t *testing.T) {
schema := fmt.Sprintf(`datasource db {
provider = "%s"
url = "%s"
}`, "postgresql", queryString)
schema, sdl, err := Introspect(schema)
assert.NoError(t, err)
assert.NotEqual(t, "", schema)
assert.NotEqual(t, "", sdl)
}
func TestNewEngine(t *testing.T) {
schema := fmt.Sprintf(`datasource db {
provider = "%s"
url = "%s"
}`, "postgresql", queryString)
query := `{
"query": "query Messages {findManymessages(take: 20 orderBy: [{id: desc}]){id message users {id name}}}",
"variables": {}
}`
engine, err := NewEngine(schema)
assert.NoError(t, err)
assert.NotNil(t, engine)
defer engine.Close()
response,err := engine.Execute(query)
assert.NoError(t, err)
if strings.Contains(response, "errors") {
t.Fatal(response)
}
}
func BenchmarkEngine_Execute(b *testing.B) {
schema := fmt.Sprintf(`datasource db {
provider = "%s"
url = "%s"
}`, "postgresql", queryString)
query := `{
"query": "query Messages {findManymessages(take: 20 orderBy: [{id: desc}]){id message users {id name}}}",
"variables": {}
}`
engine, err := NewEngine(schema)
if err != nil {
b.Fatal(err)
}
defer engine.Close()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
response,err := engine.Execute(query)
if err != nil {
b.Fatal(err)
}
if strings.Contains(response, "errors") {
b.Fatal(response)
}
}
})
}
func PrintMemUsage() {
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v KiB", bToMb(m.Alloc)/1024)
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}