Example #1
0
        public async Task FirstOrDefaultAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var tweets = new List <Tweet>
            {
                new Tweet
                {
                    Message = "Message1"
                },
                new Tweet
                {
                    Message = "Message2"
                }
            };

            foreach (var tweet in tweets)
            {
                var added = await this.tweetCollection.AddAsync(tweet).ConfigureAwait(false);

                Assert.NotNull(added);
                Assert.True(added.Id != Guid.Empty);
            }

            Assert.NotNull(await this.tweetCollection.FirstOrDefaultAsync(c => c.Message == "Message1").ConfigureAwait(false));
        }
Example #2
0
        public async Task GetAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var tweets = new List <Tweet>
            {
                new Tweet
                {
                    Message = "Message1"
                },
                new Tweet
                {
                    Message = "Message2"
                }
            };

            foreach (var tweet in tweets)
            {
                var added = await this.tweetCollection.AddAsync(tweet).ConfigureAwait(false);

                Assert.NotNull(added);
                Assert.True(added.Id != Guid.Empty);
            }

            var data = await this.tweetCollection.GetAsync(1, c => c.Message == "Message1").ConfigureAwait(false);

            Assert.NotEmpty(data);
            Assert.True(data.Count == 1);
        }
Example #3
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);
                    });
                }
            }
        }
Example #4
0
        public async Task AsQueryablePaging(IoCType ioCType)
        {
            this.SetTestContext(ioCType);

            var tweets = Enumerable.Range(0, 100).Select(i => new Tweet
            {
                Message = $"Message{i}"
            });

            await this.tweetCollection.AddRangeAsync(tweets);

            var page     = 2;
            var pageSize = 5;
            var list     = this.tweetCollection.AsQueryable()
                           .Where(c => c.Message.StartsWith("Message5"))
                           .Skip((page - 1) * pageSize)
                           .Take(5)
                           .ToList();

            var tweet = list.FirstOrDefault();

            Assert.NotNull(tweet);

            Assert.Equal("Message54", tweet.Message);
        }
Example #5
0
        public async Task GetAllAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var tweets = new List <Tweet>
            {
                new Tweet
                {
                    Message = "Hi!!!"
                },
                new Tweet
                {
                    Message = "Hi!!!"
                }
            };

            foreach (var tweet in tweets)
            {
                var added = await this.tweetCollection.AddAsync(tweet).ConfigureAwait(false);

                Assert.NotNull(added);
                Assert.True(added.Id != Guid.Empty);
            }

            var query = await this.tweetCollection.GetAllAsync(1).ConfigureAwait(false);

            var data = query.ToList();

            Assert.NotEmpty(data);
        }
        public async Task GetAllFiles(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "image.jpg");

            byte[] fileBytes = default(byte[]);

#if NET461
            fileBytes = File.ReadAllBytes(filePath);
#else
            fileBytes = await File.ReadAllBytesAsync(filePath);
#endif

            var file = new ImageBlob
            {
                FileName = "image.jpg",
                Data     = fileBytes
            };

            var fileSaved = await this.imageBlobCollection.UploadAsync(file);

            Assert.True(fileSaved != ObjectId.Empty);

            var files = await this.imageBlobCollection.GetFilesAllAsync();

            Assert.NotNull(files);
            Assert.True(files.Any());
        }
Example #7
0
        public async Task CleanAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);

            var dbName = this.tweetCollection.Database.DatabaseNamespace.DatabaseName;

            await this.context.MongoClient.DropDatabaseAsync(dbName);
        }
Example #8
0
        public async Task AddAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);

            var tweet = new Tweet
            {
                Message = "Hi!!!"
            };

            var added = await this.tweetCollection.AddAsync(tweet).ConfigureAwait(false);

            Assert.NotNull(added);
            Assert.True(added.Id != Guid.Empty);
        }
        public T Resolve <T>(IoCType ioCType)
        {
            T obj = default(T);

            switch (ioCType)
            {
            case IoCType.MicrosoftExtensionsDependencyInjection:
                return((T)instance.IoCProviders.Item1.GetService(typeof(T)));

            case IoCType.Autofac:
                return(instance.IoCProviders.Item3.Resolve <T>());

            case IoCType.CastleWindsor:
                return(instance.IoCProviders.Item2.Resolve <T>());
            }
            return(obj);
        }
Example #10
0
        public async Task ShouldCommitOperationsAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var id = Guid.NewGuid();

            var tweet = new Tweet
            {
                Id      = id,
                Message = "Message"
            };

            using (var session = await this.tweetCollection.BeginSessionAsync())
            {
                try
                {
                    session.StartTransaction();

                    await this.tweetCollection.AddAsync(tweet).ConfigureAwait(false);

                    var findTweet = await this.tweetCollection.FindAsync(id).ConfigureAwait(false);

                    findTweet.Should().BeNull();

                    var updatedMessage = "UpdatedMessage";
                    tweet.Message = updatedMessage;

                    await this.tweetCollection.UpdateOneAsync(c => c.Id == tweet.Id, c => c.Set(p => p.Message, updatedMessage)).ConfigureAwait(false);

                    findTweet = await this.tweetCollection.FindAsync(id).ConfigureAwait(false);

                    findTweet.Should().BeNull();

                    await session.CommitTransactionAsync().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await session.AbortTransactionAsync().ConfigureAwait(false);

                    throw;
                }
            }

            tweet = await this.tweetCollection.FindAsync(id).ConfigureAwait(false);

            tweet.Should().NotBeNull();
        }
