$31 GRAYBYTE WORDPRESS FILE MANAGER $92

SERVER : in-mum-web1330.main-hosting.eu #1 SMP Mon Feb 10 22:45:17 UTC 2025
SERVER IP : 93.127.173.63 | ADMIN IP 216.73.216.215
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/go/pkg/mod/go.mongodb.org/mongo-driver@v1.11.3/mongo/

HOME
Current File : /opt/go/pkg/mod/go.mongodb.org/mongo-driver@v1.11.3/mongo//single_result_test.go
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package mongo

import (
	"context"
	"errors"
	"fmt"
	"testing"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/internal/testutil/assert"
)

func TestSingleResult(t *testing.T) {
	t.Run("Decode", func(t *testing.T) {
		t.Run("decode twice", func(t *testing.T) {
			// Test that Decode and DecodeBytes can be called more than once
			c, err := newCursor(newTestBatchCursor(1, 1), bson.DefaultRegistry)
			assert.Nil(t, err, "newCursor error: %v", err)

			sr := &SingleResult{cur: c, reg: bson.DefaultRegistry}
			var firstDecode, secondDecode bson.Raw
			err = sr.Decode(&firstDecode)
			assert.Nil(t, err, "Decode error: %v", err)
			err = sr.Decode(&secondDecode)
			assert.Nil(t, err, "Decode error: %v", err)

			decodeBytes, err := sr.DecodeBytes()
			assert.Nil(t, err, "DecodeBytes error: %v", err)

			assert.Equal(t, firstDecode, secondDecode, "expected contents %v, got %v", firstDecode, secondDecode)
			assert.Equal(t, firstDecode, decodeBytes, "expected contents %v, got %v", firstDecode, decodeBytes)
		})
		t.Run("decode with error", func(t *testing.T) {
			r := []byte("foo")
			sr := &SingleResult{rdr: r, err: errors.New("DecodeBytes error")}
			res, err := sr.DecodeBytes()
			resBytes := []byte(res)
			assert.Equal(t, r, resBytes, "expected contents %v, got %v", r, resBytes)
			assert.Equal(t, sr.err, err, "expected error %v, got %v", sr.err, err)
		})
	})

	t.Run("Err", func(t *testing.T) {
		sr := &SingleResult{}
		assert.Equal(t, ErrNoDocuments, sr.Err(), "expected error %v, got %v", ErrNoDocuments, sr.Err())
	})
}

