|
| 1 | +package graphql_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + . "github.com/graph-gophers/graphql-go" |
| 10 | + "github.com/graph-gophers/graphql-go/decode" |
| 11 | +) |
| 12 | + |
| 13 | +func TestTime_ImplementsUnmarshaler(t *testing.T) { |
| 14 | + defer func() { |
| 15 | + if err := recover(); err != nil { |
| 16 | + t.Error(err) |
| 17 | + } |
| 18 | + }() |
| 19 | + |
| 20 | + // assert *Time implements decode.Unmarshaler interface |
| 21 | + var _ decode.Unmarshaler = (*Time)(nil) |
| 22 | +} |
| 23 | + |
| 24 | +func TestTime_ImplementsGraphQLType(t *testing.T) { |
| 25 | + gt := new(Time) |
| 26 | + |
| 27 | + if gt.ImplementsGraphQLType("foobar") { |
| 28 | + t.Error("Type *Time must not claim to implement GraphQL type 'foobar'") |
| 29 | + } |
| 30 | + |
| 31 | + if !gt.ImplementsGraphQLType("Time") { |
| 32 | + t.Error("Failed asserting *Time implements GraphQL type Time") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestTime_MarshalJSON(t *testing.T) { |
| 37 | + var err error |
| 38 | + var b1, b2 []byte |
| 39 | + ref := time.Date(2021, time.April, 20, 12, 3, 23, 0, time.UTC) |
| 40 | + |
| 41 | + if b1, err = json.Marshal(ref); err != nil { |
| 42 | + t.Error(err) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + if b2, err = json.Marshal(Time{Time: ref}); err != nil { |
| 47 | + t.Errorf("MarshalJSON() error = %v", err) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + if !bytes.Equal(b1, b2) { |
| 52 | + t.Errorf("MarshalJSON() got = %s, want = %s", b2, b1) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestTime_UnmarshalGraphQL(t *testing.T) { |
| 57 | + type args struct { |
| 58 | + input interface{} |
| 59 | + } |
| 60 | + |
| 61 | + ref := time.Date(2021, time.April, 20, 12, 3, 23, 0, time.UTC) |
| 62 | + |
| 63 | + t.Run("invalid", func(t *testing.T) { |
| 64 | + tests := []struct { |
| 65 | + name string |
| 66 | + args args |
| 67 | + wantErr string |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "boolean", |
| 71 | + args: args{input: true}, |
| 72 | + wantErr: "wrong type for Time: bool", |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "invalid format", |
| 76 | + args: args{input: ref.Format(time.ANSIC)}, |
| 77 | + wantErr: `parsing time "Tue Apr 20 12:03:23 2021" as "2006-01-02T15:04:05Z07:00": cannot parse "Tue Apr 20 12:03:23 2021" as "2006"`, |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tt := range tests { |
| 82 | + t.Run(tt.name, func(t *testing.T) { |
| 83 | + gt := new(Time) |
| 84 | + if err := gt.UnmarshalGraphQL(tt.args.input); err != nil { |
| 85 | + if err.Error() != tt.wantErr { |
| 86 | + t.Errorf("UnmarshalGraphQL() error = %v, want = %s", err, tt.wantErr) |
| 87 | + } |
| 88 | + |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + t.Error("UnmarshalGraphQL() expected error not raised") |
| 93 | + }) |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + args args |
| 100 | + wantEq time.Time |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "time.Time", |
| 104 | + args: args{ |
| 105 | + input: ref, |
| 106 | + }, |
| 107 | + wantEq: ref, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "string", |
| 111 | + args: args{ |
| 112 | + input: ref.Format(time.RFC3339), |
| 113 | + }, |
| 114 | + wantEq: ref, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "bytes", |
| 118 | + args: args{ |
| 119 | + input: []byte(ref.Format(time.RFC3339)), |
| 120 | + }, |
| 121 | + wantEq: ref, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "int32", |
| 125 | + args: args{ |
| 126 | + input: int32(ref.Unix()), |
| 127 | + }, |
| 128 | + wantEq: ref, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "int64", |
| 132 | + args: args{ |
| 133 | + input: ref.Unix(), |
| 134 | + }, |
| 135 | + wantEq: ref, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "float64", |
| 139 | + args: args{ |
| 140 | + input: float64(ref.Unix()), |
| 141 | + }, |
| 142 | + wantEq: ref, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + for _, tt := range tests { |
| 147 | + t.Run(tt.name, func(t *testing.T) { |
| 148 | + gt := new(Time) |
| 149 | + if err := gt.UnmarshalGraphQL(tt.args.input); err != nil { |
| 150 | + t.Errorf("UnmarshalGraphQL() error = %v", err) |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + if !gt.Equal(tt.wantEq) { |
| 155 | + t.Errorf("UnmarshalGraphQL() got = %v, want = %v", gt, tt.wantEq) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
0 commit comments