public void DataContext_EntityCreation_StoresComplexObjectProperties() { var book = BooksHelper.CreateBook(persistToDynamoDb: false, publisher: new Book.PublisherDto { Title = "O’Reilly Media", Address = "Sebastopol, CA" }); var booksTable = this.Context.GetTable <Book>(); booksTable.InsertOnSubmit(book); this.Context.SubmitChanges(); var storedBook = booksTable.Find(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" }; this.Context.SubmitChanges(); var storedBook2 = booksTable.Find(book.Name, book.PublishYear); Assert.AreEqual(storedBook2.Publisher.ToString(), storedBook.Publisher.ToString(), "Complex object properties are not equal after updating"); }
public void DataContext_Query_ReturnsDictionaryStringTimeSpan() { var book = BooksHelper.CreateBook( 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 void DataContext_Caching_ComplexObjectListFieldSavedSuccessfully() { BooksHelper.CreateBook(name: "The Hitchhiker's Guide to the Galaxy", reviews: new List <Book.ReviewDto> { new Book.ReviewDto { Author = "Beavis", Text = "Cool" } }); var bookTable = Context.GetTable <Book>(() => this.TableCache); var loadedBook = bookTable.Where(b => b.Name == "The Hitchhiker's Guide to the Galaxy").ToArray().Single(); // Modifying the existing list object is not supported. We need to replace the whole property value. loadedBook.ReviewsList = new List <Book.ReviewDto> { new Book.ReviewDto { Author = "Beavis", Text = "Cool" }, new Book.ReviewDto { Author = "Butt-head", Text = "This sucks!" } }; Context.SubmitChanges(); this._cacheHitCount = 0; var loadedBook2 = bookTable.Where(b => b.Name == "The Hitchhiker's Guide to the Galaxy").ToArray().Single(); Assert.AreNotEqual(0, this._cacheHitCount, "Query result wasn't loaded from cache"); Assert.AreEqual(2, loadedBook2.ReviewsList.Count); Assert.IsTrue(loadedBook2.ReviewsList.Exists(r => r.Author == "Butt-head")); }
public void TableCache_IndexIsRecreated() { var conditions = new SearchConditions(); // creating index using (var indexCreator = this.TableCache.StartCreatingIndex(conditions)) { indexCreator.AddEntityToIndex(new EntityKey(Guid.NewGuid(), Guid.NewGuid()), this.ToDocumentConverter(BooksHelper.CreateBook(persistToDynamoDb: false))); indexCreator.AddEntityToIndex(new EntityKey(Guid.NewGuid(), Guid.NewGuid()), this.ToDocumentConverter(BooksHelper.CreateBook(persistToDynamoDb: false))); indexCreator.AddEntityToIndex(new EntityKey(Guid.NewGuid(), Guid.NewGuid()), this.ToDocumentConverter(BooksHelper.CreateBook(persistToDynamoDb: false))); } var indexBooks = new Dictionary <EntityKey, Book>(); indexBooks[new EntityKey(Guid.NewGuid(), Guid.NewGuid())] = BooksHelper.CreateBook(persistToDynamoDb: false); indexBooks[new EntityKey(Guid.NewGuid(), Guid.NewGuid())] = BooksHelper.CreateBook(persistToDynamoDb: false); indexBooks[new EntityKey(Guid.NewGuid(), Guid.NewGuid())] = BooksHelper.CreateBook(persistToDynamoDb: false); // re-creating index using (var indexCreator = this.TableCache.StartCreatingIndex(conditions)) { foreach (var book in indexBooks) { indexCreator.AddEntityToIndex(book.Key, this.ToDocumentConverter(book.Value)); } } // checking, that index contains only the last three books var originalBooks = indexBooks.Values.OrderByDescending(b => b.Name); var loadedBooks = this.TableCache.GetEntities(conditions, null, "Name", true).Select(d => (Book)d.ToObject(typeof(Book))); this.DeepCompareBookCollections(originalBooks, loadedBooks); }
public void TableCache_IndexIsDiscardedIfEntityIsMissing() { EntityKey randomBookKey = null; int bookCount = 5; int randomBookIndex = new Random().Next(0, bookCount - 1); var conditions = new SearchConditions(); using (var indexCreator = this.TableCache.StartCreatingIndex(conditions)) { for (int i = 0; i < bookCount; i++) { var bookKey = new EntityKey(Guid.NewGuid(), Guid.NewGuid()); indexCreator.AddEntityToIndex(bookKey, this.ToDocumentConverter(BooksHelper.CreateBook(persistToDynamoDb: false))); if (i == randomBookIndex) { randomBookKey = bookKey; } } } // dropping an entity from cache this.DropEntityFromCache(randomBookKey); Assert.IsNull(this.TableCache.GetEntities(conditions, null, null, false)); Assert.IsNull(this.TableCache.GetEntities(conditions, null, null, false)); }
public void DateContext_Query_ReturnsProjection() { var bookRev1 = BooksHelper.CreateBook( 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 void DateContext_RetrievesManyRecords() { const int DataSetLength = 500; var bookRev1 = BooksHelper.CreateBook(); Parallel.For(1, DataSetLength, i => { try { BooksHelper.CreateBook(bookRev1.Name, bookRev1.PublishYear + i); } catch (ProvisionedThroughputExceededException) { Thread.Sleep(10000); // trying one more time BooksHelper.CreateBook(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 void TableCache_LocalChangesAreNeverOverwrittenWhenCreatingAnIndex() { var bookKey1 = new EntityKey(1, 1); var book1 = BooksHelper.CreateBook(author: "Mark Twain", numPages: 1, persistToDynamoDb: false); var bookKey2 = new EntityKey(2, 2); var book2 = BooksHelper.CreateBook(author: "Mark Twain", numPages: 2, persistToDynamoDb: false); var book21 = BooksHelper.CreateBook(author: "Mark Twain", numPages: 21, persistToDynamoDb: false); var bookKey3 = new EntityKey(3, 3); var book3 = BooksHelper.CreateBook(author: "Mark Twain", numPages: 3, persistToDynamoDb: false); // creating and filling one index with a filter var index1 = new SearchConditions(); index1.AddCondition("Author", new SearchCondition(ScanOperator.Equal, "Mark Twain".ToDynamoDbEntry(typeof(string)))); using (var indexCreator = this.TableCache.StartCreatingIndex(index1)) { indexCreator.AddEntityToIndex(bookKey1, this.ToDocumentConverter(book1)); indexCreator.AddEntityToIndex(bookKey2, this.ToDocumentConverter(book2)); } // now start creating another index var index2 = new SearchConditions(); using (var indexCreator = this.TableCache.StartCreatingIndex(index2)) { // loading some garnish indexCreator.AddEntityToIndex(new EntityKey(Guid.NewGuid(), Guid.NewGuid()), this.ToDocumentConverter(BooksHelper.CreateBook(author: "Mark Twain", persistToDynamoDb: false))); indexCreator.AddEntityToIndex(new EntityKey(Guid.NewGuid(), Guid.NewGuid()), this.ToDocumentConverter(BooksHelper.CreateBook(author: "Mark Twain", persistToDynamoDb: false))); // in parallel modifying existing index this.TableCache.UpdateCacheAndIndexes ( new Dictionary <EntityKey, Document> { { bookKey3, this.ToDocumentConverter(book3) } }, new Dictionary <EntityKey, Document> { { bookKey2, this.ToDocumentConverter(book21) } }, new [] { bookKey1 } ); indexCreator.AddEntityToIndex(new EntityKey(Guid.NewGuid(), Guid.NewGuid()), this.ToDocumentConverter(BooksHelper.CreateBook(author: "Mark Twain", persistToDynamoDb: false))); // loading the same books to the second index - these should be discarded indexCreator.AddEntityToIndex(bookKey2, this.ToDocumentConverter(book2)); indexCreator.AddEntityToIndex(bookKey1, this.ToDocumentConverter(book1)); } // the second index shouldn't be created Assert.IsNull(this.TableCache.GetEntities(index2, null, null, false)); // the first index should now contain book3 and book21 var expectedBooks = new[] { book3, book21 }; var loadedBooks = this.TableCache.GetEntities(index1, null, "NumPages", false).Select(d => (Book)d.ToObject(typeof(Book))); this.DeepCompareBookCollections(expectedBooks, loadedBooks); }
public void DataContext_Find_RespectsPredefinedHashKey() { var book = BooksHelper.CreateBook(publishYear: 1234); var bookTable = Context.GetTable <Book>(book.Name); bookTable.Find(book.PublishYear); }
public void DataContext_Find_ThrowsExceptionIfUserDoesNotProvideRangeKeyInHashRangeTables() { var book = BooksHelper.CreateBook(); var bookTable = Context.GetTable <Book>(); bookTable.Find(book.Name); }
public void DateContext_Query_AnyFunctionReturnsTrueIfOneElementMatchesPredicate() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012); var bookRev2 = BooksHelper.CreateBook(bookRev1.Name, 2013); var anyResult = Context.GetTable <Book>().Any(book => book.Name == bookRev1.Name && book.PublishYear == bookRev2.PublishYear); Assert.IsTrue(anyResult); }
public void DataContext_EntityRemoval_DoesNotThrowAnyExceptionsIfRecordToRemoveDoesNotExist() { var book = BooksHelper.CreateBook(persistToDynamoDb: false); var booksTable = this.Context.GetTable <Book>(); booksTable.RemoveOnSubmit(book); this.Context.SubmitChanges(); }
private void DataContext_Caching_ProjectionLoadedFromCacheBase(bool loadFullEntitiesBefore) { BooksHelper.CreateBook(author: "S hakespeare"); BooksHelper.CreateBook(author: "William Shakespeare"); BooksHelper.CreateBook(author: "S hakespeare William"); BooksHelper.CreateBook(author: "Chakespare"); var bookTable = Context.GetTable <Book>(() => this.TableCache); int shakespeareBooksCount = 0; if (loadFullEntitiesBefore) { foreach (var book in bookTable.Where(b => b.Author.StartsWith("Shakespeare"))) { Logger.DebugFormat("Found book from author: {0}", book.Author); shakespeareBooksCount++; } } else { foreach (var book in bookTable.Where(b => b.Author.StartsWith("Shakespeare")).Select(b => new { b.Name, b.Author })) { Logger.DebugFormat("Found book from author: {0}", book.Author); shakespeareBooksCount++; } } Assert.AreEqual(0, this._cacheHitCount, "Cache wasn't flushed for some strange reason"); int shakespeareBooksCountFromCache = 0; foreach (var book in from b in bookTable where b.Author.StartsWith("Shakespeare") select new { b.Name, b.Author }) { Logger.DebugFormat("Found book from author: {0}", book.Author); shakespeareBooksCountFromCache++; } Assert.AreNotEqual(0, this._cacheHitCount, "Query result wasn't loaded from cache"); Assert.AreEqual(shakespeareBooksCount, shakespeareBooksCountFromCache, "The number of books in cache differs from the number of books in DynamoDB"); shakespeareBooksCountFromCache = 0; this._cacheHitCount = 0; foreach (var book in from b in bookTable where b.Author.StartsWith("Shakespeare") select new { b.Name, b.Author }) { Logger.DebugFormat("Found book from author: {0}", book.Author); shakespeareBooksCountFromCache++; } Assert.AreNotEqual(0, this._cacheHitCount, "Query result wasn't loaded from cache"); Assert.AreEqual(shakespeareBooksCount, shakespeareBooksCountFromCache, "The number of books in cache differs from the number of books in DynamoDB"); }
public void DataContext_Query_ReturnsIntFields() { var book = BooksHelper.CreateBook(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 void DataContext_Query_ReturnsEnumFieldsStoredAsString() { var book = BooksHelper.CreateBook(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 void DataContext_Query_ReturnsEnumFieldsStoredAsInt() { var book = BooksHelper.CreateBook(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); }
public void DataContext_Query_ReturnsStringFields() { var book = BooksHelper.CreateBook(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 void DateContext_Query_AllFunctionReturnsFalseIfNoneElementsMatchPredicate() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012); BooksHelper.CreateBook(bookRev1.Name, 2013); var allResult = Context.GetTable <Book>().All(book => book.Name == Guid.NewGuid().ToString()); Assert.IsFalse(allResult); }
public void DateContext_QueryWithFilterExpressionReturnsExpectedResults() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012, numPages: 30); BooksHelper.CreateBook(bookRev1.Name, 2013, numPages: 20); BooksHelper.CreateBook(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 void DateContext_Query_LongCountFunctionReturnsCorrectNumberOfRecordsOnSmallDataSets() { const int DataSetLength = 20; var bookRev1 = BooksHelper.CreateBook(); Parallel.For(1, DataSetLength, i => BooksHelper.CreateBook(bookRev1.Name, bookRev1.PublishYear + i)); var numberOfRecordsInDb = Context.GetTable <Book>().LongCount(book => book.Name == bookRev1.Name); Assert.AreEqual(DataSetLength, numberOfRecordsInDb); }
public void DateContext_Query_AllFunctionReturnsFalseIfAtLeastOneDoesNotMatchPredicate() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012, numPages: 123); BooksHelper.CreateBook(bookRev1.Name, 2013, numPages: bookRev1.NumPages); BooksHelper.CreateBook(bookRev1.Name, 2014, numPages: 124); var allResult = Context.GetTable <Book>().All(book => book.Name == bookRev1.Name && book.NumPages == bookRev1.NumPages); Assert.IsFalse(allResult); }
public void DataContext_EntityCreation_ThrowsExceptionWhenEntityAlreadyExistsInDynamoDbButWasNeverQueriedInCurrentContext() { var book = BooksHelper.CreateBook(popularityRating: Book.Popularity.Average); book.PopularityRating = Book.Popularity.High; var booksTable = this.Context.GetTable <Book>(); booksTable.InsertOnSubmit(book); this.Context.SubmitChanges(); }
public void DateContext_Query_LastFunctionReturnsFirstElementWithoutPredicate() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012); var bookRev2 = BooksHelper.CreateBook(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(); Assert.AreEqual(bookRev2.PublishYear, lastElement.PublishYear); }
public void DateContext_Query_ElementAtThrowsArgumentOutOfRangeExceptionWhenInvalidIndexSpecified() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012); BooksHelper.CreateBook(bookRev1.Name, 2013); var bookTable = Context.GetTable <Book>(); var booksQuery = from record in bookTable where record.Name == bookRev1.Name select record; // ReSharper disable once ReturnValueOfPureMethodIsNotUsed booksQuery.ElementAt(5); }
public void DataContext_Find_ReturnsExistingRecordWhenUsedWithHashAndRangeKeys() { var book = BooksHelper.CreateBook(publishYear: 1); BooksHelper.CreateBook(book.Name, book.PublishYear + 1); var bookTable = Context.GetTable <Book>(); var storedBook = bookTable.Find(book.Name, book.PublishYear); Assert.AreEqual(book.Name, storedBook.Name, "Book with wrong name was returned"); Assert.AreEqual(book.PublishYear, storedBook.PublishYear, "Book with wrong publishYear was returned"); }
public void DataContext_Query_ReturnsComplexObjectProperties() { var book = BooksHelper.CreateBook(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 void DataContext_Query_ReturnsDateTimeFields() { var testTime = DateTime.Today.Add(new TimeSpan(0, 8, 45, 30, 25)); var book = BooksHelper.CreateBook(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 void DateContext_Query_LastOrDefaultFunctionReturnsDefaultValueIfNotFound() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012); BooksHelper.CreateBook(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 void DataContext_UpdateEntity_UpdatesRecordWhenOldRecordIsNull() { var book = BooksHelper.CreateBook(popularityRating: Book.Popularity.Average); var booksTable = this.Context.GetTable <Book>(); book.PopularityRating = Book.Popularity.High; ((ITableCudOperations)booksTable).UpdateEntity(book, null); var storedBook = booksTable.Find(book.Name, book.PublishYear); Assert.AreEqual(book.PopularityRating, storedBook.PopularityRating, "Record was not updated"); }
public void DateContext_Query_AllFunctionReturnsTrueIfAllElementsMatchPredicate() { var bookRev1 = BooksHelper.CreateBook(publishYear: 2012); BooksHelper.CreateBook(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); }