Exemplo n.º 1
0
        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 static void ResetBlogs(SampleWebAppDb context, TestDataSelection selection)
        {
            try
            {
                context.Posts.ToList().ForEach(x => context.Posts.Remove(x));
                context.Tags.ToList().ForEach(x => context.Tags.Remove(x));
                context.Blogs.ToList().ForEach(x => context.Blogs.Remove(x));
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                _logger.Critical("Exception when resetting the blogs", ex);
                throw;
            }

            var bloggers = LoadDbDataFromXml.FormBlogsWithPosts(XmlBlogsDataFileManifestPath[selection]);

            context.Blogs.AddRange(bloggers);
            var status = context.SaveChangesWithChecking();

            if (!status.IsValid)
            {
                _logger.CriticalFormat("Error when resetting courses data. Error:\n{0}",
                                       string.Join(",", status.Errors));
                throw new FormatException("problem writing to database. See log.");
            }
        }
        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.");
            }
        }
Exemplo n.º 5
0
        public void Check16ValidatePostTitleError()
        {
            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 ask a question, i.e. the title can't end with '?'.");
            }
        }
        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.");
            }
        }
Exemplo n.º 7
0
        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);
            }
        }
Exemplo n.º 8
0
        public static void FillComputedNPost(this SampleWebAppDb db, int totalOfEach)
        {
            //clear the current
            ClearDatabase(db);

            var tag1 = new Tag {
                Name = "Tag1", Slug = "tag1"
            };
            var tag2 = new Tag {
                Name = "Tag2", Slug = "tag2"
            };

            db.Tags.AddRange(new[] { tag1, tag2 });
            var blogger1 = new Blog()
            {
                Name = "Name1", EmailAddress = "Email1"
            };
            var blogger2 = new Blog()
            {
                Name = "Name2", EmailAddress = "Email2"
            };

            db.Blogs.AddRange(new[] { blogger1, blogger2 });
            var posts = BuildNPosts(tag2, blogger2, totalOfEach);

            db.Posts.AddRange(posts);

            var status = db.SaveChangesWithChecking();

            status.IsValid.ShouldEqual(true, status.Errors);
        }
Exemplo n.º 9
0
        //---------------------------------------

        public static void DeletePostEfDirect(this SampleWebAppDb db, int postId)
        {
            var postClass = new Post
            {
                PostId = postId
            };

            db.Entry(postClass).State = EntityState.Deleted;
            var status = db.SaveChangesWithChecking();

            status.IsValid.ShouldEqual(true, status.Errors);
        }
        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
            }
        }
Exemplo n.º 11
0
        //-----------------------------------------

        public static void UpdatePostEfDirect(this SampleWebAppDb db, int postId)
        {
            var postClass = db.Posts.Include(x => x.Blogger).Include(x => x.Tags).Single(x => x.PostId == postId);

            var guidString = Guid.NewGuid().ToString("N");

            postClass.Title   = guidString;
            postClass.Content = guidString;
            postClass.Blogger = db.Blogs.First();
            postClass.Tags    = new Collection <Tag> {
                db.Tags.First()
            };

            var status = db.SaveChangesWithChecking();

            status.IsValid.ShouldEqual(true, status.Errors);
        }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        //-------------------

        public static void CreatePostEfDirect(this SampleWebAppDb db, int id)
        {
            var guidString = Guid.NewGuid().ToString("N");
            var postClass  = new Post
            {
                Title   = guidString,
                Content = guidString,
                Blogger = db.Blogs.First(),
                Tags    = new Collection <Tag> {
                    db.Tags.First()
                }
            };

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

            status.IsValid.ShouldEqual(true, status.Errors);
        }
Exemplo n.º 14
0
        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);
            }
        }
Exemplo n.º 15
0
        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);
            }
        }
Exemplo n.º 16
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("The Slug on tag 'duplicate slug' must be unique and is already being used.");
            }
        }
Exemplo n.º 17
0
        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);
            }
        }
Exemplo n.º 18
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.");
            }
        }
Exemplo n.º 19
0
        //-------------------------------------------

        public static void FillComputedNAll(this SampleWebAppDb db, int totalOfEach)
        {
            //clear the current
            ClearDatabase(db);

            var tags = BuildTags(totalOfEach);

            db.Tags.AddRange(tags);
            var bloggers = BuildBloggers(totalOfEach);

            db.Blogs.AddRange(bloggers);
            var posts = BuildPostsNAll(tags, bloggers, totalOfEach);

            db.Posts.AddRange(posts);
            var grades = BuildGrades(tags, posts, totalOfEach);

            db.PostTagGrades.AddRange(grades);
            var status = db.SaveChangesWithChecking();

            status.IsValid.ShouldEqual(true, status.Errors);
        }
Exemplo n.º 20
0
        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);
            }
        }
Exemplo n.º 21
0
        public void Check25UpdatePostToAddTagOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap      = new DbSnapShot(db);
                var badTag    = db.Tags.Single(x => x.Slug == "bad");
                var firstPost = db.Posts.First();

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

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 0, 1);
                firstPost = db.Blogs.Include(x => x.Posts.Select(y => y.Tags)).First().Posts.First();
                firstPost.Tags.Count.ShouldEqual(3);
            }
        }
        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.");
            }
        }
Exemplo n.º 24
0
        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);
            }
        }
Exemplo n.º 25
0
        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);
            }
        }
Exemplo n.º 26
0
        public void Check31CheckCreateDataOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var firstPostUntracked = db.Posts.AsNoTracking().First();

                //ATTEMPT
                firstPostUntracked.Title   = Guid.NewGuid().ToString();
                firstPostUntracked.Blogger = db.Blogs.First();
                firstPostUntracked.Tags    = db.Tags.Take(2).ToList();
                db.Posts.Add(firstPostUntracked);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 1, 2);
                var loadedPost = db.Posts.Include(x => x.Blogger).Include(x => x.Tags).Single(x => x.PostId == firstPostUntracked.PostId);
                loadedPost.Blogger.BlogId.ShouldEqual(db.Blogs.First().BlogId);
                CollectionAssert.AreEquivalent(db.Tags.Take(2).Select(x => x.TagId), loadedPost.Tags.Select(x => x.TagId));
            }
        }
        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.");
            }
        }
Exemplo n.º 28
0
        public void Check30CheckCreateLastUpdatedOk()
        {
            using (var db = new SampleWebAppDb())
            {
                //SETUP
                var snap = new DbSnapShot(db);
                var firstPostUntracked = db.Posts.AsNoTracking().First();
                var originalDateTime   = firstPostUntracked.LastUpdated;
                Thread.Sleep(400);

                //ATTEMPT
                firstPostUntracked.Title   = Guid.NewGuid().ToString();
                firstPostUntracked.Blogger = db.Blogs.First();
                firstPostUntracked.Tags    = db.Tags.Take(2).ToList();
                db.Posts.Add(firstPostUntracked);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                snap.CheckSnapShot(db, 1, 2);
                var loadedPost = db.Posts.Single(x => x.PostId == firstPostUntracked.PostId);
                Assert.GreaterOrEqual(loadedPost.LastUpdated.Subtract(originalDateTime).Milliseconds, 400);
            }
        }