@@ -654,3 +654,77 @@ func TestStageBuilder_SortByCount(t *testing.T) {
654654func TestStageBuilder_Count (t * testing.T ) {
655655 assert .Equal (t , mongo.Pipeline {bson.D {bson.E {Key : "$count" , Value : "passing_scores" }}}, StageBsonBuilder ().Count ("passing_scores" ).Build ())
656656}
657+
658+ func TestStageBuilder_Lookup (t * testing.T ) {
659+ testCases := []struct {
660+ name string
661+ from string
662+ as string
663+ opt * LookUpOptions
664+
665+ want mongo.Pipeline
666+ }{
667+ {
668+ name : "basic" ,
669+ from : "orders" ,
670+ opt : & LookUpOptions {
671+ LocalField : "_id" ,
672+ ForeignField : "userId" ,
673+ Let : nil ,
674+ Pipeline : nil ,
675+ },
676+ as : "userOrders" ,
677+ want : mongo.Pipeline {
678+ {
679+ bson.E {Key : "$lookup" , Value : bson.D {
680+ bson.E {Key : "from" , Value : "orders" },
681+ bson.E {Key : "localField" , Value : "_id" },
682+ bson.E {Key : "foreignField" , Value : "userId" },
683+ bson.E {Key : "as" , Value : "userOrders" },
684+ }},
685+ },
686+ },
687+ },
688+ {
689+ name : "advanced case" ,
690+ from : "orders" ,
691+ opt : & LookUpOptions {
692+ LocalField : "" ,
693+ ForeignField : "" ,
694+ Let : bson.D {bson.E {Key : "userId" , Value : "$_id" }},
695+ Pipeline : mongo.Pipeline {
696+ {
697+ bson.E {Key : "$match" , Value : bson.D {bson.E {Key : "$expr" , Value : bson.D {bson.E {Key : "$and" , Value : []any {
698+ bson.D {bson.E {Key : "$eq" , Value : []any {"$userId" , "$$userId" }}},
699+ bson.D {bson.E {Key : "$gt" , Value : []any {"$totalAmount" , 100 }}},
700+ }}}}}},
701+ },
702+ },
703+ },
704+ as : "largeOrders" ,
705+ want : mongo.Pipeline {
706+ {
707+ bson.E {Key : "$lookup" , Value : bson.D {
708+ bson.E {Key : "from" , Value : "orders" },
709+ bson.E {Key : "let" , Value : bson.D {bson.E {Key : "userId" , Value : "$_id" }}},
710+ bson.E {Key : "pipeline" , Value : mongo.Pipeline {
711+ {
712+ bson.E {Key : "$match" , Value : bson.D {bson.E {Key : "$expr" , Value : bson.D {bson.E {Key : "$and" , Value : []any {
713+ bson.D {bson.E {Key : "$eq" , Value : []any {"$userId" , "$$userId" }}},
714+ bson.D {bson.E {Key : "$gt" , Value : []any {"$totalAmount" , 100 }}},
715+ }}}}}},
716+ },
717+ }},
718+ bson.E {Key : "as" , Value : "largeOrders" },
719+ },
720+ },
721+ },
722+ },
723+ },
724+ }
725+ for _ , tc := range testCases {
726+ t .Run (tc .name , func (t * testing.T ) {
727+ assert .Equal (t , tc .want , StageBsonBuilder ().Lookup (tc .from , tc .as , tc .opt ).Build ())
728+ })
729+ }
730+ }
0 commit comments