Skip to content

Commit

Permalink
Merge pull request #213 from dennisfaust/null_tests
Browse files Browse the repository at this point in the history
Null tests
  • Loading branch information
sharpner committed Oct 27, 2015
2 parents b6eb5c0 + fc825e4 commit 4b5c8ff
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
1 change: 1 addition & 0 deletions jsonapi/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ type SQLNullPost struct {
Likes zero.Int
Rating zero.Float
IsCool zero.Bool
Today zero.Time
}

func (s SQLNullPost) GetID() string {
Expand Down
37 changes: 32 additions & 5 deletions jsonapi/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,17 +842,14 @@ var _ = Describe("Marshalling", func() {
Context("SQL Null-Types", func() {
var nullPost SQLNullPost

BeforeEach(func() {
It("correctly marshalls String, Int64, Float64, Bool and Time", func() {
nullPost = SQLNullPost{
ID: "theID",
Title: zero.StringFrom("Test"),
Likes: zero.IntFrom(666),
Rating: zero.FloatFrom(66.66),
IsCool: zero.BoolFrom(true),
}
})

It("correctly marshalls String, Int64, Float64 and Bool", func() {
result, err := MarshalToJSON(nullPost)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(MatchJSON(`
Expand All @@ -864,11 +861,41 @@ var _ = Describe("Marshalling", func() {
"title": "Test",
"likes": 666,
"rating": 66.66,
"isCool": true
"isCool": true,
"today": "0001-01-01T00:00:00Z"
}
}
}
`))
})

It("correctly marshalls Null String, Int64, Float64, Bool and Time", func() {
nullPost = SQLNullPost{
ID: "theID",
Title: zero.StringFromPtr(nil),
Likes: zero.IntFromPtr(nil),
Rating: zero.FloatFromPtr(nil),
IsCool: zero.BoolFromPtr(nil),
Today: zero.TimeFromPtr(nil),
}
result, err := MarshalToJSON(nullPost)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(MatchJSON(`
{
"data": {
"id": "theID",
"type": "sqlNullPosts",
"attributes": {
"title": "",
"likes": 0,
"rating": 0,
"isCool": false,
"today": "0001-01-01T00:00:00Z"
}
}
}
`))
})

})
})
79 changes: 75 additions & 4 deletions jsonapi/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsonapi

import (
"database/sql"
"fmt"
"time"

"gopkg.in/guregu/null.v2/zero"
Expand Down Expand Up @@ -593,13 +594,15 @@ var _ = Describe("Unmarshal", func() {

Context("SQL Null-Types", func() {
var nullPosts []SQLNullPost
var timeZero time.Time

BeforeEach(func() {
nullPosts = []SQLNullPost{}
timeZero = time.Time{}
})

It("correctly unmarshal String, Int64 and Float64", func() {
err := UnmarshalFromJSON([]byte(`
It("correctly unmarshals String, Int64, Float64 and Time", func() {
err := UnmarshalFromJSON([]byte(fmt.Sprintf(`
{
"data": {
"id": "theID",
Expand All @@ -608,11 +611,12 @@ var _ = Describe("Unmarshal", func() {
"title": "Test",
"likes": 666,
"rating": 66.66,
"isCool": true
"isCool": true,
"today": "%v"
}
}
}
`), &nullPosts)
`, timeZero.Format(time.RFC3339))), &nullPosts)
Expect(err).ToNot(HaveOccurred())
Expect(nullPosts).To(HaveLen(1))
Expect(nullPosts[0]).To(Equal(SQLNullPost{
Expand All @@ -621,7 +625,74 @@ var _ = Describe("Unmarshal", func() {
Likes: zero.IntFrom(666),
Rating: zero.FloatFrom(66.66),
IsCool: zero.BoolFrom(true),
Today: zero.TimeFrom(timeZero.UTC()),
}))
})

It("correctly unmarshals Null String, Int64, Float64 and Time", func() {
err := UnmarshalFromJSON([]byte(`
{
"data": {
"id": "theID",
"type": "sqlNullPosts",
"attributes": {
"title": null,
"likes": null,
"rating": null,
"isCool": null,
"today": null
}
}
}
`), &nullPosts)
Expect(err).ToNot(HaveOccurred())
Expect(nullPosts).To(HaveLen(1))
Expect(nullPosts[0]).To(Equal(SQLNullPost{
ID: "theID",
Title: zero.StringFromPtr(nil),
Likes: zero.IntFromPtr(nil),
Rating: zero.FloatFromPtr(nil),
IsCool: zero.BoolFromPtr(nil),
Today: zero.TimeFromPtr(nil),
}))
})

It("overwrites existing data with nulls when marshaling", func() {
now := zero.TimeFrom(time.Now().UTC())
target := SQLNullPost{
ID: "newID",
Title: zero.StringFrom("TestTitle"),
Likes: zero.IntFrom(11), Today: now,
IsCool: zero.BoolFrom(true),
Rating: zero.FloatFrom(0.0)}
nullPosts = append(nullPosts, target)
Expect(nullPosts).To(HaveLen(1))
err := UnmarshalFromJSON([]byte(`
{
"data": {
"id": "newID",
"type": "sqlNullPosts",
"attributes": {
"title": null,
"likes": null,
"rating": null,
"isCool": null,
"today": null
}
}
}
`), &nullPosts[0])
Expect(err).ToNot(HaveOccurred())
Expect(nullPosts).To(HaveLen(1))
Expect(nullPosts[0]).To(Equal(SQLNullPost{
ID: "newID",
Title: zero.StringFromPtr(nil),
Likes: zero.IntFromPtr(nil),
Rating: zero.FloatFromPtr(nil),
IsCool: zero.BoolFromPtr(nil),
Today: zero.TimeFromPtr(nil),
}))
})

})
})

0 comments on commit 4b5c8ff

Please sign in to comment.