Ejemplo n.º 1
0
		public static Book CreateBook(
			string name = null,
			int publishYear = default(int),
			string author = default(string),
			int numPages = default(int),
			Book.Popularity popularityRating = default(Book.Popularity),
			Book.Stars userFeedbackRating = default(Book.Stars),
			List<string> rentingHistory = default(List<string>),
			IDictionary<string, TimeSpan> filmsBasedOnBook = default(IDictionary<string, TimeSpan>),
			DateTime lastRentTime = default(DateTime),
            bool persistToDynamoDb = true,
            Book.PublisherDto publisher = default(Book.PublisherDto),
            List<Book.ReviewDto> reviews = default(List<Book.ReviewDto>))
		{
			name = name ?? "TestBook" + Guid.NewGuid();

			var book = new Book
			{
				Name = name,
				PublishYear = publishYear,
				Author = author,
				NumPages = numPages,
				PopularityRating = popularityRating,
				UserFeedbackRating = userFeedbackRating,
				RentingHistory = rentingHistory,
				FilmsBasedOnBook = filmsBasedOnBook,
				LastRentTime = lastRentTime,
                Publisher = publisher,
                ReviewsList = reviews,
			};

		    var bookData = new JavaScriptSerializer().Serialize(book);

		    if (persistToDynamoDb)
		    {
		        Logger.DebugFormat("Persisting book: {0}", bookData);
		        PersistenceContext.Save(book);
		    }
		    else
            {
                Logger.DebugFormat("Created in-memory book: {0}", bookData);
		    }

		    _recordsForCleanup.Enqueue(book);

			return book;
		}
        public void DateContext_Query_DefaultIfEmptyReturnsCollectionWithOneDefaultRecordIfDefaultValueSpecified()
        {
            var bookTable = Context.GetTable<Book>();
            var booksQuery = from record in bookTable where record.Name == Guid.NewGuid().ToString() select record;

            Assert.AreEqual(0, booksQuery.Count());

            var defaultBook = new Book { Name = Guid.NewGuid().ToString(), PublishYear = 1999 };
            var defaultIfEmptyResult = booksQuery.DefaultIfEmpty(defaultBook);
            Assert.AreEqual(1, defaultIfEmptyResult.Count());

            var actualResult = defaultIfEmptyResult.First();

            Assert.IsNotNull(actualResult);
            Assert.AreEqual(defaultBook.Name, actualResult.Name);
            Assert.AreEqual(defaultBook.PublishYear, actualResult.PublishYear);
        }