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