1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
package json
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/mgo.v2/bson"
"testing"
)
func TestDBPointerValue(t *testing.T) {
Convey("Unmarshalling JSON with DBPointer values", t, func() {
key := "key"
value := `DBPointer("ref", ObjectId("552ffe9f5739878e73d116a9"))`
Convey("works for a single key", func() {
var jsonMap map[string]interface{}
data := fmt.Sprintf(`{"%v":%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue, ok := jsonMap[key].(DBPointer)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldResemble, DBPointer{"ref", bson.ObjectIdHex("552ffe9f5739878e73d116a9")})
})
Convey("works for multiple keys", func() {
var jsonMap map[string]interface{}
key1, key2, key3 := "key1", "key2", "key3"
value2 := `DBPointer("ref2", ObjectId("552ffed95739878e73d116aa"))`
value3 := `DBPointer("ref3", ObjectId("552fff215739878e73d116ab"))`
data := fmt.Sprintf(`{"%v":%v,"%v":%v,"%v":%v}`,
key1, value, key2, value2, key3, value3)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue1, ok := jsonMap[key1].(DBPointer)
So(ok, ShouldBeTrue)
So(jsonValue1, ShouldResemble, DBPointer{"ref", bson.ObjectIdHex("552ffe9f5739878e73d116a9")})
jsonValue2, ok := jsonMap[key2].(DBPointer)
So(ok, ShouldBeTrue)
So(jsonValue2, ShouldResemble, DBPointer{"ref2", bson.ObjectIdHex("552ffed95739878e73d116aa")})
jsonValue3, ok := jsonMap[key3].(DBPointer)
So(ok, ShouldBeTrue)
So(jsonValue3, ShouldResemble, DBPointer{"ref3", bson.ObjectIdHex("552fff215739878e73d116ab")})
})
Convey("works in an array", func() {
var jsonMap map[string]interface{}
data := fmt.Sprintf(`{"%v":[%v,%v,%v]}`,
key, value, value, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonArray, ok := jsonMap[key].([]interface{})
So(ok, ShouldBeTrue)
for _, _jsonValue := range jsonArray {
jsonValue, ok := _jsonValue.(DBPointer)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldResemble, DBPointer{"ref", bson.ObjectIdHex("552ffe9f5739878e73d116a9")})
}
})
Convey("will not accept an $id type that is not an ObjectId", func() {
value := `DBPointer("ref", 4)`
var jsonMap map[string]interface{}
data := fmt.Sprintf(`{"%v":%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldNotBeNil)
})
})
}
|