public async void Check02UpdateDataFromDtoAsyncOk()
        {

            //SETUP
            var dto = new SimplePostDtoAsync
            {
                PostId = 123,
                BloggerName = "This should not be copied",
                Title = "Should copy this title",
                LastUpdated = new DateTime(2000, 1, 1),
                Tags = new Collection<Tag> { new Tag { Name = "Should not copy this", Slug = "No" } }
            };


            //ATTEMPT
            var newData = new Post
            {
                Blogger = new Blog { Name = "Original Blog Name" },
                BlogId = 777,
                Content = "Original Content",
                Tags = new Collection<Tag> { new Tag { Name = "Original Tag name", Slug = "Yes" } }
            };

            var status = await dto.UpdateDataFromDtoAsync(null, dto, newData);

            //VERIFY
            status.IsValid.ShouldEqual(true, status.Errors);
            newData.PostId.ShouldEqual(123);
            newData.Title.ShouldEqual("Should copy this title");

            newData.Blogger.Name.ShouldEqual("Original Blog Name");
            newData.BlogId.ShouldEqual(777);
            newData.Content.ShouldEqual("Original Content");
            newData.Tags.Count.ShouldEqual(1);
            newData.Tags.First().Name.ShouldEqual("Original Tag name");
        }
        private void DecodeBlogsAndGrades(XElement element, Dictionary<string, Tag> tagsDict)
        {
            Bloggers = new List<Blog>();
            Posts = new List<Post>();
            PostTagGrades = new List<PostTagGrade>();
            foreach (var blogXml in element.Elements("Blog"))
            {
                var newBlogger = new Blog()
                {
                    Name = blogXml.Element("Name").Value,
                    EmailAddress = blogXml.Element("Email").Value,
                    Posts = new Collection<Post>()
                };

                foreach (var postXml in blogXml.Element("Posts").Elements("Post"))
                {
                    var newPost = new Post()
                    {
                        Blogger = newBlogger,
                        Title = postXml.Element("Title").Value,
                        Content = postXml.Element("Content").Value,
                        Tags = postXml.Element("TagSlugs").Value.Split(',').Select(x => tagsDict[x.Trim()]).ToList()
                    };


                    //look for PostTagGrades for this post
                    foreach (var postTagXml in postXml.Elements("PostTagGrade"))
                    {
                        var newPostTag = new PostTagGrade
                        {
                            PostPart = newPost,
                            TagPart = tagsDict[postTagXml.Element("TagSlug").Value.Trim()],
                            Grade = int.Parse( postTagXml.Element("Grade").Value)
                        };
                        PostTagGrades.Add( newPostTag);
                    }
                    Posts.Add(newPost);
                    newBlogger.Posts.Add(newPost);

                }
                Bloggers.Add(newBlogger);
            }
        }
        public void Check10PostCopyKeysBackToDtoOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var timer = new Stopwatch();
                var post = new Post{PostId = 123};
                var dto = new DetailPostDto();

                //ATTEMPT
                timer.Start();
                dto.AfterCreateCopyBackKeysToDtoIfPresent(db, post);
                timer.Stop();

                Console.WriteLine("took {0:f3} ms", 1000.0 * timer.ElapsedTicks / Stopwatch.Frequency);
                //VERIFY
                dto.PostId.ShouldEqual(post.PostId);
            }
        }
        public void Check20AddPostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);

                //ATTEMPT
                var uglyTag = db.Tags.Single(x => x.Slug == "ugly");
                var jonBlogger = db.Blogs.First();
                var newPost = new Post
                {
                    Blogger = jonBlogger,
                    Content = "a few simple words.",
                    Title = "A new post",
                    Tags = new[] { uglyTag }
                };

                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 1, 1);
                var uglyPosts = db.Tags.Include(x => x.Posts).Single(y => y.Slug == "ugly").Posts;
                uglyPosts.Count.ShouldEqual(3);
            }
        }
        public void Check28DeleteViaAttachOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var lastPost = db.Posts.Include(x => x.Tags).AsNoTracking().ToList().Last();
                var numTags = lastPost.Tags.Count;

                //ATTEMPT
                var postToDelete = new Post {PostId = lastPost.PostId};
                db.Posts.Attach(postToDelete);
                db.Posts.Remove(postToDelete);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, -1, -numTags);
            }
        }
 private async Task<ISuccessOrErrors> FailDeleteRelationships(IGenericServicesDbContext db, Post post)
 {
     return new SuccessOrErrors().AddSingleError("I failed.");
 }
 private async Task<ISuccessOrErrors> DeleteBloggerWithPost(IGenericServicesDbContext db, Post post)
 {
     var blogger = await db.Set<Blog>().FindAsync(post.BlogId);
     db.Set<Blog>().Remove(blogger);
     return SuccessOrErrors.Success("It was fine.");
 }
        public void Check15ValidatePostTitleError()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var existingTag = db.Tags.First();
                var existingBlogger = db.Blogs.First();

                //ATTEMPT
                var newPost = new Post()
                {
                    Blogger = existingBlogger,
                    Title = "Test post!",
                    Content = "Nothing special",
                    Tags = new[] { existingTag }
                };
                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("Sorry, but you can't get too excited and include a ! in the title.");
            }
        }
        public void Check10ValidatePostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var existingTag = db.Tags.First();
                var existingBlogger = db.Blogs.First();

                //ATTEMPT
                var newPost = new Post()
                {
                    Blogger = existingBlogger,
                    Title = "Test post",
                    Content = "Nothing special",
                    Tags = new[] { existingTag }
                };
                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db,1,1);
            }
        }
        public void Check21ValidatePostContentTwoErrors()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var existingTag = db.Tags.First();
                var existingBlogger = db.Blogs.First();

                //ATTEMPT
                var newPost = new Post()
                {
                    Blogger = existingBlogger,
                    Title = "Test post",
                    Content = "Should not end sentence with sheep. Nor end sentence with lamb.",
                    Tags = new[] { existingTag }
                };
                db.Posts.Add(newPost);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(2);
                status.Errors[0].ErrorMessage.ShouldEqual("Sorry. Not allowed to end a sentance with 'sheep'.");
                status.Errors[1].ErrorMessage.ShouldEqual("Sorry. Not allowed to end a sentance with 'lamb'.");
            }
        }