-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshaller_test.go
110 lines (84 loc) · 1.93 KB
/
marshaller_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
100
101
102
103
104
105
106
107
108
109
110
package graphql_test
import (
"bytes"
"fmt"
"math/big"
"testing"
"time"
"flamingo.me/graphql"
)
func TestMarshalFloats(t *testing.T) {
t.Parallel()
i := new(big.Float).SetFloat64(1)
v, err := graphql.UnmarshalFloat("1")
if err != nil {
t.Error(err)
}
if v.Cmp(i) != 0 {
t.Error("string unmatch")
}
v, err = graphql.UnmarshalFloat(1)
if err != nil {
t.Error(err)
}
if v.Cmp(i) != 0 {
t.Error("int unmatch")
}
v, err = graphql.UnmarshalFloat(int64(1))
if err != nil {
t.Error(err)
}
if v.Cmp(i) != 0 {
t.Error("int64 unmatch")
}
v, err = graphql.UnmarshalFloat(float64(1.0))
if err != nil {
t.Error(err)
}
if v.Cmp(i) != 0 {
t.Error("float64 unmatch")
}
_, err = graphql.UnmarshalFloat("test")
if err == nil {
t.Error("invalid float error fails")
}
}
func TestMarshalDates(t *testing.T) {
t.Parallel()
writer := graphql.MarshalDate(time.Time{})
b := bytes.NewBufferString("")
writer.MarshalGQL(b)
if b.String() != "null" {
t.Error("zero date should be null")
}
now := time.Now()
writer = graphql.MarshalDate(now)
b = bytes.NewBufferString("")
writer.MarshalGQL(b)
if b.String() != fmt.Sprintf("%q", now.Format("2006-01-02")) {
t.Error("date should be marshalled to format YYYY-MM-DD")
}
date, err := graphql.UnmarshalDate("2011-08-12")
if err != nil {
t.Fatal("Unmarshal of correct date string should work")
}
expectedDate, _ := time.Parse("2006-01-02", "2011-08-12")
if !date.Equal(expectedDate) {
t.Error("Umarshalled date is wrong")
}
_, err = graphql.UnmarshalDate("foobar")
if err == nil {
t.Error("Unmarshal of invalid date string should lead to an error")
}
_, err = graphql.UnmarshalDate(42)
if err == nil {
t.Error("Unmarshal of invalid date type should lead to an error")
}
date, err = graphql.UnmarshalDate("")
if err != nil {
t.Fatal("Unmarshal of empty string should work")
}
if !date.Equal(time.Time{}) {
t.Fatal("Date should be empty time")
}
}