Skip to content

Commit

Permalink
Add stub for timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Jan 10, 2025
1 parent d58276e commit eefdaac
Showing 1 changed file with 72 additions and 53 deletions.
125 changes: 72 additions & 53 deletions wireclient/wireclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,71 +129,90 @@ func TestConn(t *testing.T) {
})
}

func TestDecimal128(t *testing.T) {
func TestTypes(t *testing.T) {
t.Parallel()

uri := setup(t)

d := wirebson.Decimal128{H: 13, L: 42}
md := bson.NewDecimal128(13, 42)

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
t.Cleanup(cancel)

conn := ConnectPing(ctx, uri, logger(t))
require.NotNil(t, conn)

err := conn.Login(ctx, "username", "password", "admin")
require.NoError(t, err)

opts := options.Client().ApplyURI(uri).SetAuth(options.Credential{Username: "username", Password: "password"})
mConn, err := mongo.Connect(opts)
require.NoError(t, err)

db := mConn.Database(t.Name())
require.NoError(t, db.Drop(ctx))

_, body, err := conn.Request(ctx, wire.MustOpMsg(
"insert", "test",
"documents", wirebson.MustArray(wirebson.MustDocument("_id", "d", "v", d)),
"$db", t.Name(),
))
require.NoError(t, err)

doc, err := body.(*wire.OpMsg).DecodeDeepDocument()
require.NoError(t, err)
require.Equal(t, 1.0, doc.Get("ok"))
var conn *Conn
var mConn *mongo.Client

_, err = db.Collection("test").InsertOne(ctx, bson.D{{"_id", "md"}, {"v", md}})
require.NoError(t, err)
// avoid shadowing err in subtests
{
var err error

_, body, err = conn.Request(ctx, wire.MustOpMsg(
"find", "test",
"sort", wirebson.MustDocument("_id", int32(1)),
"$db", t.Name(),
))
require.NoError(t, err)
conn = ConnectPing(ctx, uri, logger(t))
require.NotNil(t, conn)

doc, err = body.(*wire.OpMsg).DecodeDeepDocument()
require.NoError(t, err)
require.Equal(t, 1.0, doc.Get("ok"))
err = conn.Login(ctx, "username", "password", "admin")
require.NoError(t, err)

expected := wirebson.MustArray(
wirebson.MustDocument("_id", "d", "v", d),
wirebson.MustDocument("_id", "md", "v", d),
)
require.Equal(t, expected, doc.Get("cursor").(*wirebson.Document).Get("firstBatch"))
opts := options.Client().ApplyURI(uri).SetAuth(options.Credential{Username: "username", Password: "password"})
mConn, err = mongo.Connect(opts)
require.NoError(t, err)

c, err := db.Collection("test").Find(ctx, bson.D{}, options.Find().SetSort(bson.D{{"_id", 1}}))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, conn.Close())
require.NoError(t, mConn.Disconnect(ctx))
})
}

var res bson.A
err = c.All(ctx, &res)
require.NoError(t, err)
t.Run("Decimal128", func(t *testing.T) {
d := wirebson.Decimal128{H: 13, L: 42}
md := bson.NewDecimal128(13, 42)

db := mConn.Database(t.Name())
require.NoError(t, db.Drop(ctx))

_, body, err := conn.Request(ctx, wire.MustOpMsg(
"insert", "test",
"documents", wirebson.MustArray(wirebson.MustDocument("_id", "d", "v", d)),
"$db", t.Name(),
))
require.NoError(t, err)

doc, err := body.(*wire.OpMsg).DecodeDeepDocument()
require.NoError(t, err)
require.Equal(t, 1.0, doc.Get("ok"))

_, err = db.Collection("test").InsertOne(ctx, bson.D{{"_id", "md"}, {"v", md}})
require.NoError(t, err)

_, body, err = conn.Request(ctx, wire.MustOpMsg(
"find", "test",
"sort", wirebson.MustDocument("_id", int32(1)),
"$db", t.Name(),
))
require.NoError(t, err)

doc, err = body.(*wire.OpMsg).DecodeDeepDocument()
require.NoError(t, err)
require.Equal(t, 1.0, doc.Get("ok"))

expected := wirebson.MustArray(
wirebson.MustDocument("_id", "d", "v", d),
wirebson.MustDocument("_id", "md", "v", d),
)
require.Equal(t, expected, doc.Get("cursor").(*wirebson.Document).Get("firstBatch"))

c, err := db.Collection("test").Find(ctx, bson.D{}, options.Find().SetSort(bson.D{{"_id", 1}}))
require.NoError(t, err)

var res bson.A
err = c.All(ctx, &res)
require.NoError(t, err)

mExpected := bson.A{
bson.D{{"_id", "d"}, {"v", md}},
bson.D{{"_id", "md"}, {"v", md}},
}
require.Equal(t, mExpected, res)
})

mExpected := bson.A{
bson.D{{"_id", "d"}, {"v", md}},
bson.D{{"_id", "md"}, {"v", md}},
}
require.Equal(t, mExpected, res)
t.Run("Timestamp", func(t *testing.T) {
// FIXME
})
}

0 comments on commit eefdaac

Please sign in to comment.