|
| 1 | +package qmgo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.mongodb.org/mongo-driver/bson" |
| 10 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAggregate(t *testing.T) { |
| 14 | + ast := require.New(t) |
| 15 | + |
| 16 | + var cli *QmgoClient |
| 17 | + |
| 18 | + cli = initClient("test") |
| 19 | + cli.DropCollection(context.Background()) |
| 20 | + cli.EnsureIndexes(context.Background(), nil, []string{"name"}) |
| 21 | + |
| 22 | + id1 := primitive.NewObjectID() |
| 23 | + id2 := primitive.NewObjectID() |
| 24 | + id3 := primitive.NewObjectID() |
| 25 | + id4 := primitive.NewObjectID() |
| 26 | + id5 := primitive.NewObjectID() |
| 27 | + docs := []interface{}{ |
| 28 | + QueryTestItem{Id: id1, Name: "Alice", Age: 10}, |
| 29 | + QueryTestItem{Id: id2, Name: "Alice", Age: 12}, |
| 30 | + QueryTestItem{Id: id3, Name: "Lucas", Age: 33}, |
| 31 | + QueryTestItem{Id: id4, Name: "Lucas", Age: 22}, |
| 32 | + QueryTestItem{Id: id5, Name: "Lucas", Age: 44}, |
| 33 | + } |
| 34 | + cli.InsertMany(context.Background(), docs) |
| 35 | + matchStage := bson.D{{"$match", []bson.E{{"age", bson.D{{"$gt", 11}}}}}} |
| 36 | + groupStage := bson.D{{"$group", bson.D{{"_id", "$name"}, {"total", bson.D{{"$sum", "$age"}}}}}} |
| 37 | + var showsWithInfo []bson.M |
| 38 | + // aggregate ALL() |
| 39 | + err := cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}).All(&showsWithInfo) |
| 40 | + ast.NoError(err) |
| 41 | + ast.Equal(2, len(showsWithInfo)) |
| 42 | + for _, v := range showsWithInfo { |
| 43 | + if "Alice" == v["_id"] { |
| 44 | + ast.Equal(int32(12), v["total"]) |
| 45 | + continue |
| 46 | + } |
| 47 | + if "Lucas" == v["_id"] { |
| 48 | + ast.Equal(int32(99), v["total"]) |
| 49 | + continue |
| 50 | + } |
| 51 | + ast.Error(errors.New("error"), "impossible") |
| 52 | + } |
| 53 | + // Iter() |
| 54 | + iter := cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}) |
| 55 | + ast.NotNil(iter) |
| 56 | + err = iter.All(&showsWithInfo) |
| 57 | + ast.NoError(err) |
| 58 | + for _, v := range showsWithInfo { |
| 59 | + if "Alice" == v["_id"] { |
| 60 | + ast.Equal(int32(12), v["total"]) |
| 61 | + continue |
| 62 | + } |
| 63 | + if "Lucas" == v["_id"] { |
| 64 | + ast.Equal(int32(99), v["total"]) |
| 65 | + continue |
| 66 | + } |
| 67 | + ast.Error(errors.New("error"), "impossible") |
| 68 | + } |
| 69 | + // One() |
| 70 | + var oneInfo bson.M |
| 71 | + |
| 72 | + iter = cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}) |
| 73 | + ast.NotNil(iter) |
| 74 | + |
| 75 | + err = iter.One(&oneInfo) |
| 76 | + ast.NoError(err) |
| 77 | + ast.Equal(true, oneInfo["_id"] == "Alice" || oneInfo["_id"] == "Lucas") |
| 78 | + |
| 79 | + // iter |
| 80 | + iter = cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}) |
| 81 | + ast.NotNil(iter) |
| 82 | + |
| 83 | + i := iter.Iter() |
| 84 | + |
| 85 | + ct := i.Next(&oneInfo) |
| 86 | + ast.Equal(true, oneInfo["_id"] == "Alice" || oneInfo["_id"] == "Lucas") |
| 87 | + ast.Equal(true, ct) |
| 88 | + ct = i.Next(&oneInfo) |
| 89 | + ast.Equal(true, oneInfo["_id"] == "Alice" || oneInfo["_id"] == "Lucas") |
| 90 | + ast.Equal(true, ct) |
| 91 | + ct = i.Next(&oneInfo) |
| 92 | + ast.Equal(false, ct) |
| 93 | + |
| 94 | + // err |
| 95 | + ast.Error(cli.Aggregate(context.Background(), 1).All(&showsWithInfo)) |
| 96 | + ast.Error(cli.Aggregate(context.Background(), 1).One(&showsWithInfo)) |
| 97 | + ast.Error(cli.Aggregate(context.Background(), 1).Iter().Err()) |
| 98 | + matchStage = bson.D{{"$match", []bson.E{{"age", bson.D{{"$gt", 100}}}}}} |
| 99 | + groupStage = bson.D{{"$group", bson.D{{"_id", "$name"}, {"total", bson.D{{"$sum", "$age"}}}}}} |
| 100 | + ast.Error(cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}).One(&showsWithInfo)) |
| 101 | + |
| 102 | +} |
0 commit comments