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 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 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.");
            }
        }
示例#4
0
 private static void ClearDatabase(SampleWebAppDb db)
 {
     db.Posts.RemoveRange(db.Posts);
     db.Tags.RemoveRange(db.Tags);
     db.Blogs.RemoveRange(db.Blogs);
     db.PostTagGrades.RemoveRange(db.PostTagGrades);
     db.SaveChanges();
 }
 public void SetUp()
 {
     using (var db = new SampleWebAppDb())
     {
         DataLayerInitialise.InitialiseThis();
         var filepath = TestFileHelpers.GetTestFileFilePath("DbContentSimple.xml");
         DataLayerInitialise.ResetDatabaseToTestData(db, filepath);
         db.SaveChanges();
     }
 }
示例#6
0
        public void Like(PostLikeViewModel postLikeViewModel)
        {
            Post post = db.Posts.SingleOrDefault(c => c.PostId == postLikeViewModel.PostId);

            if (post == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            ApplicationUser user = db.Users.SingleOrDefault(c => c.UserName == User.Identity.Name);

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            post.Like.Add(user);

            db.SaveChanges();
        }
        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.");
            }
        }