Пример #1
0
        public void GraphLookup_with_restrictSearchWithMatch_should_return_expected_result()
        {
            RequireServer.Check();
            EnsureTestData();
            var subject          = __employeesCollection.Aggregate();
            var connectFromField = (FieldDefinition <Employee, string>) "ReportsTo";
            var connectToField   = (FieldDefinition <Employee, string>) "Name";
            var startWith        = (AggregateExpressionDefinition <Employee, string>) "$reportsTo";
            var @as     = (FieldDefinition <EmployeeWithReportingHierarchy, Employee[]>) "ReportingHierarchy";
            var options = new AggregateGraphLookupOptions <Employee, Employee, EmployeeWithReportingHierarchy>
            {
                RestrictSearchWithMatch = Builders <Employee> .Filter.Ne("Id", 1)
            };

            var result = subject
                         .GraphLookup(__employeesCollection, connectFromField, connectToField, startWith, @as, options)
                         .ToList();

            var comparer = new EmployeeWithReportingHierarchyEqualityComparer();

            result.WithComparer(comparer).Should().Equal(
                new EmployeeWithReportingHierarchy(__dev, new Employee[0]),
                new EmployeeWithReportingHierarchy(__eliot, new Employee[0]),
                new EmployeeWithReportingHierarchy(__ron, new[] { __eliot }),
                new EmployeeWithReportingHierarchy(__andrew, new[] { __eliot }),
                new EmployeeWithReportingHierarchy(__asya, new[] { __eliot, __ron }),
                new EmployeeWithReportingHierarchy(__dan, new[] { __eliot, __andrew }));
        }
        public void GraphLookup_should_add_expected_stage()
        {
            var subject          = __travelersCollection.Aggregate();
            var connectFromField = (FieldDefinition <Airport, string[]>) "Connects";
            var connectToField   = (FieldDefinition <Airport, string>) "Name";
            var startWith        = (AggregateExpressionDefinition <Traveler, string>) "$nearestAirport";
            var @as        = (FieldDefinition <TravelerDestinations, Destination[]>) "Destinations";
            var depthField = (FieldDefinition <Destination, int>) "NumConnections";
            var options    = new AggregateGraphLookupOptions <Airport, Destination, TravelerDestinations>
            {
                MaxDepth = 2
            };

            var result = subject.GraphLookup(__airportsCollection, connectFromField, connectToField, startWith, @as, depthField, options);

            var stage         = result.Stages.Single();
            var renderedStage = stage.Render(__travelersCollection.DocumentSerializer, BsonSerializer.SerializerRegistry);

            renderedStage.Document.Should().Be(
                @"{
                    $graphLookup : {
                        from : 'airports',
                        connectFromField : 'connects',
                        connectToField : 'airport',
                        startWith : '$nearestAirport',
                        as : 'destinations',
                        depthField : 'numConnections',
                        maxDepth : 2
                    }
                }");
        }
