|
1 | 1 | package mongo
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "go.mongodb.org/mongo-driver/bson" |
| 9 | + "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" |
| 10 | + "go.mongodb.org/mongo-driver/x/mongo/driver/topology" |
| 11 | +) |
| 12 | + |
| 13 | +type testBatchCursor struct { |
| 14 | + batches []*bsoncore.DocumentSequence |
| 15 | + batch *bsoncore.DocumentSequence |
| 16 | +} |
| 17 | + |
| 18 | +func newTestBatchCursor(numBatches, batchSize int) *testBatchCursor { |
| 19 | + batches := make([]*bsoncore.DocumentSequence, 0, numBatches) |
| 20 | + |
| 21 | + counter := 0 |
| 22 | + for batch := 0; batch < numBatches; batch++ { |
| 23 | + var docSequence []byte |
| 24 | + |
| 25 | + for doc := 0; doc < batchSize; doc++ { |
| 26 | + var elem []byte |
| 27 | + elem = bsoncore.AppendInt32Element(elem, "foo", int32(counter)) |
| 28 | + counter++ |
| 29 | + |
| 30 | + var doc []byte |
| 31 | + doc = bsoncore.BuildDocumentFromElements(doc, elem) |
| 32 | + docSequence = append(docSequence, doc...) |
| 33 | + } |
| 34 | + |
| 35 | + batches = append(batches, &bsoncore.DocumentSequence{ |
| 36 | + Style: bsoncore.SequenceStyle, |
| 37 | + Data: docSequence, |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + return &testBatchCursor{ |
| 42 | + batches: batches, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (tbc *testBatchCursor) ID() int64 { |
| 47 | + if len(tbc.batches) == 0 { |
| 48 | + return 0 // cursor exhausted |
| 49 | + } |
| 50 | + |
| 51 | + return 10 |
| 52 | +} |
| 53 | + |
| 54 | +func (tbc *testBatchCursor) Next(context.Context) bool { |
| 55 | + if len(tbc.batches) == 0 { |
| 56 | + return false |
| 57 | + } |
| 58 | + |
| 59 | + tbc.batch = tbc.batches[0] |
| 60 | + tbc.batches = tbc.batches[1:] |
| 61 | + return true |
| 62 | +} |
| 63 | + |
| 64 | +func (tbc *testBatchCursor) Batch() *bsoncore.DocumentSequence { |
| 65 | + return tbc.batch |
| 66 | +} |
| 67 | + |
| 68 | +func (tbc *testBatchCursor) Server() *topology.Server { |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func (tbc *testBatchCursor) Err() error { |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (tbc *testBatchCursor) Close(context.Context) error { |
| 77 | + return nil |
| 78 | +} |
4 | 79 |
|
5 | 80 | func TestCursor(t *testing.T) {
|
6 | 81 | t.Run("loops until docs available", func(t *testing.T) {})
|
7 | 82 | t.Run("returns false on context cancellation", func(t *testing.T) {})
|
8 | 83 | t.Run("returns false if error occurred", func(t *testing.T) {})
|
9 | 84 | t.Run("returns false if ID is zero and no more docs", func(t *testing.T) {})
|
| 85 | + |
| 86 | + t.Run("TestAll", func(t *testing.T) { |
| 87 | + t.Run("errors if argument is not pointer to slice", func(t *testing.T) { |
| 88 | + cursor, err := newCursor(newTestBatchCursor(1, 5), nil) |
| 89 | + require.Nil(t, err) |
| 90 | + err = cursor.All(context.Background(), []bson.D{}) |
| 91 | + require.NotNil(t, err) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("fills slice with all documents", func(t *testing.T) { |
| 95 | + cursor, err := newCursor(newTestBatchCursor(1, 5), nil) |
| 96 | + require.Nil(t, err) |
| 97 | + |
| 98 | + var docs []bson.D |
| 99 | + err = cursor.All(context.Background(), &docs) |
| 100 | + require.Nil(t, err) |
| 101 | + require.Equal(t, 5, len(docs)) |
| 102 | + |
| 103 | + for index, doc := range docs { |
| 104 | + require.Equal(t, doc, bson.D{{"foo", int32(index)}}) |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("decodes each document into slice type", func(t *testing.T) { |
| 109 | + cursor, err := newCursor(newTestBatchCursor(1, 5), nil) |
| 110 | + require.Nil(t, err) |
| 111 | + |
| 112 | + type Document struct { |
| 113 | + Foo int32 `bson:"foo"` |
| 114 | + } |
| 115 | + var docs []Document |
| 116 | + err = cursor.All(context.Background(), &docs) |
| 117 | + require.Nil(t, err) |
| 118 | + require.Equal(t, 5, len(docs)) |
| 119 | + |
| 120 | + for index, doc := range docs { |
| 121 | + require.Equal(t, doc, Document{Foo: int32(index)}) |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + t.Run("multiple batches are included", func(t *testing.T) { |
| 126 | + cursor, err := newCursor(newTestBatchCursor(2, 5), nil) |
| 127 | + var docs []bson.D |
| 128 | + err = cursor.All(context.Background(), &docs) |
| 129 | + require.Nil(t, err) |
| 130 | + require.Equal(t, 10, len(docs)) |
| 131 | + |
| 132 | + for index, doc := range docs { |
| 133 | + require.Equal(t, doc, bson.D{{"foo", int32(index)}}) |
| 134 | + } |
| 135 | + }) |
| 136 | + }) |
10 | 137 | }
|
0 commit comments