mongo-tools-js-to-go

द्वारा mongodb

mongo-tools में JS/resmoke इंटीग्रेशन टेस्ट को Go testify टेस्ट में बदलते समय उपयोग करें

npx skills add https://github.com/mongodb/mongo-tools --skill mongo-tools-js-to-go

mongo-tools JS-to-Go Test Conversion

Integration Test Boilerplate

For tests within an individual tool's package (e.g. mongoimport/, mongodump/):

func TestFoo(t *testing.T) {
    testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)

    const (
        dbName   = "mongofoo_test_db"
        collName = "coll"
    )

    sessionProvider, _, err := testutil.GetBareSessionProvider()
    require.NoError(t, err)
    client, err := sessionProvider.GetSession()
    require.NoError(t, err)
    t.Cleanup(func() {
        _ = client.Database(dbName).Drop(context.Background())
    })

    coll := client.Database(dbName).Collection(collName)
    ns := &options.Namespace{DB: dbName, Collection: collName}
    // ...
}

Test type constants: IntegrationTestType, ReplSetTestType, ShardedIntegrationTestType, SSLTestType, AuthTestType.

E2E Tests (Integration Suite)

Tests that exercise the full tool pipeline (dump+restore or export+import) belong in integration/dumprestore or integration/exportimport and use testify suites.

Add tests as methods on the existing suite type for the relevant package. The suite entry point looks like:

type DumpRestoreSuite struct {
    integrationSuite.IntegrationSuite
}

func TestDumpRestore(t *testing.T) {
    testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)
    suite.Run(t, new(DumpRestoreSuite))
}

Suite test methods:

func (s *DumpRestoreSuite) TestFoo() {
    ctx := s.Context()
    client := s.Client()
    dbName := s.DBName()
    // use s.Require() / s.Assert() instead of require.New(t)
}

Key suite methods:

MethodPurpose
s.Context()Test-scoped context
s.Client()New MongoDB client (caller responsible for Disconnect)
s.DBName(prefix...)DB name derived from test name, truncated to 63 chars
s.Require()testify require bound to current (sub)test
s.Assert()testify assert bound to current (sub)test
s.T()Current *testing.T
s.Run(name, func())Subtest (updates s.T() for the duration)

No manual DB cleanup neededBeforeTest in IntegrationSuite drops all non-system databases before each test method. Do not register t.Cleanup DB drops in suite tests.

Code Conventions

  • Callers before callees: test functions before helpers, helpers before the helpers they call
  • No comments that describe what code is doing — use named functions, subtests, and descriptive variable names instead. Comments explaining why are fine.
  • Use any not interface{}
  • Always include assertion messages: assert.Equal(t, want, got, "description of what is being tested")
  • Reset map[string]any{} before each Decode call — stale keys from previous decodes persist otherwise
  • Table-driven tests: define a type fooCase struct and loop over []fooCase
  • For error cases, don't use require.Error Use one of the following:
    • require.ErrorIs(t, err, something)
    • require.ErrorAs(t, err, &var)
    • require.ErrorContains(t, err, "substring")

Round-Trip Tests (export+import or dump+restore)

Round-trip tests belong in integration/exportimport or integration/dumprestore (suite methods), not in the individual tool packages.

Critical: drop the collection between export/dump and import/restore. Without this, the restore can't be verified.

_, err = me.Export(tmpFile)
s.Require().NoError(err)
s.Require().NoError(tmpFile.Close())

s.Require().NoError(coll.Drop(s.Context())) // ← required

// now import and verify

JSON Test File Generation

Use Go data structures + json.Marshal (not hardcoded strings):

upsertFile := writeJSONLinesFile(t, dir, "data.json", []map[string]any{
    {"_id": "one", "a": 1234, "b": "foo"},
    {"_id": "two", "a": "xxx", "b": "yyy"},
})

For BSON-type-preserving output (e.g. subdocument _ids), use bson.MarshalExtJSON(doc, relaxed, escapeHTML).

Key Helpers

HelperPurpose
testutil.GetBareSessionProvider()Get a live MongoDB client
testutil.GetToolOptions()Get tool options (connects to test mongod)
testutil.GetBareArgs()CLI args (--host, --port, auth) for exec.Command
runImportOpts(t, ns, file, IngestOptions{})Import, returns errors from New() too (use for option-validation tests)
importWithIngestOpts(t, ns, file, IngestOptions{})Import, fails test if New() errors
testutil.AssertBrokenPipeHandled(t, cmd)Verify a process handles SIGPIPE as a write error

Running Tests Locally

Tool-package test:

TOOLS_TESTING_INTEGRATION=true \
go test ./mongoimport/... -v -run TestFoo -count=1

Suite (e2e) test:

