Source

gomongo / bson_test.go

Full commit
// Copyright 2009 Michael Stephens.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package mongo_test

import (
	"testing";
	"mongo";
	"fmt";
	"time";
	"bytes"
)

func assertTrue(tf bool, msg string, t *testing.T) {
	if !tf {
		t.Error(msg)
	}
}

type EmptyStruct struct{}

type OtherStruct struct {
	F, V string;
}

type ExampleStruct struct {
	First	int32;
	Second	float64;
	Third	string;
	Fourth	EmptyStruct;
	Fifth	OtherStruct;
}

func TestMarshalArrayInMap(t *testing.T) {
	as := map[string][]byte{"1": []byte{1, 2, 3, 4, 5, 6}, "2": []byte{7, 8, 9, 10, 11, 12}}
	b1, _ := mongo.Marshal(as)
	
	var as2 = map[string][]byte{}
	mongo.Unmarshal(b1.Bytes(), &as2)
	
	b2, _ := mongo.Marshal(as2)
	fmt.Print(as, as2)
	
	assertTrue(mongo.Equal(b1, b2), "map array field unmarshal->marshal", t);
}

//var b []byte = []byte{92, 0, 0, 0, 1, 115, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 102, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 118, 0, 2, 0, 0, 0, 101, 0, 2, 102, 0, 2, 0, 0, 0, 105, 0, 0, 3, 102, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 2, 116, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 16, 102, 105, 114, 115, 116, 0, 1, 0, 0, 0, 0}

var b []byte = []byte{92, 0, 0, 0, 16, 70, 105, 114, 115, 116, 0, 1, 0, 0, 0, 3, 70, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 70, 0, 2, 0, 0, 0, 105, 0, 2, 86, 0, 2, 0, 0, 0, 101, 0, 0, 1, 83, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 6, 4, 2, 84, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 3, 70, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 0}

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);

	fmt.Println(obj.Get("First").Int())
	fmt.Println(obj.Get("Second").Number())
	fmt.Println(obj.Get("Third").String())
	fmt.Println(obj.Get("Fifth").Get("F").String())
	fmt.Println(obj.Get("Fifth").Get("V").String())
	fmt.Print(obj)
	
	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);
}

func TestUnmarshal(t *testing.T) {
	var es ExampleStruct;
	mongo.Unmarshal(b, &es)
	fmt.Println(es)
	assertTrue(es.First == 1, "unmarshal int", t);
	assertTrue(es.Second == 2, "unmarshal float64", t);
	assertTrue(es.Third == "three", "unmarshal string", t);
	assertTrue(es.Fifth.F == "i" && es.Fifth.V == "e", "unmarshal struct", t);
}

type ExampleStruct2 struct {
	Date *time.Time;
}

func TestMarshal(t *testing.T) {
	var es1 ExampleStruct;
	mongo.Unmarshal(b, &es1);
	bs1, _ := mongo.Marshal(&es1);
	bs2, _ := mongo.BytesToBSON(b);
	fmt.Println(bs1.Bytes())
	fmt.Println(bs2.Bytes())
	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, err := mongo.Marshal(es2);
	if err != nil {fmt.Println(err)}
	t2 := bs2.Get("Date").Date()
	assertTrue(t2 != nil && t2.Seconds() == d.Seconds(), "date marshal", t);
	es2 = new(ExampleStruct2);
	mongo.Unmarshal(bs2.Bytes(), es2);
	assertTrue(es2.Date.Seconds() == d.Seconds(), "date unmarshal", t);
}

type ArrayStruct struct {
	Str string
	Ary []int
}


func TestMarshalArray(t *testing.T) {
	as := ArrayStruct{"a string", []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}}
	b1, _ := mongo.Marshal(as)
	
	var as2 ArrayStruct
	mongo.Unmarshal(b1.Bytes(), &as2)
	
	b2, _ := mongo.Marshal(as2)
	
	assertTrue(mongo.Equal(b1, b2), "array field unmarshal->marshal", t);
}

func TestOID(t *testing.T) {
	b := []byte{1,2,3,4,5,6,7,8,9,10,11,12}
	oid := mongo.OID(b)
	str := oid.String()
	oid2, err := mongo.NewOID(str)
	
	assertTrue(err == nil, fmt.Sprintf("conversion error %s", err), t)
	assertTrue(bytes.Equal(b, []byte(oid2)), fmt.Sprintf("oid->string-oid didn't work. %v:%s", b, []byte(oid2)), t)
}