Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue graphql.Time unmarshal time check #432 #438

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion time.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ func (t *Time) UnmarshalGraphQL(input interface{}) error {
t.Time = time.Unix(int64(input), 0)
return nil
case int64:
t.Time = time.Unix(input, 0)
if input >= 1e10 {
sec := input / 1e9
nsec := input - (sec * 1e9)
t.Time = time.Unix(sec, nsec)
} else {
t.Time = time.Unix(input, 0)
}
return nil
case float64:
t.Time = time.Unix(int64(input), 0)
Expand Down
50 changes: 50 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package graphql

import (
"testing"
"time"
)

var timeTestCases = []interface{}{
time.Now(),
int32(1),
time.Now().Unix(),
time.Now().UnixNano(),
float64(-1),
"2006-01-02T15:04:05Z",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you, pelase, check the expected values too? So that each test has got, want pair. This way you will not only check that marshalling and unmarshalling work but also that they return what you expect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I wouldn't use time.Now() instead I prefer constants, please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I added the checks for expeced values with assertion library and remove time.Now() usage.
Can you please check if it is ok for you, Thx!

}

func TestImplementsGraphQLType(t *testing.T) {
time := Time{}
if !time.ImplementsGraphQLType("Time") {
t.Fail()
}
}

func TestUnmarshalGraphQL(t *testing.T) {
var err error
testTime := &Time{}

for _, timeType := range timeTestCases {
if err = testTime.UnmarshalGraphQL(timeType); err != nil {
t.Fatalf("Failed to unmarshal %#v to Time: %s", timeType, err.Error())
}
}

if err = testTime.UnmarshalGraphQL(false); err == nil {
t.Fatalf("Unmarshaling of %T to Time should be failed.", false)
}
}

func TestMarshalJSON(t *testing.T) {
exampleTime := "\"0001-01-01T00:00:00Z\""
testTime := &Time{}

buf, err := testTime.MarshalJSON()
if err != nil {
t.Fatalf("Failed to marshal time to JSON: %s", err.Error())
}
if string(buf) != exampleTime {
t.Fatalf("Failed to marshal Time to JSON, expected %s, but instead got %s", exampleTime, buf)
}
}