TOOLS_TESTING_INTEGRATION=true \
go test ./integration/dumprestore/... -v -run TestDumpRestore/TestFoo -count=1

Add TOOLS_TESTING_AUTH=1 TOOLS_TESTING_AUTH_USERNAME=... TOOLS_TESTING_AUTH_PASSWORD=... when testing against an auth-enabled mongod.

Always run the test locally before committing.

Conversion Process

  1. Read the JS test, note what testtype it requires
  2. Decide which form to use:
    • Full-pipeline e2e (dump+restore, export+import): add a method to the existing suite in integration/dumprestore or integration/exportimport
    • Tool-unit / single-tool: plain func TestFoo(t *testing.T) inside the tool's own package
  3. Write the Go test; prefer round-trip tests for export+import scenarios
  4. Run locally, confirm it passes
  5. Provide the user with a detailed explanation of the work. This should include:
    • What the JS test was testing
    • How the Go test tests the same thing
  6. Run a sub-agent that does not share the current context. Ask that agent to provide a review of the PR. This review should ensure the following things:
    • The new Go test covers the same things that the JS test does.
    • The new Go test follows all of the guidelines for Go tests in this skill.
    • The new Go test is generally similar to other Go tests that use testify.
  7. Share the review with the user and ask if they'd like you to make further changes based on the review
  8. Prompt the user before deleting the JS file
  9. Delete the JS file

Committing

  • The commit message should start with a TOOLS ticket number, like TOOLS-1234. Ask the user which ticket to use if you don't know which one is being used for this work.
  • Auto-fix formatting before committing: precious tidy -g
  • Lint check: precious lint -g

Linting Notes

  • bson.E struct literal uses unkeyed fields — suppressed by .golangci.yml, ignore it
  • precious tidy -g fixes indentation and line-length issues automatically

mongodb की और Skills

atlas-stream-processing
mongodb
MongoDB Atlas Stream Processing (ASP) वर्कफ़्लो का प्रबंधन करता है। वर्कस्पेस प्रावधान, डेटा स्रोत/सिंक कनेक्शन, प्रोसेसर जीवनचक्र संचालन को संभालता है,…
official
mongodb-atlas-stream-processing
mongodb
मोंगोडीबी एटलस स्ट्रीम प्रोसेसिंग (ASP) वर्कफ़्लो प्रबंधित करता है। वर्कस्पेस प्रावधान, डेटा स्रोत/सिंक कनेक्शन, प्रोसेसर जीवनचक्र संचालन को संभालता है,…
official
mongodb-connection
mongodb
किसी भी समर्थित ड्राइवर भाषा के लिए MongoDB क्लाइंट कनेक्शन कॉन्फ़िगरेशन (पूल, टाइमआउट, पैटर्न) को अनुकूलित करें। काम करते/अपडेट करते/समीक्षा करते समय इस स्किल का उपयोग करें…
official
mongodb-mcp-setup
mongodb
उपयोगकर्ताओं को MongoDB MCP सर्वर के प्रमुख विकल्पों को कॉन्फ़िगर करने में मार्गदर्शन करें। इस स्किल का उपयोग तब करें जब उपयोगकर्ता के पास MongoDB MCP सर्वर इंस्टॉल हो लेकिन उसने कॉन्फ़िगर नहीं किया हो…
official
mongodb-natural-language-querying
mongodb
प्राकृतिक भाषा का उपयोग करके केवल-पठनीय MongoDB क्वेरी (find) या aggregation pipelines उत्पन्न करें, जिसमें collection schema संदर्भ और नमूना दस्तावेज़ शामिल हों। इस skill का उपयोग करें…
official
mongodb-query-optimizer
mongodb
MongoDB क्वेरी अनुकूलन और इंडेक्सिंग में सहायता करें। केवल तब उपयोग करें जब उपयोगकर्ता अनुकूलन या प्रदर्शन के बारे में पूछे: "मैं इस क्वेरी को कैसे अनुकूलित करूं?", "मैं कैसे…
official
mongodb-schema-design
mongodb
MongoDB स्कीमा डिज़ाइन पैटर्न और एंटी-पैटर्न। डेटा मॉडल डिज़ाइन करते समय, स्कीमा की समीक्षा करते समय, SQL से माइग्रेट करते समय, या प्रदर्शन समस्याओं का समाधान करते समय उपयोग करें…
official
mongodb-search-and-ai
mongodb
MongoDB उपयोगकर्ताओं को Atlas Search (फुल-टेक्स्ट), Vector Search (सिमेंटिक), और Hybrid Search समाधानों को लागू करने और अनुकूलित करने में मार्गदर्शन करता है। इस स्किल का उपयोग तब करें जब…
official