Example #11
0
        public async Task AsQueryableFilter(IoCType ioCType)
        {
            this.SetTestContext(ioCType);

            var tweets = Enumerable.Range(0, 100).Select(i => new Tweet
            {
                Message = $"Message{i}"
            });

            await this.tweetCollection.AddRangeAsync(tweets);

            var list = this.tweetCollection.AsQueryable()
                       .Where(c => c.Message == "Message50")
                       .ToList();

            var tweet = list.FirstOrDefault();

            Assert.NotNull(tweet);

            Assert.Equal("Message50", tweet.Message);
        }
Example #12
0
        public async Task UpdateAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var tweet = new Tweet
            {
                Message = "Hi!!!"
            };

            var added = await this.tweetCollection.AddAsync(tweet).ConfigureAwait(false);

            Assert.NotNull(added);
            Assert.True(added.Id != Guid.Empty);

            added.Message = "Hi All!!!";

            await this.tweetCollection.UpdateOneAsync(c => c.Id == added.Id, c => c.Set(x => x.Message, "Hi All!!!")).ConfigureAwait(false);

            var findUpdated = await this.tweetCollection.FindAsync(added.Id).ConfigureAwait(false);

            Assert.True(findUpdated.Id == added.Id);
            Assert.True(findUpdated.Message == "Hi All!!!");
        }
Example #13
0
        public async Task AsQueryableSort(IoCType ioCType)
        {
            this.SetTestContext(ioCType);

            var tweets = Enumerable.Range(0, 100).Select(i => new Tweet
            {
                Message = $"Message{i}"
            });

            await this.tweetCollection.AddRangeAsync(tweets);

            var list = this.tweetCollection.AsQueryable()
                       .Where(c => c.Message.StartsWith("Message5"))
                       .OrderByDescending(c => c.Message)
                       .ToList();

            var tweet = list.FirstOrDefault();

            Assert.NotNull(tweet);

            Assert.Equal("Message59", tweet.Message);
        }
Example #14
0
        public async Task AddRangeAsync(IoCType ioCType)
        {
            this.SetTestContext(ioCType);
            var tweets = new List <Tweet>
            {
                new Tweet
                {
                    Message = "Message1"
                },
                new Tweet
                {
                    Message = "Message2"
                }
            };

            await this.tweetCollection.AddRangeAsync(tweets).ConfigureAwait(false);

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

            Assert.NotEmpty(data);
            Assert.True(data.Count == tweets.Count);
        }
Example #15
0
 private void SetTestContext(IoCType ioCType)
 {
     this.context         = this.ioCResolver.Resolve <TContext>(ioCType);
     this.tweetCollection = this.context.Collection <Tweet>();
     this.movieCollection = this.context.Collection <Movie>();
 }
Example #16
0
 public async Task ShouldAddIndexAndRetrieveIt(IoCType ioCType)
 {
     await this.IndexAsync(ioCType).ConfigureAwait(false);
 }
Example #17
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);
                }
            }
        }
Example #18
0
 public async Task ShouldAddRangeItemsToDatabase(IoCType ioCType)
 {
     await this.AddRangeAsync(ioCType).ConfigureAwait(false);
 }
Example #19
0
 public async Task ShouldAsQueryableFilterToDatabase(IoCType ioCType)
 {
     await this.AsQueryableFilter(ioCType);
 }
Example #20
0
 public async Task ShouldAsQueryableSortToDatabase(IoCType ioCType)
 {
     await this.AsQueryableSort(ioCType);
 }
Example #21
0
 public async Task ShouldGetAllFiles(IoCType ioCType)
 {
     await this.GetAllFiles(ioCType).ConfigureAwait(false);
 }
Example #22
0
 public async Task ShouldMapReduceOperation(IoCType ioCType)
 {
     await this.MapReduceAsync(ioCType).ConfigureAwait(false);
 }
Example #23
0
 public async Task ShouldAsQueryablePagingToDatabase(IoCType ioCType)
 {
     await this.AsQueryablePaging(ioCType);
 }
Example #24
0
 public async Task ShouldDeleteItemFromDatabase(IoCType ioCType)
 {
     await this.DeleteAsync(ioCType).ConfigureAwait(false);
 }
Example #25
0
 public async Task ShouldGetItemsFromDatabase(IoCType ioCType)
 {
     await this.GetAsync(ioCType).ConfigureAwait(false);
 }
Example #26
0
 public async Task ShouldUploadAndDeleteFile(IoCType ioCType)
 {
     await this.UploadAndDeleteFile(ioCType).ConfigureAwait(false);
 }
Example #27
0
 public async Task ShouldRenameFile(IoCType ioCType)
 {
     await this.RenameFile(ioCType).ConfigureAwait(false);
 }
Example #28
0
 public async Task ShouldGetByFileName(IoCType ioCType)
 {
     await this.GetByFileName(ioCType).ConfigureAwait(false);
 }
Example #29
0
 public async Task ShouldUploadAndGetFileById(IoCType ioCType)
 {
     await this.UploadAndGetFileById(ioCType).ConfigureAwait(false);
 }
 private void SetTestContext(IoCType ioCType)
 {
     this.context             = this.ioCResolver.Resolve <TContext>(ioCType);
     this.imageBlobCollection = this.context.FileCollection <ImageBlob>();
 }