Skip to content

Commit 92c234a

Browse files
authored
TOOLS-4148 Convert test/qa-tests/jstests/import/collections.js to Go (#948)
1 parent 9939d6b commit 92c234a

File tree

2 files changed

+77
-77
lines changed

2 files changed

+77
-77
lines changed

mongoimport/mongoimport_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"bufio"
1111
"bytes"
1212
"context"
13+
"encoding/json"
1314
"fmt"
1415
"io"
1516
"math/rand"
1617
"os"
18+
"path/filepath"
1719
"reflect"
1820
"runtime"
1921
"strings"
@@ -2629,3 +2631,78 @@ func TestImportBooleanType(t *testing.T) {
26292631
assert.EqualValues(t, 1, n, "document with key=%q should exist", e.key)
26302632
}
26312633
}
2634+
2635+
// TestImportCollectionNameDerivation verifies that mongoimport correctly
2636+
// derives the collection name from the input filename.
2637+
func TestImportCollectionNameDerivation(t *testing.T) {
2638+
testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)
2639+
2640+
const dbName = "mongoimport_collections_test"
2641+
2642+
sessionProvider, _, err := testutil.GetBareSessionProvider()
2643+
require.NoError(t, err)
2644+
client, err := sessionProvider.GetSession()
2645+
require.NoError(t, err)
2646+
t.Cleanup(func() {
2647+
if err := client.Database(dbName).Drop(context.Background()); err != nil {
2648+
t.Errorf("dropping test database: %v", err)
2649+
}
2650+
if err := client.Database("testdb2").Drop(context.Background()); err != nil {
2651+
t.Errorf("dropping testdb2: %v", err)
2652+
}
2653+
})
2654+
2655+
tmpDir := t.TempDir()
2656+
rows := []map[string]int{{"a": 1, "b": 2, "c": 3}, {"a": 4, "b": 5, "c": 6}}
2657+
var sb strings.Builder
2658+
for _, row := range rows {
2659+
b, err := json.Marshal(row)
2660+
require.NoError(t, err)
2661+
sb.Write(b)
2662+
sb.WriteByte('\n')
2663+
}
2664+
2665+
fooBlahJSON := writeTestFile(t, tmpDir, "foo.blah.json", sb.String())
2666+
importFromFile(t, fooBlahJSON, dbName, "")
2667+
assertImported(t, client, dbName, "foo.blah")
2668+
2669+
fooBlahJSONBackup := writeTestFile(t, tmpDir, "foo.blah.json.backup", sb.String())
2670+
importFromFile(t, fooBlahJSONBackup, dbName, "")
2671+
assertImported(t, client, dbName, "foo.blah.json")
2672+
2673+
importFromFile(t, fooBlahJSON, dbName, "testcoll1")
2674+
assertImported(t, client, dbName, "testcoll1")
2675+
2676+
importFromFile(t, fooBlahJSON, "testdb2", "")
2677+
assertImported(t, client, "testdb2", "foo.blah")
2678+
}
2679+
2680+
func writeTestFile(t *testing.T, dir, name, content string) string {
2681+
t.Helper()
2682+
path := filepath.Join(dir, name)
2683+
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
2684+
return path
2685+
}
2686+
2687+
func importFromFile(t *testing.T, filePath, dbOverride, collOverride string) {
2688+
t.Helper()
2689+
toolOpts, err := testutil.GetToolOptions()
2690+
require.NoError(t, err)
2691+
toolOpts.Namespace = &options.Namespace{DB: dbOverride, Collection: collOverride}
2692+
mi, err := New(Options{
2693+
ToolOptions: toolOpts,
2694+
InputOptions: &InputOptions{File: filePath, ParseGrace: "stop"},
2695+
IngestOptions: &IngestOptions{},
2696+
})
2697+
require.NoError(t, err)
2698+
_, _, err = mi.ImportDocuments()
2699+
require.NoError(t, err)
2700+
}
2701+
2702+
func assertImported(t *testing.T, client *mongo.Client, db, coll string) {
2703+
t.Helper()
2704+
n, err := client.Database(db).Collection(coll).CountDocuments(context.Background(), bson.D{})
2705+
require.NoError(t, err)
2706+
assert.EqualValues(t, 2, n, "%s.%s should have 2 imported documents", db, coll)
2707+
require.NoError(t, client.Database(db).Collection(coll).Drop(context.Background()))
2708+
}

test/qa-tests/jstests/import/collections.js

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

0 commit comments

Comments
 (0)