@@ -2556,3 +2556,78 @@ func TestRoundTripSortAndSkip(t *testing.T) {
25562556 assert .EqualValues (t , 1 , c , "document with a=%d should exist" , i + 20 )
25572557 }
25582558}
2559+
2560+ // TestImportBooleanType verifies that mongoimport correctly imports legacy
2561+ // JSON with Boolean() constructor syntax using --legacy (from boolean_type.js).
2562+ func TestImportBooleanType (t * testing.T ) {
2563+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
2564+
2565+ const dbName = "mongoimport_booleantype_test"
2566+ const collName = "testcollbool"
2567+
2568+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
2569+ require .NoError (t , err )
2570+ client , err := sessionProvider .GetSession ()
2571+ require .NoError (t , err )
2572+ t .Cleanup (func () {
2573+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
2574+ t .Errorf ("dropping test database: %v" , err )
2575+ }
2576+ })
2577+
2578+ entries := []struct { key , expr string }{
2579+ {"a" , "Boolean(1)" },
2580+ {"b" , "Boolean(0)" },
2581+ {"c" , "Boolean(140)" },
2582+ {"d" , "Boolean(-140.5)" },
2583+ {"e" , "Boolean(Boolean(1))" },
2584+ {"f" , "Boolean(Boolean(0))" },
2585+ {"g" , "Boolean('')" },
2586+ {"h" , "Boolean('f')" },
2587+ {"i" , "Boolean(null)" },
2588+ {"j" , "Boolean(undefined)" },
2589+ {"k" , "Boolean(true)" },
2590+ {"l" , "Boolean(false)" },
2591+ {"m" , "Boolean(true, false)" },
2592+ {"n" , "Boolean(false, true)" },
2593+ {"o" , "[ Boolean(1), Boolean(0), Date(23) ]" },
2594+ {"p" , "Boolean(Date(15))" },
2595+ {"q" , "Boolean(0x585)" },
2596+ {"r" , "Boolean(0x0)" },
2597+ {"s" , "Boolean()" },
2598+ }
2599+
2600+ // This isn't actually valid JSON that we're generating. It's a JSON-ish format that the legacy
2601+ // shell generated and that mongoimport supports with the `--legacy` flag.
2602+ tmpFile , err := os .CreateTemp (t .TempDir (), "boolean-*.json" )
2603+ require .NoError (t , err )
2604+ for _ , e := range entries {
2605+ _ , err = fmt .Fprintf (tmpFile , "{ key: '%s', bool: %s }\n " , e .key , e .expr )
2606+ require .NoError (t , err )
2607+ }
2608+ require .NoError (t , tmpFile .Close ())
2609+
2610+ importToolOptions , err := testutil .GetToolOptions ()
2611+ require .NoError (t , err )
2612+ importToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
2613+ mi , err := New (Options {
2614+ ToolOptions : importToolOptions ,
2615+ InputOptions : & InputOptions {
2616+ File : tmpFile .Name (),
2617+ ParseGrace : "stop" ,
2618+ Legacy : true ,
2619+ },
2620+ IngestOptions : & IngestOptions {},
2621+ })
2622+ require .NoError (t , err )
2623+ imported , _ , err := mi .ImportDocuments ()
2624+ require .NoError (t , err )
2625+ assert .EqualValues (t , len (entries ), imported , "should import all documents" )
2626+
2627+ coll := client .Database (dbName ).Collection (collName )
2628+ for _ , e := range entries {
2629+ n , err := coll .CountDocuments (t .Context (), bson.D {{"key" , e .key }})
2630+ require .NoError (t , err )
2631+ assert .EqualValues (t , 1 , n , "document with key=%q should exist" , e .key )
2632+ }
2633+ }
0 commit comments