From c8db65c7727435f2c42b57fe67326e11cf3f9290 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 10 Sep 2024 14:03:26 -0700 Subject: [PATCH] Improving our MongoDB tests (#900) --- lib/typing/mongo/bson_test.go | 202 +++++++++++++++++----------------- 1 file changed, 103 insertions(+), 99 deletions(-) diff --git a/lib/typing/mongo/bson_test.go b/lib/typing/mongo/bson_test.go index 1b70941fb..5f9681394 100644 --- a/lib/typing/mongo/bson_test.go +++ b/lib/typing/mongo/bson_test.go @@ -1,16 +1,14 @@ package mongo import ( + "encoding/json" "testing" "time" "github.com/artie-labs/transfer/lib/typing/ext" - - "go.mongodb.org/mongo-driver/bson/primitive" - - "go.mongodb.org/mongo-driver/bson" - "github.com/stretchr/testify/assert" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" ) // TestJSONEToMap tests every single type is listed here: @@ -66,15 +64,14 @@ func TestJSONEToMap(t *testing.T) { "test_null": null, "test_list": [1.0,2.0,3.0,4.0,"hello"], "test_nested_object": { - "a": { - "b": { - "c": "hello" - } + "a": {"b": {"c": "hello"}}, + "test_timestamp": {"$timestamp": { "t": 1678929517, "i": 1 }}, + "super_nested": { + "test_timestamp": {"$timestamp": { "t": 1678929517, "i": 1 }}, + "foo": "bar" } }, - "test_timestamp": { - "$timestamp": { "t": 1678929517, "i": 1 } - }, + "test_timestamp": {"$timestamp": { "t": 1678929517, "i": 1 }}, "test_nan": NaN, "test_nan_v2": {"$numberDouble": "NaN"}, "test_nan_string": "NaN", @@ -94,31 +91,29 @@ 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 + 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"]) + { + // int32 + assert.Equal(t, int32(1), result["quantity"]) + 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) @@ -131,74 +126,83 @@ 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"]) - - // 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"]) + { + // 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 + 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) {