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)); }
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); }
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); }); } } }
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); }
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()); }
public async Task CleanAsync(IoCType ioCType) { this.SetTestContext(ioCType); var dbName = this.tweetCollection.Database.DatabaseNamespace.DatabaseName; await this.context.MongoClient.DropDatabaseAsync(dbName); }
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); }
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(); }
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); }
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!!!"); }
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); }
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); }
private void SetTestContext(IoCType ioCType) { this.context = this.ioCResolver.Resolve <TContext>(ioCType); this.tweetCollection = this.context.Collection <Tweet>(); this.movieCollection = this.context.Collection <Movie>(); }
public async Task ShouldAddIndexAndRetrieveIt(IoCType ioCType) { await this.IndexAsync(ioCType).ConfigureAwait(false); }
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); } } }
public async Task ShouldAddRangeItemsToDatabase(IoCType ioCType) { await this.AddRangeAsync(ioCType).ConfigureAwait(false); }
public async Task ShouldAsQueryableFilterToDatabase(IoCType ioCType) { await this.AsQueryableFilter(ioCType); }
public async Task ShouldAsQueryableSortToDatabase(IoCType ioCType) { await this.AsQueryableSort(ioCType); }
public async Task ShouldGetAllFiles(IoCType ioCType) { await this.GetAllFiles(ioCType).ConfigureAwait(false); }
public async Task ShouldMapReduceOperation(IoCType ioCType) { await this.MapReduceAsync(ioCType).ConfigureAwait(false); }
public async Task ShouldAsQueryablePagingToDatabase(IoCType ioCType) { await this.AsQueryablePaging(ioCType); }
public async Task ShouldDeleteItemFromDatabase(IoCType ioCType) { await this.DeleteAsync(ioCType).ConfigureAwait(false); }
public async Task ShouldGetItemsFromDatabase(IoCType ioCType) { await this.GetAsync(ioCType).ConfigureAwait(false); }
public async Task ShouldUploadAndDeleteFile(IoCType ioCType) { await this.UploadAndDeleteFile(ioCType).ConfigureAwait(false); }
public async Task ShouldRenameFile(IoCType ioCType) { await this.RenameFile(ioCType).ConfigureAwait(false); }
public async Task ShouldGetByFileName(IoCType ioCType) { await this.GetByFileName(ioCType).ConfigureAwait(false); }
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>(); }