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
Changes from 2 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
@@ -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
@@ -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=
@@ -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=
2 changes: 1 addition & 1 deletion wirebson/bson.go
Original file line number Diff line number Diff line change
@@ -183,8 +183,8 @@ func fromDriver(v any) (any, error) {
h, l := v.GetBytes()

return Decimal128{
L: l,
H: h,
L: l,
}, nil

default:
16 changes: 11 additions & 5 deletions wirebson/bson_test.go
Original file line number Diff line number Diff line change
@@ -294,7 +294,7 @@ var normalTestCases = []normalTestCase{
"int32", MustArray(int32(42), int32(0)),
"timestamp", MustArray(Timestamp(42), Timestamp(0)),
"int64", MustArray(int64(42), int64(0)),
"decimal128", MustArray(Decimal128{L: 42, H: 13}, Decimal128{}),
"decimal128", MustArray(Decimal128{H: 13, L: 42}, Decimal128{}),
),
mi: `
{
@@ -359,8 +359,8 @@ var normalTestCases = []normalTestCase{
int64(0),
],
"decimal128": [
Decimal128(42,13),
Decimal128(0,0),
Decimal128(H:13,L:42),
Decimal128(H:0,L:0),
],
}`,
j: `
@@ -731,11 +731,17 @@ var normalTestCases = []normalTestCase{
0x00,
},
doc: MustDocument(
"f", Decimal128{L: 42, H: 13},
"f", Decimal128{H: 13, L: 42},
),
mi: `
{
"f": Decimal128(42,13),
"f": Decimal128(H:13,L:42),
}`,
j: `
{
"f": {
"$numberDecimal": "2.39807672958224171050E-6156"
}
}`,
},
{
6 changes: 3 additions & 3 deletions wirebson/decimal128.go
Original file line number Diff line number Diff line change
@@ -21,8 +21,8 @@ import (

// Decimal128 represents BSON scalar type decimal128.
type Decimal128 struct {
L uint64
H uint64
L uint64
}

// sizeDecimal128 is a size of the encoding of [Decimal128] in bytes.
@@ -33,8 +33,8 @@ const sizeDecimal128 = 16
// b must be at least 16 ([sizeDecimal128]) bytes long; otherwise, encodeDecimal128 will panic.
// Only b[0:16] bytes are modified.
func encodeDecimal128(b []byte, v Decimal128) {
binary.LittleEndian.PutUint64(b, uint64(v.L))
binary.LittleEndian.PutUint64(b[8:], uint64(v.H))
binary.LittleEndian.PutUint64(b, uint64(v.L))
}

// decodeDecimal128 decodes [Decimal128] value from b.
@@ -47,8 +47,8 @@ func decodeDecimal128(b []byte) (Decimal128, error) {
return res, fmt.Errorf("DecodeDecimal128: expected at least %d bytes, got %d: %w", sizeDecimal128, len(b), ErrDecodeShortInput)
}

res.L = binary.LittleEndian.Uint64(b[:8])
res.H = binary.LittleEndian.Uint64(b[8:])
res.L = binary.LittleEndian.Uint64(b[:8])

return res, nil
}
6 changes: 3 additions & 3 deletions wirebson/logging.go
Original file line number Diff line number Diff line change
@@ -335,10 +335,10 @@ func logMessage(v any, indent, depth int, b *strings.Builder) {
b.WriteByte(')')

case Decimal128:
b.WriteString("Decimal128(")
b.WriteString(strconv.FormatUint(uint64(v.L), 10))
b.WriteByte(',')
b.WriteString("Decimal128(H:")
b.WriteString(strconv.FormatUint(uint64(v.H), 10))
b.WriteString(",L:")
b.WriteString(strconv.FormatUint(uint64(v.L), 10))
b.WriteByte(')')

default:
95 changes: 94 additions & 1 deletion wireclient/wireclient_test.go
Original file line number Diff line number Diff line change
@@ -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].
@@ -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)

@@ -102,3 +128,70 @@ func TestConn(t *testing.T) {
})
})
}

func TestDecimal128(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"))

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