Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions mongoimport/mongoimport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"context"
"fmt"
"io"
"math/rand"
"os"
"reflect"
"runtime"
Expand Down Expand Up @@ -2479,3 +2480,77 @@ func exportAndImportWithQuery(
require.NoError(t, err)
return n
}

// TestRoundTripSortAndSkip verifies that mongoexport --sort and --skip
// correctly affect which documents are exported.
func TestRoundTripSortAndSkip(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)

const dbName = "mongoimport_roundtrip_sortskip_test"
const collName = "data"

sessionProvider, _, err := testutil.GetBareSessionProvider()
require.NoError(t, err)
client, err := sessionProvider.GetSession()
require.NoError(t, err)
t.Cleanup(func() {
if err := client.Database(dbName).Drop(context.Background()); err != nil {
t.Errorf("dropping test database: %v", err)
}
})

coll := client.Database(dbName).Collection(collName)
docs := make([]any, 50)
for i := range 50 {
docs[i] = bson.D{{"a", i}}
}
Comment on lines +2504 to +2506
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this kinda defeats the purpose of testing sort behavior right? The js test inserted in scrambled order, whereas this inserts in sorted order.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, actuallly ... the AI pointed out that WiredTiger doesn't guarantee an order for an unsorted query ... but I added a shuffle.

rand.Shuffle(len(docs), func(i, j int) {
docs[i], docs[j] = docs[j], docs[i]
})

_, err = coll.InsertMany(t.Context(), docs)
require.NoError(t, err)

exportToolOptions, err := testutil.GetToolOptions()
require.NoError(t, err)
exportToolOptions.Namespace = &options.Namespace{DB: dbName, Collection: collName}
me, err := mongoexport.New(mongoexport.Options{
ToolOptions: exportToolOptions,
OutputFormatOptions: &mongoexport.OutputFormatOptions{
Type: "json", JSONFormat: "relaxed",
},
InputOptions: &mongoexport.InputOptions{Sort: "{a:1}", Skip: 20},
})
require.NoError(t, err)
defer me.Close()
tmpFile, err := os.CreateTemp(t.TempDir(), "export-*.json")
require.NoError(t, err)
n, err := me.Export(tmpFile)
require.NoError(t, err)
require.NoError(t, tmpFile.Close())
assert.EqualValues(t, 30, n, "should export 30 documents after skipping 20")

require.NoError(t, coll.Drop(t.Context()))

importToolOptions, err := testutil.GetToolOptions()
require.NoError(t, err)
importToolOptions.Namespace = &options.Namespace{DB: dbName, Collection: collName}
mi, err := New(Options{
ToolOptions: importToolOptions,
InputOptions: &InputOptions{File: tmpFile.Name(), ParseGrace: "stop"},
IngestOptions: &IngestOptions{},
})
require.NoError(t, err)
imported, _, err := mi.ImportDocuments()
require.NoError(t, err)
assert.EqualValues(t, 30, imported, "should import all 30 exported documents")

count, err := coll.CountDocuments(t.Context(), bson.D{})
require.NoError(t, err)
assert.EqualValues(t, 30, count, "collection should have 30 documents")
for i := range 30 {
c, err := coll.CountDocuments(t.Context(), bson.D{{"a", i + 20}})
require.NoError(t, err)
assert.EqualValues(t, 1, c, "document with a=%d should exist", i+20)
}
}
69 changes: 0 additions & 69 deletions test/qa-tests/jstests/export/sort_and_skip.js

This file was deleted.