@@ -2706,3 +2706,69 @@ func assertImported(t *testing.T, client *mongo.Client, db, coll string) {
27062706 assert .EqualValues (t , 2 , n , "%s.%s should have 2 imported documents" , db , coll )
27072707 require .NoError (t , client .Database (db ).Collection (coll ).Drop (context .Background ()))
27082708}
2709+
2710+ // TestRoundTripDecimal128 verifies that a Decimal128 value survives an
2711+ // export-then-import round-trip.
2712+ func TestRoundTripDecimal128 (t * testing.T ) {
2713+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
2714+
2715+ const dbName = "mongoimport_decimal128_test"
2716+ const collName = "dec128"
2717+
2718+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
2719+ require .NoError (t , err )
2720+ client , err := sessionProvider .GetSession ()
2721+ require .NoError (t , err )
2722+ t .Cleanup (func () {
2723+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
2724+ t .Errorf ("dropping test database: %v" , err )
2725+ }
2726+ })
2727+
2728+ dec , err := bson .ParseDecimal128 ("123456789012345678901234567890" )
2729+ require .NoError (t , err )
2730+ testDoc := bson.D {{"_id" , "foo" }, {"x" , dec }}
2731+
2732+ coll := client .Database (dbName ).Collection (collName )
2733+ _ , err = coll .InsertOne (t .Context (), testDoc )
2734+ require .NoError (t , err )
2735+
2736+ exportToolOptions , err := testutil .GetToolOptions ()
2737+ require .NoError (t , err )
2738+ exportToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
2739+ me , err := mongoexport .New (mongoexport.Options {
2740+ ToolOptions : exportToolOptions ,
2741+ OutputFormatOptions : & mongoexport.OutputFormatOptions {
2742+ Type : "json" ,
2743+ JSONFormat : "canonical" ,
2744+ },
2745+ InputOptions : & mongoexport.InputOptions {},
2746+ })
2747+ require .NoError (t , err )
2748+ defer me .Close ()
2749+ tmpFile , err := os .CreateTemp (t .TempDir (), "export-*.json" )
2750+ require .NoError (t , err )
2751+ _ , err = me .Export (tmpFile )
2752+ require .NoError (t , err )
2753+ require .NoError (t , tmpFile .Close ())
2754+
2755+ require .NoError (t , coll .Drop (t .Context ()))
2756+
2757+ importToolOptions , err := testutil .GetToolOptions ()
2758+ require .NoError (t , err )
2759+ importToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
2760+ mi , err := New (Options {
2761+ ToolOptions : importToolOptions ,
2762+ InputOptions : & InputOptions {File : tmpFile .Name (), ParseGrace : "stop" },
2763+ IngestOptions : & IngestOptions {},
2764+ })
2765+ require .NoError (t , err )
2766+ imported , _ , err := mi .ImportDocuments ()
2767+ require .NoError (t , err )
2768+ assert .EqualValues (t , 1 , imported , "should import 1 document" )
2769+
2770+ var result bson.D
2771+ err = coll .FindOne (t .Context (), bson.D {{"_id" , "foo" }}).Decode (& result )
2772+ require .NoError (t , err )
2773+ assert .Equal (t , testDoc , result , "imported doc should match original" )
2774+ }
0 commit comments