@@ -9,6 +9,7 @@ package mongoimport
99import (
1010 "bufio"
1111 "bytes"
12+ "context"
1213 "fmt"
1314 "io"
1415 "os"
@@ -24,7 +25,10 @@ import (
2425 "github.com/mongodb/mongo-tools/common/testutil"
2526 "github.com/mongodb/mongo-tools/common/util"
2627 "github.com/mongodb/mongo-tools/common/wcwrapper"
28+ "github.com/mongodb/mongo-tools/mongoexport"
2729 . "github.com/smartystreets/goconvey/convey"
30+ "github.com/stretchr/testify/assert"
31+ "github.com/stretchr/testify/require"
2832 "go.mongodb.org/mongo-driver/v2/bson"
2933 mopt "go.mongodb.org/mongo-driver/v2/mongo/options"
3034)
@@ -1533,3 +1537,76 @@ func TestImportMIOSOE(t *testing.T) {
15331537
15341538 _ = database .Drop (t .Context ())
15351539}
1540+
1541+ // TestRoundTripBasicData verifies that data exported by mongoexport can be
1542+ // fully restored by mongoimport with all documents intact.
1543+ func TestRoundTripBasicData (t * testing.T ) {
1544+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
1545+
1546+ const dbName = "mongoimport_roundtrip_basic_test"
1547+ const collName = "data"
1548+
1549+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
1550+ require .NoError (t , err )
1551+ client , err := sessionProvider .GetSession ()
1552+ require .NoError (t , err )
1553+ t .Cleanup (func () {
1554+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
1555+ t .Errorf ("dropping test database: %v" , err )
1556+ }
1557+ })
1558+
1559+ coll := client .Database (dbName ).Collection (collName )
1560+ var docs []bson.D
1561+ for i := range 50 {
1562+ docs = append (docs , bson.D {{"_id" , int32 (i )}})
1563+ }
1564+ _ , err = coll .InsertMany (t .Context (), docs )
1565+ require .NoError (t , err )
1566+
1567+ exportToolOptions , err := testutil .GetToolOptions ()
1568+ require .NoError (t , err )
1569+ exportToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
1570+
1571+ tmpFile , err := os .CreateTemp (t .TempDir (), "export-*.json" )
1572+ require .NoError (t , err )
1573+
1574+ me , err := mongoexport .New (mongoexport.Options {
1575+ ToolOptions : exportToolOptions ,
1576+ OutputFormatOptions : & mongoexport.OutputFormatOptions {
1577+ Type : "json" ,
1578+ JSONFormat : "canonical" ,
1579+ },
1580+ InputOptions : & mongoexport.InputOptions {},
1581+ })
1582+ require .NoError (t , err )
1583+ defer me .Close ()
1584+ _ , err = me .Export (tmpFile )
1585+ require .NoError (t , err )
1586+ require .NoError (t , tmpFile .Close ())
1587+
1588+ require .NoError (t , coll .Drop (t .Context ()))
1589+
1590+ importToolOptions , err := testutil .GetToolOptions ()
1591+ require .NoError (t , err )
1592+ importToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
1593+ mi , err := New (Options {
1594+ ToolOptions : importToolOptions ,
1595+ InputOptions : & InputOptions {File : tmpFile .Name ()},
1596+ IngestOptions : & IngestOptions {},
1597+ })
1598+ require .NoError (t , err )
1599+ imported , _ , err := mi .ImportDocuments ()
1600+ require .NoError (t , err )
1601+ assert .EqualValues (t , 50 , imported , "should import all 50 documents" )
1602+
1603+ count , err := coll .CountDocuments (t .Context (), bson.D {})
1604+ require .NoError (t , err )
1605+ assert .EqualValues (t , 50 , count , "collection should have all 50 documents after round-trip" )
1606+
1607+ for i := range int32 (50 ) {
1608+ c , err := coll .CountDocuments (t .Context (), bson.D {{"_id" , i }})
1609+ require .NoError (t , err )
1610+ assert .EqualValues (t , 1 , c , "document with _id %d should exist after round-trip" , i )
1611+ }
1612+ }
0 commit comments