func TestNewSingleResultFromDocument(t *testing.T) {
	// Mock a document returned by FindOne in SingleResult.
	t.Run("mock FindOne", func(t *testing.T) {
		findOneResult := bson.D{{"_id", 2}, {"foo", "bar"}}
		res := NewSingleResultFromDocument(findOneResult, nil, nil)

		// Assert that first, decoded document is as expected.
		findOneResultBytes, err := bson.Marshal(findOneResult)
		assert.Nil(t, err, "Marshal error: %v", err)
		expectedDecoded := bson.Raw(findOneResultBytes)
		decoded, err := res.DecodeBytes()
		assert.Nil(t, err, "DecodeBytes error: %v", err)
		assert.Equal(t, expectedDecoded, decoded,
			"expected decoded SingleResult to be %v, got %v", expectedDecoded, decoded)

		// Assert that RDR contents are set correctly after Decode.
		assert.NotNil(t, res.rdr, "expected non-nil rdr contents")
		assert.Equal(t, expectedDecoded, res.rdr,
			"expected RDR contents to be %v, got %v", expectedDecoded, res.rdr)

		// Assert that a call to cur.Next will return false, as there was only one document in
		// the slice passed to NewSingleResultFromDocument.
		next := res.cur.Next(context.Background())
		assert.False(t, next, "expected call to Next to return false, got true")

		// Check for error on SingleResult.
		assert.Nil(t, res.Err(), "SingleResult error: %v", res.Err())

		// Assert that a call to cur.Close will not fail.
		err = res.cur.Close(context.Background())
		assert.Nil(t, err, "Close error: %v", err)
	})

	// Mock an error in SingleResult.
	t.Run("mock FindOne with error", func(t *testing.T) {
		mockErr := fmt.Errorf("mock error")
		res := NewSingleResultFromDocument(bson.D{}, mockErr, nil)

		// Assert that decoding returns the mocked error.
		_, err := res.DecodeBytes()
		assert.NotNil(t, err, "expected DecodeBytes error, got nil")
		assert.Equal(t, mockErr, err, "expected error %v, got %v", mockErr, err)

		// Check for error on SingleResult.
		assert.NotNil(t, res.Err(), "expected SingleResult error, got nil")
		assert.Equal(t, mockErr, res.Err(), "expected SingleResult error %v, got %v",
			mockErr, res.Err())

		// Assert that error is propagated to underlying cursor.
		assert.NotNil(t, res.cur.err, "expected underlying cursor, got nil")
		assert.Equal(t, mockErr, res.cur.err, "expected underlying cursor %v, got %v",
			mockErr, res.cur.err)
	})
}

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
1 Jan 1970 12.00 AM
root / root
0
address
--
25 Jan 2024 4.43 PM
root / root
0555
description
--
25 Jan 2024 4.43 PM
root / root
0555
gridfs
--
25 Jan 2024 4.43 PM
root / root
0555
integration
--
25 Jan 2024 4.43 PM
root / root
0555
options
--
25 Jan 2024 4.43 PM
root / root
0555
readconcern
--
25 Jan 2024 4.43 PM
root / root
0555
readpref
--
25 Jan 2024 4.43 PM
root / root
0555
writeconcern
--
25 Jan 2024 4.43 PM
root / root
0555
batch_cursor.go
1.56 KB
25 Jan 2024 4.43 PM
root / root
0444
bson_helpers_test.go
2.208 KB
25 Jan 2024 4.43 PM
root / root
0444
bulk_write.go
16.054 KB
25 Jan 2024 4.43 PM
root / root
0444
bulk_write_models.go
12.305 KB
25 Jan 2024 4.43 PM
root / root
0444
change_stream.go
22.284 KB
25 Jan 2024 4.43 PM
root / root
0444
change_stream_deployment.go
1.351 KB
25 Jan 2024 4.43 PM
root / root
0444
change_stream_test.go
0.857 KB
25 Jan 2024 4.43 PM
root / root
0444
client.go
28.838 KB
25 Jan 2024 4.43 PM
root / root
0444
client_encryption.go
12.148 KB
25 Jan 2024 4.43 PM
root / root
0444
client_examples_test.go
15.06 KB
25 Jan 2024 4.43 PM
root / root
0444
client_side_encryption_examples_test.go
11.118 KB
25 Jan 2024 4.43 PM
root / root
0444
client_test.go
18.347 KB
25 Jan 2024 4.43 PM
root / root
0444
collection.go
58.865 KB
25 Jan 2024 4.43 PM
root / root
0444
collection_test.go
8.775 KB
25 Jan 2024 4.43 PM
root / root
0444
crud_examples_test.go
30.029 KB
25 Jan 2024 4.43 PM
root / root
0444
crypt_retrievers.go
1.786 KB
25 Jan 2024 4.43 PM
root / root
0444
cursor.go
10.557 KB
25 Jan 2024 4.43 PM
root / root
0444
cursor_test.go
7.092 KB
25 Jan 2024 4.43 PM
root / root
0444
database.go
27.19 KB
25 Jan 2024 4.43 PM
root / root
0444
database_test.go
4.198 KB
25 Jan 2024 4.43 PM
root / root
0444
doc.go
5.937 KB
25 Jan 2024 4.43 PM
root / root
0444
errors.go
19.745 KB
25 Jan 2024 4.43 PM
root / root
0444
errors_test.go
1.866 KB
25 Jan 2024 4.43 PM
root / root
0444
index_options_builder.go
6.528 KB
25 Jan 2024 4.43 PM
root / root
0444
index_view.go
15.269 KB
25 Jan 2024 4.43 PM
root / root
0444
main_test.go
1.496 KB
25 Jan 2024 4.43 PM
root / root
0444
mongo.go
12.521 KB
25 Jan 2024 4.43 PM
root / root
0444
mongo_test.go
12.931 KB
25 Jan 2024 4.43 PM
root / root
0444
mongocryptd.go
5.07 KB
25 Jan 2024 4.43 PM
root / root
0444
ocsp_test.go
2.348 KB
25 Jan 2024 4.43 PM
root / root
0444
read_write_concern_spec_test.go
8.521 KB
25 Jan 2024 4.43 PM
root / root
0444
results.go
9.236 KB
25 Jan 2024 4.43 PM
root / root
0444
results_test.go
2.571 KB
25 Jan 2024 4.43 PM
root / root
0444
session.go
13.816 KB
25 Jan 2024 4.43 PM
root / root
0444
single_result.go
3.84 KB
25 Jan 2024 4.43 PM
root / root
0444
single_result_test.go
4.047 KB
25 Jan 2024 4.43 PM
root / root
0444
util.go
0.277 KB
25 Jan 2024 4.43 PM
root / root
0444
with_transactions_test.go
23.404 KB
25 Jan 2024 4.43 PM
root / root
0444

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF