forked from domodwyer/mgo
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to demonstrate tagging fallback.
Typo fix. Fix issues in test. Update test code.
- Loading branch information
1 parent
31878d4
commit e7fec90
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package bson_test | ||
|
||
import ( | ||
"github.com/globalsign/mgo/bson" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
type mixedTagging struct { | ||
First string | ||
Second string `bson:"second_field"` | ||
Third string `json:"third_field"` | ||
Fourth string `bson:"fourth_field" json:"alternate"` | ||
} | ||
|
||
// TestTaggingFallback checks that tagging fallback can be used/works as expected. | ||
func (s *S) TestTaggingFallback(c *C) { | ||
initial := &mixedTagging{ | ||
First: "One", | ||
Second: "Two", | ||
Third: "Three", | ||
Fourth: "Four", | ||
} | ||
|
||
// Take only testing.T, leave only footprints. | ||
initialState := bson.JSONTagFallbackState() | ||
defer bson.SetJSONTagFallback(initialState) | ||
|
||
// Marshal with the new mode applied. | ||
bson.SetJSONTagFallback(true) | ||
bson, errBSON := bson.Marshal(initial) | ||
c.Assert(errBSON, IsNil) | ||
|
||
// Unmarshal into a generic map so that we can pick up the actual field names | ||
// selected. | ||
target := make(map[string]string) | ||
errUnmarshal := bson.Unmarshal(bson, target) | ||
c.Assert(errUnmarshal, IsNil) | ||
|
||
// No tag, so standard naming | ||
_, firstExists := target["first"] | ||
c.Assert(firstExists, Exists, true) | ||
|
||
// Just a BSON tag | ||
_, secondExists := target["second_field"] | ||
c.Assert(secondExists, Exists, true) | ||
|
||
// Just a JSON tag | ||
_, thirdExists := target["third_field"] | ||
c.Assert(thirdExists, Exists, true) | ||
|
||
// Should marshal 4th as fourth_field (since we have both tags) | ||
_, fourthExists := target["fourth_field"] | ||
c.Assert(fourthExists, Exists, true) | ||
} |