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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package bsonrw
import (
"fmt"
"io/ioutil"
"reflect"
"strings"
"testing"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func TestExtJSONValueWriter(t *testing.T) {
oid := primitive.ObjectID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C}
testCases := []struct {
name string
fn interface{}
params []interface{}
}{
{
"WriteBinary",
(*extJSONValueWriter).WriteBinary,
[]interface{}{[]byte{0x01, 0x02, 0x03}},
},
{
"WriteBinaryWithSubtype (not 0x02)",
(*extJSONValueWriter).WriteBinaryWithSubtype,
[]interface{}{[]byte{0x01, 0x02, 0x03}, byte(0xFF)},
},
{
"WriteBinaryWithSubtype (0x02)",
(*extJSONValueWriter).WriteBinaryWithSubtype,
[]interface{}{[]byte{0x01, 0x02, 0x03}, byte(0x02)},
},
{
"WriteBoolean",
(*extJSONValueWriter).WriteBoolean,
[]interface{}{true},
},
{
"WriteDBPointer",
(*extJSONValueWriter).WriteDBPointer,
[]interface{}{"bar", oid},
},
{
"WriteDateTime",
(*extJSONValueWriter).WriteDateTime,
[]interface{}{int64(12345678)},
},
{
"WriteDecimal128",
(*extJSONValueWriter).WriteDecimal128,
[]interface{}{primitive.NewDecimal128(10, 20)},
},
{
"WriteDouble",
(*extJSONValueWriter).WriteDouble,
[]interface{}{float64(3.14159)},
},
{
"WriteInt32",
(*extJSONValueWriter).WriteInt32,
[]interface{}{int32(123456)},
},
{
"WriteInt64",
(*extJSONValueWriter).WriteInt64,
[]interface{}{int64(1234567890)},
},
{
"WriteJavascript",
(*extJSONValueWriter).WriteJavascript,
[]interface{}{"var foo = 'bar';"},
},
{
"WriteMaxKey",
(*extJSONValueWriter).WriteMaxKey,
[]interface{}{},
},
{
"WriteMinKey",
(*extJSONValueWriter).WriteMinKey,
[]interface{}{},
},
{
"WriteNull",
(*extJSONValueWriter).WriteNull,
[]interface{}{},
},
{
"WriteObjectID",
(*extJSONValueWriter).WriteObjectID,
[]interface{}{oid},
},
{
"WriteRegex",
(*extJSONValueWriter).WriteRegex,
[]interface{}{"bar", "baz"},
},
{
"WriteString",
(*extJSONValueWriter).WriteString,
[]interface{}{"hello, world!"},
},
{
"WriteSymbol",
(*extJSONValueWriter).WriteSymbol,
[]interface{}{"symbollolz"},
},
{
"WriteTimestamp",
(*extJSONValueWriter).WriteTimestamp,
[]interface{}{uint32(10), uint32(20)},
},
{
"WriteUndefined",
(*extJSONValueWriter).WriteUndefined,
[]interface{}{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fn := reflect.ValueOf(tc.fn)
if fn.Kind() != reflect.Func {
t.Fatalf("fn must be of kind Func but it is a %v", fn.Kind())
}
if fn.Type().NumIn() != len(tc.params)+1 || fn.Type().In(0) != reflect.TypeOf((*extJSONValueWriter)(nil)) {
t.Fatalf("fn must have at least one parameter and the first parameter must be a *valueWriter")
}
if fn.Type().NumOut() != 1 || fn.Type().Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
t.Fatalf("fn must have one return value and it must be an error.")
}
params := make([]reflect.Value, 1, len(tc.params)+1)
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
params[0] = reflect.ValueOf(ejvw)
for _, param := range tc.params {
params = append(params, reflect.ValueOf(param))
}
t.Run("incorrect transition", func(t *testing.T) {
results := fn.Call(params)
got := results[0].Interface().(error)
fnName := tc.name
if strings.Contains(fnName, "WriteBinary") {
fnName = "WriteBinaryWithSubtype"
}
want := TransitionError{current: mTopLevel, name: fnName, modes: []mode{mElement, mValue},
action: "write"}
if !compareErrors(got, want) {
t.Errorf("Errors do not match. got %v; want %v", got, want)
}
})
})
}
t.Run("WriteArray", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mArray, parent: mTopLevel,
name: "WriteArray", modes: []mode{mElement, mValue}, action: "write"}
_, got := ejvw.WriteArray()
if !compareErrors(got, want) {
t.Errorf("Did not get expected error. got %v; want %v", got, want)
}
})
t.Run("WriteCodeWithScope", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mCodeWithScope, parent: mTopLevel,
name: "WriteCodeWithScope", modes: []mode{mElement, mValue}, action: "write"}
_, got := ejvw.WriteCodeWithScope("")
if !compareErrors(got, want) {
t.Errorf("Did not get expected error. got %v; want %v", got, want)
}
})
t.Run("WriteDocument", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mDocument, parent: mTopLevel,
name: "WriteDocument", modes: []mode{mElement, mValue, mTopLevel}, action: "write"}
_, got := ejvw.WriteDocument()
if !compareErrors(got, want) {
t.Errorf("Did not get expected error. got %v; want %v", got, want)
}
})
t.Run("WriteDocumentElement", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
ejvw.push(mElement)
want := TransitionError{current: mElement,
destination: mElement,
parent: mTopLevel,
name: "WriteDocumentElement",
modes: []mode{mDocument, mTopLevel, mCodeWithScope},
action: "write"}
_, got := ejvw.WriteDocumentElement("")
if !compareErrors(got, want) {
t.Errorf("Did not get expected error. got %v; want %v", got, want)
}
})
t.Run("WriteDocumentEnd", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
ejvw.push(mElement)
want := fmt.Errorf("incorrect mode to end document: %s", mElement)
got := ejvw.WriteDocumentEnd()
if !compareErrors(got, want) {
t.Errorf("Did not get expected error. got %v; want %v", got, want)
}
})
t.Run("WriteArrayElement", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
ejvw.push(mElement)
want := TransitionError{current: mElement,
destination: mValue,
parent: mTopLevel,
name: "WriteArrayElement",
modes: []mode{mArray},
action: "write"}
_, got := ejvw.WriteArrayElement()
if !compareErrors(got, want) {
t.Errorf("Did not get expected error. got %v; want %v", got, want)
}
})
t.Run("WriteArrayEnd", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true)
ejvw.push(mElement)
want := fmt.Errorf("incorrect mode to end array: %s", mElement)
got := ejvw.WriteArrayEnd()
if !compareErrors(got, want) {
t.Errorf("Did not get expected error. got %v; want %v", got, want)
}
})
t.Run("WriteBytes", func(t *testing.T) {
t.Run("writeElementHeader error", func(t *testing.T) {
ejvw := newExtJSONWriterFromSlice(nil, true, true)
want := TransitionError{current: mTopLevel, destination: mode(0),
name: "WriteBinaryWithSubtype", modes: []mode{mElement, mValue}, action: "write"}
got := ejvw.WriteBinaryWithSubtype(nil, (byte)(bsontype.EmbeddedDocument))
if !compareErrors(got, want) {
t.Errorf("Did not received expected error. got %v; want %v", got, want)
}
})
})
t.Run("FormatDoubleWithExponent", func(t *testing.T) {
want := "3E-12"
got := formatDouble(float64(0.000000000003))
if got != want {
t.Errorf("Did not receive expected string. got %s: want %s", got, want)
}
})
}
|