Skip to content

Commit e39dcd6

Browse files
committed
Convert test/qa-tests/jstests/export/data_types.js to Go
This PR adds a new `TestRoundTripDataTypes` test to `mongoimport/mongoimport_test.go`.
1 parent 1e1b554 commit e39dcd6

3 files changed

Lines changed: 94 additions & 72 deletions

File tree

.ai-plans/2026-03-13/js-to-go-test-migration/plan.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ The mongoexport library API: create `mongoexport.MongoExport{Options: opts}`, th
252252

253253
- [x] **Step 1: Convert `basic_data.js`** (NEW) — `TestRoundTripBasicData` in `mongoimport/mongoimport_test.go`: inserts 50 `{_id: N}` docs, exports to a temp file via mongoexport, drops the collection, imports via mongoimport, then verifies count == 50 and each `_id 0..49` exists.
254254

255-
- [ ] **Step 2: Convert `data_types.js`** (NEW) — `TestExportDataTypes`: insert documents with int, float, string, subdoc, array, BinData, ISODate, Timestamp, Regex; verify export contains correct Extended JSON representations.
255+
- [x] **Step 2: Convert `data_types.js`** (NEW) — `TestRoundTripDataTypes` in `mongoimport/mongoimport_test.go`: inserts 9 documents covering int, float, string, subdocument, array, BinData, ISODate, Timestamp, and Regex; exports to temp file; drops collection; imports via mongoimport; asserts count == 9 and each typed document can be found by value (Regex via `$exists`).
256256

257257
- [ ] **Step 3: Convert `export_views.js`** (NEW) — `TestExportViews`: create a MongoDB view with a pipeline, export it, verify exported data matches the view's pipeline output.
258258

mongoimport/mongoimport_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"runtime"
1818
"strings"
1919
"testing"
20+
"time"
2021

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

test/qa-tests/jstests/export/data_types.js

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)