Exemplo n.º 1
0
// func FixBindParams ( qry string, data ...interface{} ) ( qryFixed string, retData []interface{}, err error ) {
func SVar(v interface{}) string {
	s, err := json.Marshal(v)
	if err != nil {
		return fmt.Sprintf("Error:%s", err)
	} else {
		return string(s)
	}
}
Exemplo n.º 2
0
func TestEncodeAttachments(t *testing.T) {
	var input interface{}
	var target []io.Reader
	buf1 := bytes.NewBufferString("data1")
	buf2 := bytes.NewBufferString("data2")
	attachment1 := &Attachment{Data: buf1}
	attachment2 := &Attachment{Data: buf2}

	test := func() {
		attachment1.num = -1
		attachment2.num = -1
		attachments := encodeAttachments(input)
		if len(attachments)+len(target) > 0 {
			So(attachments, ShouldResemble, target)
		}
	}

	Convey("No attachment", t, func() {
		input = &NoAttachment{}
		target = nil

		test()
	})

	Convey("Many attachment", t, func() {
		input = &HaveAttachment{A: attachment1}
		target = []io.Reader{buf1}

		test()

		So(attachment1.num, ShouldEqual, 0)
	})

	Convey("Array of attachments", t, func() {
		input = [...]interface{}{HaveAttachment{A: attachment1}, &HaveAttachment{A: attachment2}}
		target = []io.Reader{buf1, buf2}

		test()

		So(attachment1.num, ShouldEqual, 0)
		So(attachment2.num, ShouldEqual, 1)
	})

	Convey("Slice of attachments", t, func() {
		input = []interface{}{HaveAttachment{A: attachment1}, &HaveAttachment{A: attachment2}}
		target = []io.Reader{buf1, buf2}

		test()

		So(attachment1.num, ShouldEqual, 0)
		So(attachment2.num, ShouldEqual, 1)
	})

	Convey("Map of attachments", t, func() {
		input = map[string]interface{}{"test": HaveAttachment{A: attachment1}, "testp": &HaveAttachment{A: attachment2}}

		attachment1.num = -1
		attachment2.num = -1
		attachments := encodeAttachments(input)

		So(attachment1.num, ShouldBeIn, []int{0, 1})
		switch attachment1.num {
		case 0:
			So(attachment2.num, ShouldEqual, 1)
			target = []io.Reader{buf1, buf2}
			So(attachments, ShouldResemble, target)
		case 1:
			So(attachment2.num, ShouldEqual, 0)
			target = []io.Reader{buf2, buf1}
			So(attachments, ShouldResemble, target)
		}
	})

	Convey("Encode attachment", t, func() {
		input = map[string]interface{}{"test": HaveAttachment{A: attachment1}}

		attachment1.num = -1
		encodeAttachments(input)

		b, err := json.Marshal(input)
		So(err, ShouldBeNil)
		So(string(b), ShouldEqual, `{"test":{"i":0,"a":{"_placeholder":true,"num":0}}}`)
	})

}