Skip to content

Commit fc825e4

Browse files
committed
Add tests for marshaling and unmarshaling null/zero fields
Add a test verifiying that existing data gets overwritten by nulls. Add marshal test for nil values into JSON. Add test for unmarshaling null JSON values. Update marshal test for time entry. Add a time type to the zero lib verification. Add a test verifiying that existing data gets overwritten by nulls.
1 parent b6eb5c0 commit fc825e4

File tree

3 files changed

+108
-9
lines changed

3 files changed

+108
-9
lines changed

jsonapi/fixtures_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ type SQLNullPost struct {
345345
Likes zero.Int
346346
Rating zero.Float
347347
IsCool zero.Bool
348+
Today zero.Time
348349
}
349350

350351
func (s SQLNullPost) GetID() string {

jsonapi/marshal_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,17 +842,14 @@ var _ = Describe("Marshalling", func() {
842842
Context("SQL Null-Types", func() {
843843
var nullPost SQLNullPost
844844

845-
BeforeEach(func() {
845+
It("correctly marshalls String, Int64, Float64, Bool and Time", func() {
846846
nullPost = SQLNullPost{
847847
ID: "theID",
848848
Title: zero.StringFrom("Test"),
849849
Likes: zero.IntFrom(666),
850850
Rating: zero.FloatFrom(66.66),
851851
IsCool: zero.BoolFrom(true),
852852
}
853-
})
854-
855-
It("correctly marshalls String, Int64, Float64 and Bool", func() {
856853
result, err := MarshalToJSON(nullPost)
857854
Expect(err).ToNot(HaveOccurred())
858855
Expect(result).To(MatchJSON(`
@@ -864,11 +861,41 @@ var _ = Describe("Marshalling", func() {
864861
"title": "Test",
865862
"likes": 666,
866863
"rating": 66.66,
867-
"isCool": true
864+
"isCool": true,
865+
"today": "0001-01-01T00:00:00Z"
868866
}
869867
}
870868
}
871869
`))
872870
})
871+
872+
It("correctly marshalls Null String, Int64, Float64, Bool and Time", func() {
873+
nullPost = SQLNullPost{
874+
ID: "theID",
875+
Title: zero.StringFromPtr(nil),
876+
Likes: zero.IntFromPtr(nil),
877+
Rating: zero.FloatFromPtr(nil),
878+
IsCool: zero.BoolFromPtr(nil),
879+
Today: zero.TimeFromPtr(nil),
880+
}
881+
result, err := MarshalToJSON(nullPost)
882+
Expect(err).ToNot(HaveOccurred())
883+
Expect(result).To(MatchJSON(`
884+
{
885+
"data": {
886+
"id": "theID",
887+
"type": "sqlNullPosts",
888+
"attributes": {
889+
"title": "",
890+
"likes": 0,
891+
"rating": 0,
892+
"isCool": false,
893+
"today": "0001-01-01T00:00:00Z"
894+
}
895+
}
896+
}
897+
`))
898+
})
899+
873900
})
874901
})

jsonapi/unmarshal_test.go

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jsonapi
22

33
import (
44
"database/sql"
5+
"fmt"
56
"time"
67

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

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

597599
BeforeEach(func() {
598600
nullPosts = []SQLNullPost{}
601+
timeZero = time.Time{}
599602
})
600603

601-
It("correctly unmarshal String, Int64 and Float64", func() {
602-
err := UnmarshalFromJSON([]byte(`
604+
It("correctly unmarshals String, Int64, Float64 and Time", func() {
605+
err := UnmarshalFromJSON([]byte(fmt.Sprintf(`
603606
{
604607
"data": {
605608
"id": "theID",
@@ -608,11 +611,12 @@ var _ = Describe("Unmarshal", func() {
608611
"title": "Test",
609612
"likes": 666,
610613
"rating": 66.66,
611-
"isCool": true
614+
"isCool": true,
615+
"today": "%v"
612616
}
613617
}
614618
}
615-
`), &nullPosts)
619+
`, timeZero.Format(time.RFC3339))), &nullPosts)
616620
Expect(err).ToNot(HaveOccurred())
617621
Expect(nullPosts).To(HaveLen(1))
618622
Expect(nullPosts[0]).To(Equal(SQLNullPost{
@@ -621,7 +625,74 @@ var _ = Describe("Unmarshal", func() {
621625
Likes: zero.IntFrom(666),
622626
Rating: zero.FloatFrom(66.66),
623627
IsCool: zero.BoolFrom(true),
628+
Today: zero.TimeFrom(timeZero.UTC()),
629+
}))
630+
})
631+
632+
It("correctly unmarshals Null String, Int64, Float64 and Time", func() {
633+
err := UnmarshalFromJSON([]byte(`
634+
{
635+
"data": {
636+
"id": "theID",
637+
"type": "sqlNullPosts",
638+
"attributes": {
639+
"title": null,
640+
"likes": null,
641+
"rating": null,
642+
"isCool": null,
643+
"today": null
644+
}
645+
}
646+
}
647+
`), &nullPosts)
648+
Expect(err).ToNot(HaveOccurred())
649+
Expect(nullPosts).To(HaveLen(1))
650+
Expect(nullPosts[0]).To(Equal(SQLNullPost{
651+
ID: "theID",
652+
Title: zero.StringFromPtr(nil),
653+
Likes: zero.IntFromPtr(nil),
654+
Rating: zero.FloatFromPtr(nil),
655+
IsCool: zero.BoolFromPtr(nil),
656+
Today: zero.TimeFromPtr(nil),
657+
}))
658+
})
659+
660+
It("overwrites existing data with nulls when marshaling", func() {
661+
now := zero.TimeFrom(time.Now().UTC())
662+
target := SQLNullPost{
663+
ID: "newID",
664+
Title: zero.StringFrom("TestTitle"),
665+
Likes: zero.IntFrom(11), Today: now,
666+
IsCool: zero.BoolFrom(true),
667+
Rating: zero.FloatFrom(0.0)}
668+
nullPosts = append(nullPosts, target)
669+
Expect(nullPosts).To(HaveLen(1))
670+
err := UnmarshalFromJSON([]byte(`
671+
{
672+
"data": {
673+
"id": "newID",
674+
"type": "sqlNullPosts",
675+
"attributes": {
676+
"title": null,
677+
"likes": null,
678+
"rating": null,
679+
"isCool": null,
680+
"today": null
681+
}
682+
}
683+
}
684+
`), &nullPosts[0])
685+
Expect(err).ToNot(HaveOccurred())
686+
Expect(nullPosts).To(HaveLen(1))
687+
Expect(nullPosts[0]).To(Equal(SQLNullPost{
688+
ID: "newID",
689+
Title: zero.StringFromPtr(nil),
690+
Likes: zero.IntFromPtr(nil),
691+
Rating: zero.FloatFromPtr(nil),
692+
IsCool: zero.BoolFromPtr(nil),
693+
Today: zero.TimeFromPtr(nil),
624694
}))
625695
})
696+
626697
})
627698
})

0 commit comments

Comments
 (0)