Skip to content

Commit 690a4ae

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 a95e9d2 commit 690a4ae

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

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

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

0 commit comments

Comments
 (0)