public void Check01ValidateTagOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);

                //ATTEMPT
                var dupTag = new Tag { Name = "non-duplicate slug", Slug = Guid.NewGuid().ToString("N") };
                db.Tags.Add(dupTag);
                var status = db.SaveChangesWithChecking();

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

                //ATTEMPT
                var dupTag = new Tag {Name = "duplicate slug", Slug = existingTag.Slug};
                db.Tags.Add(dupTag);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("The Slug on tag 'duplicate slug' must be unique.");
            }
        }
        public void Check01DeleteFailBecauseOfForeignKeyBad()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var post = db.Posts.First();
                db.PostLinks.Add(new PostLink {PostPart = post});
                db.SaveChanges();
            }
            
            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Posts.Remove(db.Posts.First());
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("This operation failed because another data entry uses this entry.");
            }
        }
        public void Check22CheckUpdateLastUpdatedOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var firstPost = db.Posts.First();
                var originalDateTime = firstPost.LastUpdated;
                Thread.Sleep(400);

                //ATTEMPT
                firstPost.Title = Guid.NewGuid().ToString();
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db);
                Assert.GreaterOrEqual(db.Posts.First().LastUpdated.Subtract(originalDateTime).Milliseconds, 400);
            }
        }
        public void Check21CheckUpdateSimpleOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var newGuid = Guid.NewGuid().ToString();

                //ATTEMPT
                var firstPost = db.Posts.First();
                firstPost.Title = newGuid;
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db);
                db.Posts.First().Title.ShouldEqual(newGuid);
            }
        }
        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 Check02UniqueKeyErrorBad()
        {
            //NOTE: To test this I needed to comment out the ValidateEntity method in SampleWebAppDb

            var tagGuid = Guid.NewGuid().ToString("N");
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid});
                db.SaveChanges();
            }

            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid });
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }
        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'.");
            }
        }
        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);
            }
        }
        public void Test01ValidateTagCaughtByHandleDictError()
        {
            //SETUP
            GenericServicesConfig.AddToSqlHandlerDict(2601, TestExceptionCatch);
            using (var db = new SampleWebAppDb())
            {
                var existingTag = db.Tags.First();

                //ATTEMPT
                var dupTag = new Tag { Name = "duplicate slug", Slug = existingTag.Slug };
                db.Tags.Add(dupTag);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("SQL error 2601. Following class types had errors: Tag.");
            }
        }
        public void Test10ValidateTagCaughtBySqlDict()
        {
            //SETUP
            GenericServicesConfig.AddToSqlHandlerDict(2601, TestExceptionNoCatch);
            using (var db = new SampleWebAppDb())
            {
                var existingTag = db.Tags.First();

                //ATTEMPT
                var dupTag = new Tag { Name = "duplicate slug", Slug = existingTag.Slug };
                db.Tags.Add(dupTag);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }
Esempio n. 12
0
        public void Check02ValidateTagError()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var existingTag = db.Tags.First();

                //ATTEMPT
                var dupTag = new Tag { Name = "duplicate slug", Slug = existingTag.Slug };
                db.Tags.Add(dupTag);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }
        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 Check26ReplaceTagsOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var firstPost = db.Posts.First();
                var tagsNotInFirstPostTracked = db.Tags.Where(x => x.Posts.All(y => y.PostId != firstPost.PostId)).ToList();

                //ATTEMPT
                db.Entry(firstPost).Collection(x => x.Tags).Load();
                firstPost.Tags = tagsNotInFirstPostTracked;
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 0, -1);
                firstPost = db.Posts.Include(x => x.Tags).First();
                firstPost.Tags.Count.ShouldEqual(1);
            }
        }
        public void Check27DeleteConnectedPostOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var lastPost = db.Posts.Include(x => x.Tags).ToList().Last();
                var numTags = lastPost.Tags.Count;

                //ATTEMPT
                db.Posts.Remove(lastPost);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, -1, -numTags);
            }
        }
        public void Check05CauseBothErrorsBad()
        {
            //NOTE: To test this I needed to comment out the ValidateEntity method in SampleWebAppDb

            var tagGuid = Guid.NewGuid().ToString("N");
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var post = db.Posts.First();
                db.PostLinks.Add(new PostLink { PostPart = post });
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid });
                db.SaveChanges();
            }

            using (var db = new SampleWebAppDb())
            {
                //ATTEMPT
                db.Posts.Remove(db.Posts.First());
                db.Tags.Add(new Tag { Name = tagGuid, Slug = tagGuid });
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(false);
                status.Errors.Count.ShouldEqual(1);         //for these two cases we only get one error
                status.Errors[0].ErrorMessage.ShouldEqual("One of the properties is marked as Unique index and there is already an entry with that value.");
            }
        }
        public void Check32DeleteViaRemoveOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var firstPostTagGrades = db.PostTagGrades.AsNoTracking().First();

                //ATTEMPT
                var ptgToDelete = new PostTagGrade
                {
                    PostId = firstPostTagGrades.PostId,
                    TagId = firstPostTagGrades.TagId
                };
                db.PostTagGrades.Attach(ptgToDelete);
                db.PostTagGrades.Remove(ptgToDelete);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 0, 0, 0, 0, -1);
            }
        }
        public void Check10UniqueKeyPostTagBad()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var firstPostTag = db.PostTagGrades.Include(x => x.PostPart).Include(x => x.TagPart).First();
          
                //ATTEMPT
                db.PostTagGrades.Add(new PostTagGrade{ PostPart = firstPostTag.PostPart, TagPart = firstPostTag.TagPart });
                var ex = Assert.Throws<DbUpdateException>(  () => db.SaveChangesWithChecking());

                //VERIFY
            }
        }