Collections are enumerated live rather than from a list, so a collection added later is backed up with no code change. Documents are written as the raw BSON the driver returned, so Decimal128, ObjectId, DateTime and binary subtypes survive byte for byte.
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package backup
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
)
|
|
|
|
// testDB connects to the MongoDB named by MONGO_TEST_URI and returns a client
|
|
// plus a database name unique to this test, dropped when the test ends.
|
|
//
|
|
// Skips rather than fails when the variable is unset: these tests need a real
|
|
// server, and a developer without one should still be able to run the rest of
|
|
// the suite.
|
|
func testDB(t *testing.T) (*mongo.Client, string) {
|
|
t.Helper()
|
|
uri := os.Getenv("MONGO_TEST_URI")
|
|
if uri == "" {
|
|
t.Skip("MONGO_TEST_URI is not set; skipping tests that need MongoDB")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
client, err := mongo.Connect(options.Client().ApplyURI(uri))
|
|
if err != nil {
|
|
t.Fatalf("connect: %v", err)
|
|
}
|
|
if err := client.Ping(ctx, nil); err != nil {
|
|
t.Fatalf("ping: %v", err)
|
|
}
|
|
name := fmt.Sprintf("vantage_test_%d", time.Now().UnixNano())
|
|
t.Cleanup(func() {
|
|
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
_ = client.Database(name).Drop(c)
|
|
_ = client.Disconnect(c)
|
|
})
|
|
return client, name
|
|
}
|