|
| 1 | +package graphql_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/graphql-go/graphql" |
| 9 | + "github.com/graphql-go/graphql/language/ast" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTypeSystem_Scalar_ParseValueOutputDateTime(t *testing.T) { |
| 13 | + t1, _ := time.Parse(time.RFC3339, "2017-07-23T03:46:56.647Z") |
| 14 | + tests := []dateTimeSerializationTest{ |
| 15 | + {nil, nil}, |
| 16 | + {"", nil}, |
| 17 | + {(*string)(nil), nil}, |
| 18 | + {"2017-07-23", nil}, |
| 19 | + {"2017-07-23T03:46:56.647Z", t1}, |
| 20 | + } |
| 21 | + for _, test := range tests { |
| 22 | + val := graphql.DateTime.ParseValue(test.Value) |
| 23 | + if val != test.Expected { |
| 24 | + reflectedValue := reflect.ValueOf(test.Value) |
| 25 | + t.Fatalf("failed DateTime.ParseValue(%v(%v)), expected: %v, got %v", reflectedValue.Type(), test.Value, test.Expected, val) |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestTypeSystem_Scalar_ParseLiteralOutputDateTime(t *testing.T) { |
| 31 | + t1, _ := time.Parse(time.RFC3339, "2017-07-23T03:46:56.647Z") |
| 32 | + for name, testCase := range map[string]struct { |
| 33 | + Literal ast.Value |
| 34 | + Expected interface{} |
| 35 | + }{ |
| 36 | + "String": { |
| 37 | + Literal: &ast.StringValue{ |
| 38 | + Value: "2017-07-23T03:46:56.647Z", |
| 39 | + }, |
| 40 | + Expected: t1, |
| 41 | + }, |
| 42 | + "NotAString": { |
| 43 | + Literal: &ast.IntValue{}, |
| 44 | + Expected: nil, |
| 45 | + }, |
| 46 | + } { |
| 47 | + t.Run(name, func(t *testing.T) { |
| 48 | + parsed := graphql.DateTime.ParseLiteral(testCase.Literal) |
| 49 | + if parsed != testCase.Expected { |
| 50 | + t.Fatalf("failed DateTime.ParseLiteral(%T(%v)), expected: %v, got %v", testCase.Literal, testCase.Literal, parsed, testCase.Expected) |
| 51 | + } |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
0 commit comments