@@ -17,6 +17,7 @@ import (
1717 "runtime"
1818 "strings"
1919 "testing"
20+ "time"
2021
2122 "github.com/mongodb/mongo-tools/common"
2223 "github.com/mongodb/mongo-tools/common/db"
@@ -1610,3 +1611,95 @@ func TestRoundTripBasicData(t *testing.T) {
16101611 assert .EqualValues (t , 1 , c , "document with _id %d should exist after round-trip" , i )
16111612 }
16121613}
1614+
1615+ // TestRoundTripDataTypes verifies that documents with diverse BSON types
1616+ // survive an export-then-import round-trip intact.
1617+ func TestRoundTripDataTypes (t * testing.T ) {
1618+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
1619+
1620+ const dbName = "mongoimport_roundtrip_datatypes_test"
1621+ const collName = "data"
1622+
1623+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
1624+ require .NoError (t , err )
1625+ client , err := sessionProvider .GetSession ()
1626+ require .NoError (t , err )
1627+ t .Cleanup (func () {
1628+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
1629+ t .Errorf ("dropping test database: %v" , err )
1630+ }
1631+ })
1632+
1633+ coll := client .Database (dbName ).Collection (collName )
1634+ docs := []any {
1635+ bson.D {{"num" , 1 }},
1636+ bson.D {{"flt" , 1.0 }},
1637+ bson.D {{"str" , "1" }},
1638+ bson.D {{"obj" , bson.D {{"a" , 1 }}}},
1639+ bson.D {{"arr" , bson.A {0 , 1 }}},
1640+ bson.D {{"bd" , bson.Binary {Subtype : 0x00 , Data : []byte {0xd7 , 0x6d , 0xf8 }}}},
1641+ bson.D {
1642+ {
1643+ "date" ,
1644+ bson .NewDateTimeFromTime (time .Date (2009 , 8 , 27 , 12 , 34 , 56 , 789000000 , time .UTC )),
1645+ },
1646+ },
1647+ bson.D {{"ts" , bson.Timestamp {T : 1234 , I : 5678 }}},
1648+ bson.D {{"rx" , bson.Regex {Pattern : `foo*"bar"` , Options : "i" }}},
1649+ }
1650+ _ , err = coll .InsertMany (t .Context (), docs )
1651+ require .NoError (t , err )
1652+
1653+ exportToolOptions , err := testutil .GetToolOptions ()
1654+ require .NoError (t , err )
1655+ exportToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
1656+
1657+ tmpFile , err := os .CreateTemp (t .TempDir (), "export-*.json" )
1658+ require .NoError (t , err )
1659+
1660+ me , err := mongoexport .New (mongoexport.Options {
1661+ ToolOptions : exportToolOptions ,
1662+ OutputFormatOptions : & mongoexport.OutputFormatOptions {
1663+ Type : "json" ,
1664+ JSONFormat : "canonical" ,
1665+ },
1666+ InputOptions : & mongoexport.InputOptions {},
1667+ })
1668+ require .NoError (t , err )
1669+ defer me .Close ()
1670+ _ , err = me .Export (tmpFile )
1671+ require .NoError (t , err )
1672+ require .NoError (t , tmpFile .Close ())
1673+
1674+ require .NoError (t , coll .Drop (t .Context ()))
1675+
1676+ importToolOptions , err := testutil .GetToolOptions ()
1677+ require .NoError (t , err )
1678+ importToolOptions .Namespace = & options.Namespace {DB : dbName , Collection : collName }
1679+ mi , err := New (Options {
1680+ ToolOptions : importToolOptions ,
1681+ InputOptions : & InputOptions {File : tmpFile .Name ()},
1682+ IngestOptions : & IngestOptions {},
1683+ })
1684+ require .NoError (t , err )
1685+ imported , _ , err := mi .ImportDocuments ()
1686+ require .NoError (t , err )
1687+ assert .EqualValues (t , 9 , imported , "should import all 9 documents" )
1688+
1689+ count , err := coll .CountDocuments (t .Context (), bson.D {})
1690+ require .NoError (t , err )
1691+ assert .EqualValues (t , 9 , count , "collection should have all 9 documents after round-trip" )
1692+
1693+ for _ , q := range []bson.D {
1694+ {{"num" , 1 }},
1695+ {{"flt" , 1.0 }},
1696+ {{"str" , "1" }},
1697+ {{"obj" , bson.D {{"a" , 1 }}}},
1698+ {{"arr" , bson.A {0 , 1 }}},
1699+ {{"rx" , bson.D {{"$exists" , true }}}},
1700+ } {
1701+ c , err := coll .CountDocuments (t .Context (), q )
1702+ require .NoError (t , err )
1703+ assert .EqualValues (t , 1 , c , "document matching %v should exist after round-trip" , q )
1704+ }
1705+ }
0 commit comments