|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/mongodb/mongo-go-driver/internal/testutil/helpers" |
| 5 | + "github.com/mongodb/mongo-go-driver/mongo/writeconcern" |
| 6 | + "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 7 | + "github.com/mongodb/mongo-go-driver/x/network/description" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAggregate(t *testing.T) { |
| 12 | + wc := writeconcern.New(writeconcern.W(10)) |
| 13 | + legacyDesc := description.SelectedServer{ |
| 14 | + Server: description.Server{ |
| 15 | + WireVersion: &description.VersionRange{ |
| 16 | + Max: 4, |
| 17 | + }, |
| 18 | + }, |
| 19 | + } |
| 20 | + desc := description.SelectedServer{ |
| 21 | + Server: description.Server{ |
| 22 | + WireVersion: &description.VersionRange{ |
| 23 | + Max: 5, |
| 24 | + }, |
| 25 | + }, |
| 26 | + } |
| 27 | + outDoc := bsonx.Doc{{"$out", bsonx.Int32(1)}} |
| 28 | + outPipeline := bsonx.Arr{bsonx.Document(outDoc)} |
| 29 | + |
| 30 | + testCases := []struct { |
| 31 | + name string |
| 32 | + desc description.SelectedServer |
| 33 | + pipeline bsonx.Arr |
| 34 | + wcExpected bool |
| 35 | + }{ |
| 36 | + {"LegacyDescNoOut", legacyDesc, bsonx.Arr{}, false}, |
| 37 | + {"LegacyDescOut", legacyDesc, outPipeline, false}, |
| 38 | + {"NewDescNoOut", desc, bsonx.Arr{}, false}, |
| 39 | + {"NewDescOut", desc, outPipeline, true}, |
| 40 | + } |
| 41 | + |
| 42 | + for _, tc := range testCases { |
| 43 | + t.Run(tc.name, func(t *testing.T) { |
| 44 | + cmd := Aggregate{ |
| 45 | + NS: Namespace{ |
| 46 | + DB: "db", |
| 47 | + Collection: "coll", |
| 48 | + }, |
| 49 | + Pipeline: tc.pipeline, |
| 50 | + WriteConcern: wc, |
| 51 | + } |
| 52 | + |
| 53 | + readCmd, err := cmd.encode(tc.desc) |
| 54 | + testhelpers.RequireNil(t, err, "error encoding: %s", err) |
| 55 | + |
| 56 | + _, err = readCmd.Command.LookupErr("writeConcern") |
| 57 | + nilErr := err == nil |
| 58 | + // err should be nil if wc was expected |
| 59 | + if nilErr != tc.wcExpected { |
| 60 | + t.Fatalf("write concern mismatch: expected %v got %v", tc.wcExpected, nilErr) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
0 commit comments