Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for decimal128 and timestamp #78

Merged
merged 12 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ issues:
max-same-issues: 0

exclude-use-default: false
exclude-rules: []
exclude-rules:
# that's a valid usage of bson.D / bson.E
- linters: [govet]
text: "composites: go.mongodb.org/mongo-driver/v2/bson.E struct literal uses unkeyed fields"


run:
timeout: 3m
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
Expand All @@ -12,17 +16,23 @@ github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver/v2 v2.0.0 h1:Jfd7XpdZa9yk3eY774bO7SWVb30noLSirL9nKTpavhI=
go.mongodb.org/mongo-driver/v2 v2.0.0/go.mod h1:nSjmNq4JUstE8IRZKTktLgMHM4F1fccL6HGX1yh+8RA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
116 changes: 115 additions & 1 deletion wireclient/wireclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"

"github.com/FerretDB/wire"
"github.com/FerretDB/wire/wirebson"
)

// logWriter provides [io.Writer] for [testing.TB].
Expand Down Expand Up @@ -56,14 +62,34 @@ func logger(tb testing.TB) *slog.Logger {
return slog.New(h)
}

func TestConn(t *testing.T) {
// setup waits for FerretDB or MongoDB to start and returns the URI.
func setup(t testing.TB) string {
t.Helper()

if testing.Short() {
t.Skip("skipping integration tests for -short")
}

uri := os.Getenv("MONGODB_URI")
require.NotEmpty(t, uri, "MONGODB_URI environment variable must be set; set it or run tests with `go test -short`")

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

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

err := conn.Close()
require.NoError(t, err)

return uri
}

func TestConn(t *testing.T) {
t.Parallel()

uri := setup(t)

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

Expand Down Expand Up @@ -102,3 +128,91 @@ func TestConn(t *testing.T) {
})
})
}

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

uri := setup(t)

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

var conn *Conn
var mConn *mongo.Client

// avoid shadowing err in subtests
{
var err error

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)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

t.Cleanup(func() {
require.NoError(t, conn.Close())
require.NoError(t, mConn.Disconnect(ctx))
})
}

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)
})

t.Run("Timestamp", func(t *testing.T) {
// FIXME
})
}
Loading