public async Task DateContext_RetrievesManyRecords() { const int DataSetLength = 500; var bookRev1 = await BooksHelper.CreateBookAsync(); await ParallelForAsync(1, DataSetLength, async i => { try { await BooksHelper.CreateBookAsync(bookRev1.Name, bookRev1.PublishYear + i); } catch (ProvisionedThroughputExceededException) { Thread.Sleep(10000); // trying one more time await BooksHelper.CreateBookAsync(bookRev1.Name, bookRev1.PublishYear + i); } }); var allBooks = Context.GetTable <Book>().Where(book => book.Name == bookRev1.Name).ToList(); Assert.AreEqual(DataSetLength, allBooks.Count); var query = from b in Context.GetTable <Book>() where b.Name == bookRev1.Name select b; allBooks = query.ToListAsync().Result; Assert.AreEqual(DataSetLength, allBooks.Count); }
public async Task DateContext_Query_ReturnsProjection() { var bookRev1 = await BooksHelper.CreateBookAsync( numPages : 123, lastRentTime : DateTime.Today, popularityRating : Book.Popularity.AboveAverage, filmsBasedOnBook : new Dictionary <string, TimeSpan> { { "Avatar 12", TimeSpan.FromMinutes(9000) } }, publisher : new Book.PublisherDto { Title = "Avatar" } ); var query = from b in Context.GetTable <Book>() where b.Name == bookRev1.Name && b.PublishYear == bookRev1.PublishYear select new { b.Author, b.NumPages, b.LastRentTime, b.PopularityRating, b.FilmsBasedOnBook, b.Publisher }; var bookProjection = query.Single(); Assert.AreEqual(bookRev1.Author, bookProjection.Author); Assert.AreEqual(bookRev1.NumPages, bookProjection.NumPages); Assert.AreEqual(bookRev1.LastRentTime, bookProjection.LastRentTime); Assert.AreEqual(bookRev1.PopularityRating, bookProjection.PopularityRating); Assert.AreEqual(bookRev1.FilmsBasedOnBook.Count, bookProjection.FilmsBasedOnBook.Count); Assert.AreEqual(bookRev1.Publisher.Title, bookProjection.Publisher.Title); }
public async Task DataContext_Find_RespectsPredefinedHashKey() { var book = await BooksHelper.CreateBookAsync(publishYear : 1234); var bookTable = Context.GetTable <Book>(book.Name); await bookTable.FindAsync(book.PublishYear); }
public async Task DateContext_Query_ExceptReturnsSubsetOfQueryNotIncludingAnotherQueryResults() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); var bookRev2 = await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var bookRev3 = await BooksHelper.CreateBookAsync(bookRev1.Name, 2014); await BooksHelper.CreateBookAsync(bookRev1.Name, 2015); var bookTable = Context.GetTable <Book>(); // ReSharper disable once ImplicitlyCapturedClosure var booksQuery1 = from record in bookTable where record.Name == bookRev1.Name select record; var booksQuery2 = from record in bookTable where record.Name == bookRev1.Name && record.PublishYear < bookRev3.PublishYear select record; var exceptResult = booksQuery1.Except(booksQuery2, new BooksComparer()); Assert.AreEqual(2, exceptResult.Count()); var resultAsList = exceptResult.ToList(); Assert.IsTrue(resultAsList.Contains(bookRev1, new BooksComparer())); Assert.IsTrue(resultAsList.Contains(bookRev2, new BooksComparer())); }
public async Task DataContext_Query_ReturnsDictionaryStringTimeSpan() { var book = await BooksHelper.CreateBookAsync( filmsBasedOnBook : new Dictionary <string, TimeSpan> { { "A Man and a Test", TimeSpan.FromMinutes(90) }, { "Avatar 12", TimeSpan.FromMinutes(9000) }, { "Aliens 8", TimeSpan.FromMinutes(80) } }); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == book.Name select record; var storedBook = booksQuery.First(); Assert.IsNotNull(storedBook.FilmsBasedOnBook, "Expected non-null dictionary"); Assert.AreEqual(book.FilmsBasedOnBook.Count, storedBook.FilmsBasedOnBook.Count, "Dictionary size mismatch"); foreach (var key in book.FilmsBasedOnBook.Keys) { Assert.IsTrue( storedBook.FilmsBasedOnBook.ContainsKey(key), "Stored dictionary does not have required key (" + key + ")"); Assert.AreEqual(book.FilmsBasedOnBook[key], storedBook.FilmsBasedOnBook[key], "Values mismatch"); } }
public async Task DataContext_EntityCreation_StoresComplexObjectProperties() { var book = await BooksHelper.CreateBookAsync(persistToDynamoDb : false, publisher : new Book.PublisherDto { Title = "O’Reilly Media", Address = "Sebastopol, CA" }); var booksTable = this.Context.GetTable <Book>(); booksTable.InsertOnSubmit(book); await this.Context.SubmitChangesAsync(); var storedBook = await booksTable.FindAsync(book.Name, book.PublishYear); Assert.AreEqual(book.Publisher.ToString(), storedBook.Publisher.ToString(), "Complex object properties are not equal"); storedBook.Publisher = new Book.PublisherDto { Title = "O’Reilly Media", Address = "Illoqortormiut, Greenland" }; await this.Context.SubmitChangesAsync(); var storedBook2 = await booksTable.FindAsync(book.Name, book.PublishYear); Assert.AreEqual(storedBook2.Publisher.ToString(), storedBook.Publisher.ToString(), "Complex object properties are not equal after updating"); }
public async Task DataContext_Find_ThrowsExceptionIfUserDoesNotProvideRangeKeyInHashRangeTables() { var book = await BooksHelper.CreateBookAsync(); var bookTable = Context.GetTable <Book>(); (await Should.ThrowAsync <InvalidOperationException>(() => bookTable.FindAsync(book.Name))) .Message.ShouldContain("has 2 key fields, but 1 key values was provided"); }
public async Task DataContext_EntityRemoval_DoesNotThrowAnyExceptionsIfRecordToRemoveDoesNotExist() { var book = await BooksHelper.CreateBookAsync(persistToDynamoDb : false); var booksTable = this.Context.GetTable <Book>(); booksTable.RemoveOnSubmit(book); await this.Context.SubmitChangesAsync(); }
public async Task DateContext_QueryWithFilterExpressionReturnsExpectedResults() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012, numPages : 30); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013, numPages : 20); await BooksHelper.CreateBookAsync(bookRev1.Name, 2014, numPages : 10); var filterExp1 = new Expression() { ExpressionStatement = "#N1 < :V1 AND #N1 > :V2", ExpressionAttributeNames = { { "#N1", "NumPages" } }, ExpressionAttributeValues = { { ":V1", 1000 }, { ":V2", 1 } } }; var filterExp2 = new Expression() { ExpressionStatement = "#N1 >= :V1", ExpressionAttributeNames = { { "#N1", "NumPages" } }, ExpressionAttributeValues = { { ":V1", 20 } } }; var filterExp3 = new Expression() { ExpressionStatement = "#N1 > :V1", ExpressionAttributeNames = { { "#N1", "NumPages" } }, ExpressionAttributeValues = { { ":V1", 100 } } }; var query1 = from b in Context.GetTable <Book>().WithFilterExpression(filterExp1) where b.Name == bookRev1.Name select b; var query2 = ( from b in Context.GetTable <Book>() where b.Name == bookRev1.Name select b ) .WithFilterExpression(filterExp2); var query3 = Context.GetTable <Book>().Where(b => b.Name == bookRev1.Name); query3.WithFilterExpression(filterExp3); Assert.AreEqual(3, query1.Count()); Assert.AreEqual(3, query1.Count()); Assert.AreEqual(2, query2.Count()); Assert.AreEqual(2, query2.Count()); Assert.AreEqual(0, query3.Count()); Assert.AreEqual(0, query3.Count()); Assert.IsTrue(Context.GetTable <Book>().Count() > 2); }
public async Task DataContext_Query_ReturnsIntFields() { var book = await BooksHelper.CreateBookAsync(numPages : 555); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == book.Name select record; var storedBook = booksQuery.First(); Assert.AreEqual(book.NumPages, storedBook.NumPages); }
public async Task DateContext_Query_AllFunctionReturnsFalseIfNoneElementsMatchPredicate() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var allResult = Context.GetTable <Book>().All(book => book.Name == Guid.NewGuid().ToString()); Assert.IsFalse(allResult); }
public async Task DataContext_Query_ReturnsEnumFieldsStoredAsString() { var book = await BooksHelper.CreateBookAsync(popularityRating : Book.Popularity.Average); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == book.Name select record; var storedBook = booksQuery.First(); Assert.AreEqual(book.PopularityRating, storedBook.PopularityRating); }
public async Task DataContext_Query_ReturnsStringFields() { var book = await BooksHelper.CreateBookAsync(author : "TestAuthor"); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == book.Name select record; var storedBook = booksQuery.First(); Assert.AreEqual(book.Author, storedBook.Author); }
public async Task DateContext_Query_AnyFunctionReturnsTrueIfAllElementsMatchPredicate() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var anyResult = Context.GetTable <Book>().Any(book => book.Name == bookRev1.Name); Assert.IsTrue(anyResult); }
public async Task DataContext_Query_ReturnsEnumFieldsStoredAsInt() { var book = await BooksHelper.CreateBookAsync(userFeedbackRating : Book.Stars.Platinum); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == book.Name select record; var storedBook = booksQuery.First(); Assert.AreEqual(book.UserFeedbackRating, storedBook.UserFeedbackRating); }
// [ExpectedException(typeof(ArgumentOutOfRangeException))] public async Task DateContext_Query_ElementAtThrowsArgumentOutOfRangeExceptionWhenInvalidIndexSpecified() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == bookRev1.Name select record; Should.Throw <ArgumentOutOfRangeException>(() => booksQuery.ElementAt(5)); }
// [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "cannot be added, because entity with that key already exists", MatchType = MessageMatch.Contains)] public async Task DataContext_EntityCreation_ThrowsExceptionWhenEntityAlreadyExistsInDynamoDbButWasNeverQueriedInCurrentContext() { var book = await BooksHelper.CreateBookAsync(popularityRating : Book.Popularity.Average); book.PopularityRating = Book.Popularity.High; var booksTable = this.Context.GetTable <Book>(); booksTable.InsertOnSubmit(book); await this.Context.SubmitChangesAsync(); }
public async Task DateContext_Query_LongCountFunctionReturnsCorrectNumberOfRecordsOnSmallDataSets() { const int DataSetLength = 20; var bookRev1 = await BooksHelper.CreateBookAsync(); await ParallelForAsync(1, DataSetLength, i => BooksHelper.CreateBookAsync(bookRev1.Name, bookRev1.PublishYear + i)); var numberOfRecordsInDb = Context.GetTable <Book>().LongCount(book => book.Name == bookRev1.Name); Assert.AreEqual(DataSetLength, numberOfRecordsInDb); }
public async Task DataContext_Query_ReturnsDateTimeFields() { var testTime = DateTime.Today.Add(new TimeSpan(0, 8, 45, 30, 25)); var book = await BooksHelper.CreateBookAsync(lastRentTime : testTime); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == book.Name select record; var storedBook = booksQuery.First(); Assert.AreEqual(book.LastRentTime.ToUniversalTime(), storedBook.LastRentTime.ToUniversalTime()); }
public async Task DateContext_Query_AllFunctionReturnsFalseIfAtLeastOneDoesNotMatchPredicate() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012, numPages : 123); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013, numPages : bookRev1.NumPages); await BooksHelper.CreateBookAsync(bookRev1.Name, 2014, numPages : 124); var allResult = Context.GetTable <Book>().All(book => book.Name == bookRev1.Name && book.NumPages == bookRev1.NumPages); Assert.IsFalse(allResult); }
public async Task DataContext_Query_ReturnsComplexObjectProperties() { var book = await BooksHelper.CreateBookAsync(publisher : new Book.PublisherDto { Title = "O’Reilly Media", Address = "Sebastopol, CA" }); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == book.Name select record; var storedBook = booksQuery.First(); Assert.AreEqual(book.Publisher.ToString(), storedBook.Publisher.ToString(), "Complex object properties are not equal"); }
public async Task DateContext_Query_LastFunctionReturnsFirstElementMatchingPredicate() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == bookRev1.Name select record; var lastElement = booksQuery.Last(book => book.PublishYear == bookRev1.PublishYear); Assert.AreEqual(bookRev1.PublishYear, lastElement.PublishYear); }
public async Task DateContext_Query_LastOrDefaultFunctionReturnsDefaultValueIfNotFound() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == bookRev1.Name select record; var lastElement = booksQuery.LastOrDefault(book => book.Name == null); Assert.IsNull(lastElement); }
public async Task DataContext_EntityRemoval_RemovesExistingRecordFromDynamoDb() { var book = await BooksHelper.CreateBookAsync(); var booksTable = this.Context.GetTable <Book>(); booksTable.RemoveOnSubmit(book); await this.Context.SubmitChangesAsync(); var storedBooksCount = booksTable.Count(storedBook => storedBook.Name == book.Name); Assert.AreEqual(0, storedBooksCount, "Record was not deleted"); }
public async Task DateContext_Query_AllFunctionReturnsTrueIfAllElementsMatchPredicate() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == bookRev1.Name select record; var allResult = booksQuery.All(book => book.Name == bookRev1.Name); Assert.IsTrue(allResult); }
public async Task DataContext_EntityCreation_PersistsRecordToDynamoDb() { var book = await BooksHelper.CreateBookAsync(persistToDynamoDb : false); var booksTable = this.Context.GetTable <Book>(); booksTable.InsertOnSubmit(book); await this.Context.SubmitChangesAsync(); var storedBook = await booksTable.FindAsync(book.Name, book.PublishYear); Assert.IsNotNull(storedBook); }
public async Task DataContext_UpdateEntity_UpdatesRecordWhenOldRecordIsNull() { var book = await BooksHelper.CreateBookAsync(popularityRating : Book.Popularity.Average); var booksTable = this.Context.GetTable <Book>(); book.PopularityRating = Book.Popularity.High; ((ITableCudOperations)booksTable).UpdateEntity(book, null); var storedBook = await booksTable.FindAsync(book.Name, book.PublishYear); Assert.AreEqual(book.PopularityRating, storedBook.PopularityRating, "Record was not updated"); }
public async Task DateContext_Query_ElementAtOrDefaultReturnsDefaultValueIfNotFound() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == bookRev1.Name select record; var positionedBook = booksQuery.ElementAtOrDefault(4); Assert.IsNull(positionedBook); }
public async Task DateContext_QueryOperationConfigCanBeCustomized() { var bookRev1 = await BooksHelper.CreateBookAsync(publishYear : 2012, author : "Gogol"); await BooksHelper.CreateBookAsync(bookRev1.Name, 2013); var query = Context.GetTable <Book>().Where(b => b.Name == bookRev1.Name) .ConfigureQueryOperation(config => { config.FilterExpression = new Expression() { ExpressionStatement = "attribute_exists (Author)" }; }); Assert.AreEqual(1, query.Count()); }
public async Task DataContext_EntityCreation_ThrowsExceptionWhenEntityPreviouslyStoredInDynamoDbWasQueriedInCurrentContext() { var book = await BooksHelper.CreateBookAsync(popularityRating : Book.Popularity.Average); var booksTable = this.Context.GetTable <Book>(); await booksTable.FindAsync(book.Name, book.PublishYear); book.PopularityRating = Book.Popularity.High; booksTable.InsertOnSubmit(book); (await Should.ThrowAsync <InvalidOperationException>(() => this.Context.SubmitChangesAsync())).Message.ShouldContain( "cannot be added, because entity with that key already exists" ); }