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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
package gojsonq
import (
"bytes"
"encoding/json"
"testing"
)
func Test_abs(t *testing.T) {
testCases := []struct {
data int
expected int
}{
{
data: 15,
expected: 15,
},
{
data: -25,
expected: 25,
},
{
data: 0,
expected: 0,
},
}
for _, tc := range testCases {
if o := abs(tc.data); o != tc.expected {
t.Errorf("expected: %v got: %v", tc.expected, o)
}
}
}
func Test_isIndex(t *testing.T) {
testCases := []struct {
node string
expected bool
}{
{
node: "items",
expected: false,
},
{
node: "[0]",
expected: true,
},
{
node: "[101]",
expected: true,
},
{
node: "101",
expected: false,
},
}
for _, tc := range testCases {
if o := isIndex(tc.node); o != tc.expected {
t.Errorf("expected: %v got: %v", tc.expected, o)
}
}
}
func Test_getIndex(t *testing.T) {
testCases := []struct {
node string
expected int
}{
{
node: "Invalid integer",
expected: -1,
},
{
node: "item",
expected: -1,
},
{
node: "[0]",
expected: 0,
},
{
node: "101",
expected: -1,
},
{
node: "[101]",
expected: 101,
},
}
for _, tc := range testCases {
if o, _ := getIndex(tc.node); o != tc.expected {
t.Errorf("expected: %v got: %v", tc.expected, o)
}
}
}
func Test_toString(t *testing.T) {
testCases := []struct {
val interface{}
expected string
}{
{
val: 10,
expected: "10",
},
{
val: -10,
expected: "-10",
},
{
val: 10.99,
expected: "10.99",
},
{
val: -10.99,
expected: "-10.99",
},
{
val: true,
expected: "true",
},
}
for _, tc := range testCases {
if o := toString(tc.val); o != tc.expected {
t.Errorf("expected: %v got: %v", tc.expected, o)
}
}
}
func Test_toFloat64(t *testing.T) {
testCases := []struct {
val interface{}
expected float64
}{
{
val: 10,
expected: 10,
},
{
val: int8(1),
expected: 1,
},
{
val: int16(91),
expected: 91,
},
{
val: int32(88),
expected: 88,
},
{
val: int64(898),
expected: 898,
},
{
val: float32(99.01),
// The nearest IEEE754 float32 value of 99.01 is 99.01000213623047; which are not equal (while using ==).
// Need suggestions for precision float value.
// one way to solve the comparison using convertFloat(string with float precision)==float64
expected: 99.01000213623047,
},
{
val: float32(-99),
expected: -99,
},
{
val: -99.91,
expected: -99.91,
},
{
val: "",
expected: 0,
},
{
val: []int{},
expected: 0,
},
}
for _, tc := range testCases {
if o, _ := toFloat64(tc.val); o != tc.expected {
t.Errorf("expected: %v got: %v", tc.expected, o)
}
}
}
func Test_sorter(t *testing.T) {
testCases := []struct {
tag string
asc bool
inArr []interface{}
outArr []interface{}
}{
{
tag: "list of string, result should be in ascending order",
asc: true,
inArr: []interface{}{"x", "b", "a", "c", "z"},
outArr: []interface{}{"a", "b", "c", "x", "z"},
},
{
tag: "list of string, result should be in descending order",
asc: false,
inArr: []interface{}{"x", "b", "a", "c", "z"},
outArr: []interface{}{"z", "x", "c", "b", "a"},
},
{
tag: "list of float64, result should be in ascending order",
asc: true,
inArr: []interface{}{8.0, 7.0, 1.0, 3.0, 5.0, 8.0},
outArr: []interface{}{1.0, 3.0, 5.0, 7.0, 8.0, 8.0},
},
{
tag: "list of float64, result should be in descending order",
asc: false,
inArr: []interface{}{8.0, 7.0, 1.0, 3.0, 5.0, 8.0},
outArr: []interface{}{8.0, 8.0, 7.0, 5.0, 3.0, 1.0},
},
}
for _, tc := range testCases {
obb, _ := json.Marshal(sortList(tc.inArr, tc.asc))
ebb, _ := json.Marshal(tc.outArr)
if !bytes.Equal(obb, ebb) {
t.Errorf("expected: %v got: %v", string(obb), string(ebb))
}
}
}
func Test_sortMap(t *testing.T) {
testCases := []struct {
tag string
inObjs interface{}
outObjs interface{}
key string
asc bool
}{
{
tag: "should return in ascending order of string value name",
key: "name",
asc: true,
inObjs: []map[string]interface{}{
{"name": "Z", "height": 5.8},
{"name": "A", "height": 5.5},
{"name": "D", "height": 4.9},
{"name": "X", "height": 5.9},
},
outObjs: []map[string]interface{}{
{"name": "A", "height": 5.5},
{"name": "D", "height": 4.9},
{"name": "X", "height": 5.9},
{"name": "Z", "height": 5.8},
},
},
{
tag: "should return in descending order of string value name",
key: "name",
asc: false,
inObjs: []map[string]interface{}{
{"name": "Z", "height": 5.8},
{"name": "A", "height": 5.5},
{"name": "D", "height": 4.9},
{"name": "X", "height": 5.9},
},
outObjs: []map[string]interface{}{
{"name": "Z", "height": 5.8},
{"name": "X", "height": 5.9},
{"name": "D", "height": 4.9},
{"name": "A", "height": 5.5},
},
},
{
tag: "should return in ascending order of float value height",
key: "height",
asc: true,
inObjs: []map[string]interface{}{
{"name": "Z", "height": 5.8},
{"name": "A", "height": 5.5},
{"name": "D", "height": 4.9},
{"name": "X", "height": 5.9},
},
outObjs: []map[string]interface{}{
{"name": "D", "height": 4.9},
{"name": "A", "height": 5.5},
{"name": "Z", "height": 5.8},
{"name": "X", "height": 5.9},
},
},
{
tag: "should return in descending order of float value height",
key: "height",
asc: false,
inObjs: []map[string]interface{}{
{"name": "Z", "height": 5.8},
{"name": "A", "height": 5.5},
{"name": "D", "height": 4.9},
{"name": "X", "height": 5.9},
},
outObjs: []map[string]interface{}{
{"name": "X", "height": 5.9},
{"name": "Z", "height": 5.8},
{"name": "A", "height": 5.5},
{"name": "D", "height": 4.9},
},
},
{
key: "height",
asc: false,
inObjs: []string{"a", "z", "x"},
outObjs: []string{"a", "z", "x"},
},
{
key: "invalid_key",
asc: false,
inObjs: []string{"x", "z", "a"},
outObjs: []string{"x", "z", "a"},
},
}
for _, tc := range testCases {
inObjs := tc.inObjs
sm := &sortMap{}
sm.key = tc.key
sm.desc = !tc.asc
sm.Sort(inObjs)
assertInterface(t, inObjs, tc.outObjs, tc.tag)
}
}
func Test_getNestedValue(t *testing.T) {
var content interface{}
if err := json.Unmarshal([]byte(jsonStr), &content); err != nil {
t.Error("failed to decode json:", err)
}
testCases := []struct {
tag string
query string
expected interface{}
expectError bool
}{
{
tag: "accessing node",
query: "vendor.name",
expected: `Star Trek`,
expectError: false,
},
{
tag: "should return nil",
query: "vendor.xox",
expected: nil,
expectError: true,
},
{
tag: "should return a map",
query: "vendor.items.[0]",
expected: map[string]interface{}{"id": 1, "name": "MacBook Pro 13 inch retina", "price": 1350},
expectError: false,
},
{
tag: "accessing not existed index",
query: "vendor.items.[10]",
expected: nil,
expectError: true,
},
{
tag: "accessing invalid index error",
query: "vendor.items.[x]",
expected: nil,
expectError: true,
},
{
tag: "should receive valid float value",
query: "vendor.items.[0].price",
expected: 1350,
expectError: false,
},
}
for _, tc := range testCases {
out, err := getNestedValue(content, tc.query, defaultSeparator)
if tc.expectError && err == nil {
t.Error("failed to catch error")
}
if !tc.expectError {
assertInterface(t, tc.expected, out, tc.tag)
}
}
}
func Test_makeAlias(t *testing.T) {
testCases := []struct {
tag string
input string
node string
alias string
separator string
}{
{
tag: "scenario 1",
input: "user.name as uname",
node: "user.name",
alias: "uname",
separator: ".",
},
{
tag: "scenario 2",
input: "post.title",
node: "post.title",
alias: "title",
separator: ".",
},
{
tag: "scenario 3",
input: "name",
node: "name",
alias: "name",
separator: ".",
},
{
tag: "scenario 4",
input: "post->title",
node: "post->title",
alias: "title",
separator: "->",
},
}
for _, tc := range testCases {
n, a := makeAlias(tc.input, tc.separator)
if tc.node != n || tc.alias != a {
t.Errorf("Tag: %v\nExpected: %v %v \nGot: %v %v\n", tc.tag, tc.node, tc.alias, n, a)
}
}
}
func Test_length(t *testing.T) {
testCases := []struct {
tag string
input interface{}
output int
errExpected bool
}{
{
tag: "scenario 1: should return 5 with no error",
input: "Hello",
output: 5,
errExpected: false,
},
{
tag: "scenario 2: must return error with -1",
input: 45,
output: -1,
errExpected: true,
},
{
tag: "scenario 3: must return length of array",
input: []interface{}{"john", "31", false},
output: 3,
errExpected: false,
},
{
tag: "scenario 4: must return length of map",
input: map[string]interface{}{"name": "john", "age": 31, "is_designer": false},
output: 3,
errExpected: false,
},
}
for _, tc := range testCases {
out, outErr := length(tc.input)
if out != tc.output {
if tc.errExpected && outErr == nil {
t.Errorf("tag: %s\nExpected: %v\nGot: %v", tc.tag, tc.output, out)
}
}
}
}
|