コード例 #1
0
ファイル: mongo_test.go プロジェクト: patrickxb/gomongo
func TestOtherStuff(t *testing.T) {
	doc, _ := mongo.Marshal(map[string]string{"_id": "doc1", "title": "A Mongo document", "content": "Testing, 1. 2. 3."})
	conn, _ := mongo.Connect("127.0.0.1", 27017)
	collection := conn.GetDB("test").GetCollection("test_collection")
	collection.Insert(doc)

	query, _ := mongo.Marshal(map[string]string{"_id": "doc1"})
	got, _ := collection.FindOne(query)
	assertTrue(mongo.Equal(doc, got), "equal", t)

}
コード例 #2
0
ファイル: bson_test.go プロジェクト: patrickxb/gomongo
func TestSerializeAndDeserialize(t *testing.T) {
	obj, err := mongo.BytesToBSON(b)
	assertTrue(err == nil, fmt.Sprintf("failed parsing %v", b), t)
	obj2, _ := mongo.BytesToBSON(obj.Bytes())
	assertTrue(mongo.Equal(obj, obj2), fmt.Sprintf("obj != obj2 for %v", b), t)

	assertTrue(obj.Get("first").Int() == 1, "obj['first'] != 1", t)
	assertTrue(obj.Get("second").Number() == 2, "obj['second'] != 2.0", t)
	assertTrue(obj.Get("third").String() == "three", "obj['third'] != 'three'", t)
	assertTrue(obj.Get("fifth").Get("f").String() == "i", "obj['fifth']['f'] != 'i'", t)
	assertTrue(obj.Get("fifth").Get("v").String() == "e", "obj['fifth']['v'] != 'e'", t)
}
コード例 #3
0
ファイル: bson_test.go プロジェクト: patrickxb/gomongo
func TestMarshal(t *testing.T) {
	var es1 ExampleStruct
	mongo.Unmarshal(b, &es1)
	bs1, _ := mongo.Marshal(&es1)
	bs2, _ := mongo.BytesToBSON(b)
	assertTrue(mongo.Equal(bs1, bs2), "unmarshal->marshal", t)

	m := map[string]string{"f": "i", "v": "e"}
	bs3, _ := mongo.Marshal(&m)
	assertTrue(mongo.Equal(bs3, bs2.Get("fifth")), "marshal map", t)

	arr, _ := mongo.Marshal([]int{1, 2, 3})
	assertTrue(arr.Elem(0).Long() == 1, "array marshal (0)", t)
	assertTrue(arr.Elem(1).Long() == 2, "array marshal (1)", t)
	assertTrue(arr.Elem(2).Long() == 3, "array marshal (2)", t)

	d := time.UTC()
	es2 := &ExampleStruct2{d}
	bs2, _ = mongo.Marshal(es2)
	assertTrue(bs2.Get("date").Date().Seconds() == d.Seconds(), "date marshal", t)
	es2 = new(ExampleStruct2)
	mongo.Unmarshal(bs2.Bytes(), es2)
	assertTrue(es2.Date.Seconds() == d.Seconds(), "date unmarshal", t)
}