Пример #3
0
        public void GraphLookup_with_restrictSearchWithMatch_should_add_expected_stage()
        {
            var subject          = __employeesCollection.Aggregate();
            var connectFromField = (FieldDefinition <Employee, string>) "ReportsTo";
            var connectToField   = (FieldDefinition <Employee, string>) "Name";
            var startWith        = (AggregateExpressionDefinition <Employee, string>) "$reportsTo";
            var @as     = (FieldDefinition <EmployeeWithReportingHierarchy, Employee[]>) "ReportingHierarchy";
            var options = new AggregateGraphLookupOptions <Employee, Employee, EmployeeWithReportingHierarchy>
            {
                RestrictSearchWithMatch = Builders <Employee> .Filter.Ne("Id", 1)
            };

            var result = subject.GraphLookup(__employeesCollection, connectFromField, connectToField, startWith, @as, options);

            var stage         = result.Stages.Single();
            var renderedStage = stage.Render(__employeesCollection.DocumentSerializer, BsonSerializer.SerializerRegistry);

            renderedStage.Document.Should().Be(
                @"{
                    $graphLookup : {
                        from : 'employees',
                        connectFromField : 'reportsTo',
                        connectToField : 'name',
                        startWith : '$reportsTo',
                        as : 'reportingHierarchy',
                        restrictSearchWithMatch : { _id : { $ne : 1 } }
                    }
                }");
        }
        public void GraphLookup_typed_with_array_valued_start_with_should_return_expected_result()
        {
            RequireServer.Check().Supports(Feature.AggregateGraphLookupStage);
            EnsureTestData();
            var subject = __airportsCollection.Aggregate();
            var options = new AggregateGraphLookupOptions <Airport, Airport, AirportDestinationsIncludingStops>
            {
                MaxDepth = 2
            };

            var result = subject
                         .GraphLookup(
                from: __airportsCollection,
                connectFromField: x => x.Connects,
                connectToField: x => x.Name,
                startWith: x => x.Connects,
                @as: (AirportDestinationsIncludingStops x) => x.Destinations,
                options: options)
                         .ToList();

            var comparer = new AirportDestinationsIncludingStopsEqualityComparer();

            result.WithComparer(comparer).Should().Equal(
                new AirportDestinationsIncludingStops(__jfk, new[] { __jfk, __ord, __bos, __pwm, __lhr }),
                new AirportDestinationsIncludingStops(__bos, new[] { __bos, __ord, __jfk, __pwm, __lhr }),
                new AirportDestinationsIncludingStops(__ord, new[] { __ord, __jfk, __bos, __pwm }),
                new AirportDestinationsIncludingStops(__pwm, new[] { __pwm, __ord, __jfk, __bos, __lhr }),
                new AirportDestinationsIncludingStops(__lhr, new[] { __lhr, __jfk, __bos, __pwm }));
        }
        public void GraphLookup_typed_with_array_valued_start_with_should_add_expected_stage()
        {
            var subject = __airportsCollection.Aggregate();
            var options = new AggregateGraphLookupOptions <Airport, Airport, AirportDestinationsIncludingStops>
            {
                MaxDepth = 2
            };

            var result = subject.GraphLookup(
                from: __airportsCollection,
                connectFromField: x => x.Connects,
                connectToField: x => x.Name,
                startWith: x => x.Connects,
                @as: (AirportDestinationsIncludingStops x) => x.Destinations,
                options: options);

            var stage         = result.Stages.Single();
            var renderedStage = stage.Render(__airportsCollection.DocumentSerializer, BsonSerializer.SerializerRegistry);

            renderedStage.Document.Should().Be(
                @"{
                    $graphLookup : {
                        from : 'airports',
                        connectFromField : 'connects',
                        connectToField : 'airport',
                        startWith : '$connects',
                        as : 'destinations',
                        maxDepth : 2
                    }
                }");
        }
        public void GraphLookup_typed_should_add_expected_stage()
        {
            var subject = __travelersCollection.Aggregate();
            var options = new AggregateGraphLookupOptions <Airport, Destination, TravelerDestinations>
            {
                MaxDepth = 2
            };

            var result = subject.GraphLookup(
                from: __airportsCollection,
                connectFromField: x => x.Connects,
                connectToField: x => x.Name,
                startWith: x => x.NearestAirport,
                @as: (TravelerDestinations x) => x.Destinations,
                depthField: (Destination x) => x.NumConnections,
                options: options);

            var stage         = result.Stages.Single();
            var renderedStage = stage.Render(__travelersCollection.DocumentSerializer, BsonSerializer.SerializerRegistry);

            renderedStage.Document.Should().Be(
                @"{
                    $graphLookup : {
                        from : 'airports',
                        connectFromField : 'connects',
                        connectToField : 'airport',
                        startWith : '$nearestAirport',
                        as : 'destinations',
                        depthField : 'numConnections',
                        maxDepth : 2
                    }
                }");
        }
        public void GraphLookup_typed_should_return_expected_result()
        {
            RequireServer.Check().Supports(Feature.AggregateGraphLookupStage);
            EnsureTestData();
            var subject          = __travelersCollection.Aggregate();
            var connectFromField = (FieldDefinition <Airport, string[]>) "Connects";
            var connectToField   = (FieldDefinition <Airport, string>) "Name";
            var startWith        = (AggregateExpressionDefinition <Traveler, string>) "$nearestAirport";
            var @as        = (FieldDefinition <TravelerDestinations, Destination[]>) "Destinations";
            var depthField = (FieldDefinition <Destination, int>) "NumConnections";
            var options    = new AggregateGraphLookupOptions <Airport, Destination, TravelerDestinations>
            {
                MaxDepth = 2
            };

            var result = subject
                         .GraphLookup(__airportsCollection, connectFromField, connectToField, startWith, @as, depthField, options)
                         .ToList();

            var comparer = new TravelerDestinationsEqualityComparer();

            result.WithComparer(comparer).Should().Equal(
                new TravelerDestinations(__dev, new[]
            {
                new Destination(__pwm, 2),
                new Destination(__ord, 1),
                new Destination(__bos, 1),
                new Destination(__jfk, 0)
            }),
                new TravelerDestinations(__eliot, new[]
            {
                new Destination(__pwm, 2),
                new Destination(__ord, 1),
                new Destination(__bos, 1),
                new Destination(__jfk, 0)
            }),
                new TravelerDestinations(__jeff, new[]
            {
                new Destination(__ord, 2),
                new Destination(__pwm, 1),
                new Destination(__lhr, 2),
                new Destination(__jfk, 1),
                new Destination(__bos, 0)
            }));
        }
Пример #8
0
 public IAggregateFluent <TNewResult> GraphLookup <TFrom, TConnectFrom, TConnectTo, TStartWith, TAsElement, TAs, TNewResult>(IMongoCollection <TFrom> from, FieldDefinition <TFrom, TConnectFrom> connectFromField, FieldDefinition <TFrom, TConnectTo> connectToField, AggregateExpressionDefinition <BsonDocument, TStartWith> startWith, FieldDefinition <TNewResult, TAs> @as, FieldDefinition <TAsElement, int> depthField, AggregateGraphLookupOptions <TFrom, TAsElement, TNewResult> options = null) where TAs : IEnumerable <TAsElement>
 {
     return(BlockProvider.GraphLookup <TFrom, TConnectFrom, TConnectTo, TStartWith, TAsElement, TAs, TNewResult>(from, connectFromField, connectToField, startWith, @as, depthField, options));
 }