|
| 1 | +// Copyright (C) MongoDB, Inc. 2024-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package mongo |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "go.mongodb.org/mongo-driver/bson" |
| 13 | + "go.mongodb.org/mongo-driver/internal/assert" |
| 14 | + "go.mongodb.org/mongo-driver/internal/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestBatches(t *testing.T) { |
| 18 | + t.Run("test Addvancing", func(t *testing.T) { |
| 19 | + batches := &modelBatches{ |
| 20 | + models: make([]clientWriteModel, 2), |
| 21 | + } |
| 22 | + batches.AdvanceBatches(3) |
| 23 | + size := batches.Size() |
| 24 | + assert.Equal(t, 0, size, "expected: %d, got: %d", 1, size) |
| 25 | + }) |
| 26 | + t.Run("test appendBatches", func(t *testing.T) { |
| 27 | + client, err := NewClient() |
| 28 | + require.NoError(t, err, "NewClient error: %v", err) |
| 29 | + batches := &modelBatches{ |
| 30 | + client: client, |
| 31 | + models: []clientWriteModel{ |
| 32 | + {"ns0", nil}, |
| 33 | + {"ns1", &ClientInsertOneModel{ |
| 34 | + Document: bson.D{{"foo", 42}}, |
| 35 | + }}, |
| 36 | + {"ns2", &ClientReplaceOneModel{ |
| 37 | + Filter: bson.D{{"foo", "bar"}}, |
| 38 | + Replacement: bson.D{{"foo", "baz"}}, |
| 39 | + }}, |
| 40 | + {"ns1", &ClientDeleteOneModel{ |
| 41 | + Filter: bson.D{{"qux", "quux"}}, |
| 42 | + }}, |
| 43 | + }, |
| 44 | + offset: 1, |
| 45 | + result: &ClientBulkWriteResult{}, |
| 46 | + } |
| 47 | + var n int |
| 48 | + n, _, err = batches.AppendBatchSequence(nil, 4, 16_000, 16_000) |
| 49 | + require.NoError(t, err, "AppendBatchSequence error: %v", err) |
| 50 | + assert.Equal(t, 3, n, "expected %d appendings, got: %d", 3, n) |
| 51 | + |
| 52 | + _ = batches.cursorHandlers[0](&cursorInfo{Ok: true, Idx: 0}, nil) |
| 53 | + _ = batches.cursorHandlers[1](&cursorInfo{Ok: true, Idx: 1}, nil) |
| 54 | + _ = batches.cursorHandlers[2](&cursorInfo{Ok: true, Idx: 2}, nil) |
| 55 | + |
| 56 | + ins, ok := batches.result.InsertResults[1] |
| 57 | + assert.True(t, ok, "expected an insert results") |
| 58 | + assert.NotNil(t, ins.InsertedID, "expected an ID") |
| 59 | + |
| 60 | + _, ok = batches.result.UpdateResults[2] |
| 61 | + assert.True(t, ok, "expected an insert results") |
| 62 | + |
| 63 | + _, ok = batches.result.DeleteResults[3] |
| 64 | + assert.True(t, ok, "expected an insert results") |
| 65 | + }) |
| 66 | +} |
0 commit comments