Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Sep 10, 2024
1 parent a97697e commit d88a9b4
Showing 1 changed file with 99 additions and 85 deletions.
184 changes: 99 additions & 85 deletions lib/typing/mongo/bson_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mongo

import (
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -70,6 +71,11 @@ func TestJSONEToMap(t *testing.T) {
"b": {
"c": "hello"
}
},
"test_timestamp": {"$timestamp": { "t": 1678929517, "i": 1 }},
"super_nested": {
"test_timestamp": {"$timestamp": { "t": 1678929517, "i": 1 }},
"foo": "bar"
}
},
"test_timestamp": {
Expand All @@ -95,30 +101,28 @@ func TestJSONEToMap(t *testing.T) {
result, err := JSONEToMap(bsonData)
assert.NoError(t, err)

assert.Equal(t, map[string]any{"$binary": map[string]any{"base64": "c8edabc3f7384ca3b68dab92a91478a3", "subType": "04"}}, result["Binary"])

// String
assert.Equal(t, "Robin Tang", result["full_name"])

// NumberDecimal
assert.Equal(t, "13.37", result["test_decimal"])
assert.Equal(t, 13.37, result["test_decimal_2"])

{
// String
assert.Equal(t, "Robin Tang", result["full_name"])
}
{
// Numbers
// NumberDecimal
assert.Equal(t, "13.37", result["test_decimal"])
assert.Equal(t, 13.37, result["test_decimal_2"])
assert.Equal(t, int64(10004), result["_id"])
assert.Equal(t, int64(107), result["product_id"])
assert.Equal(t, int32(1), result["quantity"])
// 32-bit ints
assert.Equal(t, int32(30), result["number_int"])
assert.Equal(t, int32(1337), result["test_int"])
}
{
// V2 of NaN and Infinity
assert.Nil(t, result["test_nan_v2"])
assert.Nil(t, result["test_infinity_v2"])
assert.Nil(t, result["test_negative_infinity_v2"])
}

assert.Equal(t, int64(10004), result["_id"])
assert.Equal(t, int64(107), result["product_id"])
assert.Equal(t, int32(1), result["quantity"])

// 32-bit ints
assert.Equal(t, int32(30), result["number_int"])
assert.Equal(t, int32(1337), result["test_int"])

{
// Date
extendedTime, isOk := result["order_date"].(*ext.ExtendedTime)
Expand All @@ -131,74 +135,84 @@ func TestJSONEToMap(t *testing.T) {
assert.True(t, isOk)
assert.Equal(t, ext.NewExtendedTime(time.Date(2023, time.March, 16, 1, 18, 37, 0, time.UTC), ext.DateTimeKindType, ext.ISO8601), extendedTime)
}
{
// Boolean
assert.Equal(t, false, result["test_bool_false"])
assert.Equal(t, true, result["test_bool_true"])
}
{
// ObjectID
assert.Equal(t, "63793b4014f7f28f570c524e", result["object_id"])
}
{
// Arrays
assert.Equal(t, []any{float64(1), float64(2), float64(3), float64(4), "hello"}, result["test_list"])
}
{
// Nested object

// Boolean
assert.Equal(t, false, result["test_bool_false"])
assert.Equal(t, true, result["test_bool_true"])

// ObjectID
assert.Equal(t, "63793b4014f7f28f570c524e", result["object_id"])

// Arrays
assert.Equal(t, []any{float64(1), float64(2), float64(3), float64(4), "hello"}, result["test_list"])

// Objects
assert.Equal(t, map[string]any{"a": map[string]any{"b": map[string]any{"c": "hello"}}}, result["test_nested_object"])

// NaN
assert.Equal(t, nil, result["test_nan"])
assert.Equal(t, "NaN", result["test_nan_string"]) // This should not be escaped.
assert.Equal(t, "NaNaNaNa", result["test_nan_string33"])

// Null
assert.Equal(t, nil, result["test_null"])

// Infinity
assert.Equal(t, nil, result["test_infinity"])
assert.Equal(t, "Infinity", result["test_infinity_string"]) // This should not be escaped.
assert.Equal(t, "Infinity123", result["test_infinity_string1"]) // This should not be escaped.

// Negative Infinity
assert.Equal(t, nil, result["test_negative_infinity"])
assert.Equal(t, "-Infinity", result["test_negative_infinity_string"]) // This should not be escaped.
assert.Equal(t, "-Infinity123", result["test_negative_infinity_string1"]) // This should not be escaped.

// Min and Max Keys
assert.Equal(t, map[string]any{"$maxKey": 1}, result["maxValue"])
assert.Equal(t, map[string]any{"$minKey": 1}, result["minValue"])

// All the binary data types.
// 0. Generic Binary
assert.Equal(t,
map[string]any{"$binary": map[string]interface{}{"base64": "123456ABCDEF", "subType": "00"}},
result["profilePic"],
)
// 1. Compiled Function
assert.Equal(t,
map[string]any{"$binary": map[string]interface{}{"base64": "cHJpbnQoJ0hlbGxvIFdvcmxkJyk=", "subType": "01"}},
result["compiledFunction"],
)

// 3 + 4 UUID
assert.Equal(t, "856e56ff-cbb0-411e-855a-98b08b875140", result["unique_id"])

// 5. Checksum
assert.Equal(t,
map[string]any{"$binary": map[string]interface{}{"base64": "1B2M2Y8AsgTpgAmY7PhCfg==", "subType": "05"}},
result["fileChecksum"],
)

// 6. Secure Data
assert.Equal(t,
map[string]any{"$binary": map[string]interface{}{"base64": "YWJjZGVmZ2hpamtsbW5vcA==", "subType": "06"}},
result["secureData"],
)

// Javascript
assert.Equal(t, map[string]any{"$code": "function() {return 0.10;}"}, result["calcDiscount"])

// Regular Expressions
assert.Equal(t, map[string]any{"$options": "", "$regex": `@example\.com$`}, result["emailPattern"])
value, err := json.Marshal(result["test_nested_object"])
assert.NoError(t, err)
assert.Equal(t, `{"a":{"b":{"c":"hello"}},"super_nested":{"foo":"bar","test_timestamp":"2023-03-16T01:18:37+00:00"},"test_timestamp":"2023-03-16T01:18:37+00:00"}`, string(value))
}
{
// NaN
assert.Equal(t, nil, result["test_nan"])
assert.Equal(t, "NaN", result["test_nan_string"]) // This should not be escaped.
assert.Equal(t, "NaNaNaNa", result["test_nan_string33"])
}
{
// Null
assert.Equal(t, nil, result["test_null"])
}
{
// Infinity
assert.Equal(t, nil, result["test_infinity"])
assert.Equal(t, "Infinity", result["test_infinity_string"]) // This should not be escaped.
assert.Equal(t, "Infinity123", result["test_infinity_string1"]) // This should not be escaped.
}
{
// Negative Infinity
assert.Equal(t, nil, result["test_negative_infinity"])
assert.Equal(t, "-Infinity", result["test_negative_infinity_string"]) // This should not be escaped.
assert.Equal(t, "-Infinity123", result["test_negative_infinity_string1"]) // This should not be escaped.
}
{
// Min and Max Keys
assert.Equal(t, map[string]any{"$maxKey": 1}, result["maxValue"])
assert.Equal(t, map[string]any{"$minKey": 1}, result["minValue"])
}
{
// Binary data types
{
// 0. Generic Binary
assert.Equal(t, map[string]any{"$binary": map[string]interface{}{"base64": "123456ABCDEF", "subType": "00"}}, result["profilePic"])
}
{
// Compiled Function
assert.Equal(t, map[string]any{"$binary": map[string]interface{}{"base64": "cHJpbnQoJ0hlbGxvIFdvcmxkJyk=", "subType": "01"}}, result["compiledFunction"])
}
{
// 3 + 4 UUID
assert.Equal(t, "856e56ff-cbb0-411e-855a-98b08b875140", result["unique_id"])
assert.Equal(t, map[string]any{"$binary": map[string]any{"base64": "c8edabc3f7384ca3b68dab92a91478a3", "subType": "04"}}, result["Binary"])
}
{
// Checksum
assert.Equal(t, map[string]any{"$binary": map[string]interface{}{"base64": "1B2M2Y8AsgTpgAmY7PhCfg==", "subType": "05"}}, result["fileChecksum"])
}
{
// Secure Data
assert.Equal(t, map[string]any{"$binary": map[string]interface{}{"base64": "YWJjZGVmZ2hpamtsbW5vcA==", "subType": "06"}}, result["secureData"])
}
}
{
// Other data types
// Javascript
assert.Equal(t, map[string]any{"$code": "function() {return 0.10;}"}, result["calcDiscount"])
// Regular Expressions
assert.Equal(t, map[string]any{"$options": "", "$regex": `@example\.com$`}, result["emailPattern"])
}
}

func TestBsonDocToMap(t *testing.T) {
Expand Down

0 comments on commit d88a9b4

Please sign in to comment.