Esempio n. 1
0
        public async Task IndexAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var movies          = MovieMock.GetMovieMocks();
            var expectedIndices = new List <string>
            {
                "_id",
                "CategoryIndex"
            };

            await this.movieCollection.AddRangeAsync(movies).ConfigureAwait(false);

            var data = await this.movieCollection.GetAllAsync(1).ConfigureAwait(false);

            var indexManager = this.movieCollection.Collection.Indexes;

            var indices = indexManager.List();

            while (indices.MoveNext())
            {
                var currentIndex = indices.Current;
                foreach (var index in currentIndex)
                {
                    Assert.Contains(index.Elements, c =>
                    {
                        if (index.TryGetValue("name", out var name))
                        {
                            return(expectedIndices.Any(x => name.ToString().Contains(x)));
                        }

                        return(false);
                    });
                }
            }
        }
Esempio n. 2
0
        public async Task MapReduceAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var movies = MovieMock.GetMovieMocks();

            var expected = new List <ReduceResult <MovieProjection> >
            {
                new ReduceResult <MovieProjection>()
                {
                    Id    = "Horror",
                    value = new MovieProjection
                    {
                        Count        = 2,
                        TotalMinutes = 463,
                        Average      = 231.5
                    }
                },
                new ReduceResult <MovieProjection>()
                {
                    Id    = "SciFi",
                    value = new MovieProjection
                    {
                        Count        = 1,
                        TotalMinutes = 118,
                        Average      = 118
                    }
                }
            };

            var data = await this.movieCollection.GetAllAsync(1).ConfigureAwait(false);

            await this.movieCollection.DeleteManyByIdAsync(data).ConfigureAwait(false);

            await this.movieCollection.AddRangeAsync(movies).ConfigureAwait(false);

            BsonJavaScript map = @"
                function() {
                    emit(this.Category, { Count: 1, TotalMinutes: this.Minutes });
                }";

            BsonJavaScript reduce = @"
                function(key, values) {
                    var result = {Count: 0, TotalMinutes: 0 };

                    values.forEach(function(value){               
                        result.Count += value.Count;
                        result.TotalMinutes += value.TotalMinutes;
                    });

                    return result;
                }";

            BsonJavaScript finalice = @"
                function(key, value){
                  value.Average = value.TotalMinutes / value.Count;
                  return value;
                }";

            var options = new MapReduceOptions <Movie, ReduceResult <MovieProjection> >
            {
                Finalize      = finalice,
                OutputOptions = MapReduceOutputOptions.Inline
            };

            var statistics = await this.movieCollection.MapReduceAsync(map, reduce, options).ConfigureAwait(false);

            foreach (var item in statistics)
            {
                var expectedMovie = expected.FirstOrDefault(c => c.Id == item.Id);
                if (expectedMovie != null)
                {
                    Assert.True(expectedMovie.value.Count <= item.value.Count);
                    Assert.True(expectedMovie.value.TotalMinutes <= item.value.TotalMinutes);
                    Assert.True(expectedMovie.value.Average <= item.value.Average);
                }
            }
        }