Exemple #1
0
		public void TestPostFunctionality()
		{
			// Create few Post to enter into the database.
			const string title = "My First Post";
			var post1 = new Post()
			{
				Title = title,
				Body = "This isn't a very long post.",
				Tags = new List<string>(),
				Comments = new List<Comment>
				{
					{ new Comment() { TimePosted = new DateTime(2013,1,1), 
									  Email = "*****@*****.**", 
									  Body = "This article is too short!" } },
					{ new Comment() { TimePosted = new DateTime(2013,1,2), 
									  Email = "*****@*****.**", 
									  Body = "I agree with Jake." } }
				}
			};

			var post2 = new Post()
			{
				Title = "My Second Post",
				Body = "This isn't a very long post either.",
				Tags = new List<string>(),
				Comments = new List<Comment>
				{
					{ new Comment() { TimePosted = new DateTime(2013,1,3), 
									  Email = "*****@*****.**", 
									  Body = "Why are you wasting your time!" } },
				}
			};

			// Insert the posts into the DB
			PostAccess.CreatePost(post1);
			PostAccess.CreatePost(post2);

			// Get a post
			var posts = PostAccess.FindPostWithCommentsBy("sara");

			// Verify the acceptance criteria
			Assert.AreEqual(1, posts.Count());
			Assert.AreEqual(title, posts.First());

			// Compute tags for posts
			PostAccess.ComputeTags(new[] { post1.Title });

			// Get the post
			var postsWithTag = PostAccess.GetPosts();

			// Verify the acceptance criteria
			Assert.AreEqual(2, postsWithTag.Count());
			Assert.IsTrue(postsWithTag.First(p => p.Title == post1.Title).Tags.Count > 0);
			Assert.IsTrue(postsWithTag.First(p => p.Title == post2.Title).Tags.Count == 0);
		}
		private bool UpdateTagsInPost(IMongoRepository<Post> repo, Post post)
		{
			var tags = ComputeTagsFromBody(post.Body);

			// COMPARE AND SWAP (CAS)
			// - Compare the body text to make sure its not stale
			// - Swap in the freshly computed tags
			return repo.Update(Query.And(
									Query.EQ("_id", post.Id),
									Query.EQ(Post.FN_BODY, post.Body)
							   ),
			                   Update.AddToSetEachWrapped(Post.FN_TAGS, tags),
			                   UpdateFlags.None);
		}
		public void TestComputeTagsWithFault()
		{
			// Create few Post to enter into the database.
			var post1 = new Post()
			{
				Title = "My First Post",
				Body = "This isn't a very long post.",
				Tags = new List<string>(),
				Comments = new List<Comment>
				{
					{ new Comment() { TimePosted = new DateTime(2013,1,1), 
									  Email = "*****@*****.**", 
									  Body = "This article is too short!" } },
					{ new Comment() { TimePosted = new DateTime(2013,1,2), 
									  Email = "*****@*****.**", 
									  Body = "I agree with Jake." } }
				}
			};

			var post2 = new Post()
			{
				Title = "My Second Post",
				Body = "This isn't a very long post either.",
				Tags = new List<string>(),
				Comments = new List<Comment>
				{
					{ new Comment() { TimePosted = new DateTime(2013,1,3), 
									  Email = "*****@*****.**", 
									  Body = "Why are you wasting your time!" } },
				}
			};

			// Insert the posts into the DB
			PostAccess.CreatePost(post1);
			PostAccess.CreatePost(post2);

			// Compute tags for posts
			using (new FaultInjection.FaultInjection(new RandomlyFaultQueryAndUpdatesTestScenario(), this))
			{
				PostAccess.ComputeTags(new[] { post1.Title });
			}

			// Get the post
			var postsWithTag = PostAccess.GetPosts();

			// Verify the acceptance criteria
			Assert.AreEqual(2, postsWithTag.Count());
			Assert.IsTrue(postsWithTag.First(p => p.Title == post1.Title).Tags.Count > 0);
			Assert.IsTrue(postsWithTag.First(p => p.Title == post2.Title).Tags.Count == 0);
		}
		public void CreatePost(Post post)
		{
			var command = new CreatePostCommand() { PostToCreate = post };
			ExecuteCommand<int>(command);
		}