Skip to content

Commit c2b897c

Browse files
committed
CDRIVER-3877 test BSON string produced (#730)
1 parent d21ab4b commit c2b897c

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/libbson/src/bson/bson.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2583,7 +2583,7 @@ _bson_as_json_visit_int64 (const bson_iter_t *iter,
25832583

25842584
if (state->mode == BSON_JSON_MODE_CANONICAL) {
25852585
bson_string_append_printf (
2586-
state->str, "{ \"$numberLong\" : \"%" PRId64 "\"}", v_int64);
2586+
state->str, "{ \"$numberLong\" : \"%" PRId64 "\" }", v_int64);
25872587
} else {
25882588
bson_string_append_printf (state->str, "%" PRId64, v_int64);
25892589
}

src/libbson/tests/test-bson.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,7 +2446,8 @@ test_bson_binary_null_handling (void)
24462446
}
24472447

24482448
static void
2449-
test_bson_append_null_from_utf8_or_symbol (void) {
2449+
test_bson_append_null_from_utf8_or_symbol (void)
2450+
{
24502451
bson_t bson;
24512452
bson_iter_t iter;
24522453

@@ -2461,6 +2462,46 @@ test_bson_append_null_from_utf8_or_symbol (void) {
24612462
bson_destroy (&bson);
24622463
}
24632464

2465+
static void
2466+
test_bson_as_json_string (void)
2467+
{
2468+
bson_t *all_types;
2469+
int i;
2470+
char *actual;
2471+
const char *expected =
2472+
"{ \"double\" : { \"$numberDouble\" : \"1.0\" }, \"string\" : "
2473+
"\"string_example\", \"document\" : { \"x\" : \"y\" }, \"document\" : [ "
2474+
"\"x\" ], \"binary\" : { \"$binary\" : { \"base64\": \"ZGF0YQ==\", "
2475+
"\"subType\" : \"00\" } }, \"undefined\" : { \"$undefined\" : true }, "
2476+
"\"oid\" : { \"$oid\" : \"000000000000000000000000\" }, \"bool\" : true, "
2477+
"\"datetime\" : { \"$date\" : { \"$numberLong\" : \"123\" } }, \"null\" "
2478+
": null, \"regex\" : { \"$regularExpression\" : { \"pattern\" : \"a+\", "
2479+
"\"options\" : \"\" } }, \"dbpointer\" : { \"$dbPointer\" : { \"$ref\" : "
2480+
"\"collection\", \"$id\" : { \"$oid\" : \"000000000000000000000000\" } } "
2481+
"}, \"code\" : { \"$code\" : \"var x = 1;\" }, \"symbol\" : { "
2482+
"\"$symbol\" : \"symbol_example\" }, \"code\" : { \"$code\" : \"var x = "
2483+
"1;\" }, \"code_w_scope\" : { \"$code\" : \"var x = 1;\", \"$scope\" : { "
2484+
"} }, \"int32\" : { \"$numberInt\" : \"1\" }, \"timestamp\" : { "
2485+
"\"$timestamp\" : { \"t\" : 2, \"i\" : 3 } }, \"int64\" : { "
2486+
"\"$numberLong\" : \"4\" }, \"decimal128\" : { \"$numberDecimal\" : "
2487+
"\"1.23456789\" }, \"minkey\" : { \"$minKey\" : 1 }, \"maxkey\" : { "
2488+
"\"$maxKey\" : 1 }, \"\" : { \"$numberInt\" : \"-1\" } }";
2489+
2490+
all_types = bson_with_all_types ();
2491+
actual = bson_as_canonical_extended_json (all_types, NULL);
2492+
2493+
for (i = 0; i < strlen (expected); i++) {
2494+
if (expected[i] != actual[i]) {
2495+
test_error ("character mismatch at %d. Expected: %s, got %s",
2496+
i,
2497+
expected,
2498+
actual);
2499+
}
2500+
}
2501+
2502+
bson_free (actual);
2503+
}
2504+
24642505
void
24652506
test_bson_install (TestSuite *suite)
24662507
{
@@ -2555,5 +2596,8 @@ test_bson_install (TestSuite *suite)
25552596
test_bson_iter_init_from_data_at_offset);
25562597
TestSuite_Add (
25572598
suite, "/bson/value/null_handling", test_bson_binary_null_handling);
2558-
TestSuite_Add (suite, "/bson/append_null_from_utf8_or_symbol", test_bson_append_null_from_utf8_or_symbol);
2599+
TestSuite_Add (suite,
2600+
"/bson/append_null_from_utf8_or_symbol",
2601+
test_bson_append_null_from_utf8_or_symbol);
2602+
TestSuite_Add (suite, "/bson/as_json_string", test_bson_as_json_string);
25592603
}

0 commit comments

Comments
 (0)