public void UpdateContent_Always_ShouldSucceed()
        {
            Post existingPost;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                existingPost = dbContext.Posts
                               .OrderBy(p => Guid.NewGuid())
                               .First();
            }
            var oldContent = existingPost.Content;
            var newContent = "vauprewhfsoivdlhbgreyfads;ivjnr";

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                service.UpdateContent(existingPost.Title, newContent);
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                var updatedPost = dbContext.Posts
                                  .Where(p => p.PostId == existingPost.PostId)
                                  .Single();
                Assert.AreNotEqual(oldContent, updatedPost.Content);
                Assert.AreEqual(newContent, updatedPost.Content);
            }
        }
        public void DeleteByTitle_InvalidTitle_ShouldFailAndNotDeleteAnything(string invalidTitle)
        {
            IEnumerable <Post> beforeList;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                beforeList = dbContext.Posts
                             .ToList();
            }

            int result;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                result = service.DeleteByTitle(invalidTitle);
            }

            Assert.AreEqual(0, result);
            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                IEnumerable <Post> afterList = dbContext.Posts
                                               .ToList();

                Assert.AreEqual(beforeList.Count(), afterList.Count());
                foreach (var post in beforeList)
                {
                    Assert.True(afterList.Contains(post));
                }
            }
        }
 private void QuickDumpAllTables()
 {
     using (var dbContext = new BloggingDbContext(_contextOptions))
     {
         dbContext.Database.EnsureDeletedAsync();
     }
 }
        public void Find_Always_ShouldReturnCorrectList(string term)
        {
            IEnumerable <Post> expectedList;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                expectedList = dbContext.Posts
                               .Include(p => p.Blog)
                               .Where(p => p.Title.Contains(term))
                               .ToList();
            }

            IEnumerable <Post> returnedList;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                returnedList = service.Find(term);
            }

            Assert.AreEqual(expectedList.Count(), returnedList.Count());
            foreach (var expectedPost in expectedList)
            {
                var correspondingActualPost =
                    returnedList
                    .Where(p => p.PostId == expectedPost.PostId)
                    .Single();

                Assert.AreEqual(expectedPost.Title, correspondingActualPost.Title);
                Assert.AreEqual(expectedPost.Content, correspondingActualPost.Content);
                Assert.AreEqual(expectedPost.Blog.Url, correspondingActualPost.Blog.Url);
            }
        }
        public void GetAll_Always_ShouldReturnTheFullList()
        {
            IEnumerable <Post> expectedFullList;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                expectedFullList = dbContext.Posts
                                   .Include(p => p.Blog)
                                   .ToList();
            }

            IEnumerable <Post> actualFullList;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                actualFullList = service.GetAll();
            }

            Assert.AreEqual(expectedFullList.Count(), actualFullList.Count());
            foreach (var expectedPost in expectedFullList)
            {
                var correspondingActualPost =
                    actualFullList
                    .Where(p => p.PostId == expectedPost.PostId)
                    .Single();

                Assert.AreEqual(expectedPost.Title, correspondingActualPost.Title);
                Assert.AreEqual(expectedPost.Content, correspondingActualPost.Content);
                Assert.IsNotNull(correspondingActualPost.Blog);
                Assert.AreEqual(expectedPost.Blog.Url, correspondingActualPost.Blog.Url);
            }
        }
    internal static void Seed(BloggingDbContext dbContext)
    {
        var blog = new BlogEntity
        {
            Id      = 1,
            Title   = "每个人都有选择幸福的自由",
            Content = "每个人都要为自己的选择负责 很多前来心理咨询的来访者,几乎都是是还没有学会爱自己的人,也是还不大明白对自己负责",
            Created = new DateTime(2017, 3, 19),
        };

        dbContext.Blogs.AddOrUpdate(b => b.Id, blog);

        var firstComment = new CommentEntity
        {
            Id      = 1,
            Blog    = blog,
            Content = "我感同身受",
        };

        var secondComment = new CommentEntity
        {
            Id      = 2,
            BlogId  = 1, //两种指定外键的方式,或者指定BlogId,或者指定Blog, EF会自动算出来.
            Content = "我感同身受",
        };

        dbContext.Comments.AddOrUpdate(c => c.Id, firstComment);
        dbContext.Comments.AddOrUpdate(c => c.Id, secondComment);
    }
Example #7
0
        public BlogValidator(BloggingDbContext dbContext)
        {
            // Case0: No dependency on DbContext
            RuleFor(blog => blog.BlogId).NotEmpty();
            RuleFor(blog => blog.Url).NotNull();

            // Case1: You need DbContext for runtime validation.
            // DbContext can be null on swagger generation and active on request validation.
            RuleFor(blog => blog.Url)
            .Must(url => dbContext.Blogs.Count(b => b.Url == url) == 0)
            .WithMessage("Url must be unique");

            // Case2: You need DbContext for defining rules.
            // DbContext must be active on swagger generation!!!
            var propertyMetadata = dbContext.Metadata
                                   .FirstOrDefault(metadata => metadata.TypeName == nameof(Blog) && metadata.PropertyName == nameof(Blog.Author));

            if (propertyMetadata != null)
            {
                var ruleBuilder = RuleFor(blog => blog.Author);
                if (propertyMetadata.IsRequired)
                {
                    ruleBuilder.NotNull();
                }
                if (propertyMetadata.MaxLength.HasValue)
                {
                    ruleBuilder.MaximumLength(propertyMetadata.MaxLength.Value);
                }
            }
        }
        public void DeleteByTitle_TitleExists_ShouldSucceed()
        {
            Post targetPostToDelete;
            int  oldCount;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                targetPostToDelete = dbContext.Posts
                                     .OrderBy(p => Guid.NewGuid().ToString())
                                     .First();
                oldCount = dbContext.Posts
                           .Count();
            }

            int result;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                result = service.DeleteByTitle(targetPostToDelete.Title);
            }

            Assert.AreEqual(1, result);
            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                int newCount = dbContext.Posts.Count();
                Assert.AreEqual(oldCount - 1, newCount);
                var afterDeletionQuery = dbContext.Posts
                                         .Where(p => p.Title.Equals(targetPostToDelete.Title));
                Assert.True(afterDeletionQuery.Count() == 0);
            }
        }
        public void UpdateContent_TitleCannotBeFound_ShouldFail()
        {
            var nonexistentTitle = "NonExistentTitle";
            var newContent       = "vauprewhfsoivdlhbgreyfads;ivjnr";

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                Assert.Throws(typeof(InvalidOperationException),
                              () => service.UpdateContent(nonexistentTitle, newContent));
            }
        }
        public void UpdateContent_InvalidNewTitle_ShouldFail(string invalidNewTitle)
        {
            var newContent = "vauprewhfsoivdlhbgreyfads;ivjnr";

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);

                Assert.Throws(
                    typeof(InvalidOperationException),
                    () => service.UpdateContent(invalidNewTitle, newContent));
            }
        }
        public void UpdateTitle_OriginalTitleCannotBeFound_ShouldFail()
        {
            string nonexistentOldTitle = "avdsoiblnksdaigfuv";
            string newTitle            = "Why hybrid supercars are afraid of the truth";

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);

                Assert.Throws(
                    typeof(InvalidOperationException),
                    () => service.UpdateTitle(nonexistentOldTitle, newTitle));
            }
        }
        private static SqliteConnection CreateDatabaseAndGetConnection()
        {
            var connection = new SqliteConnection("Data Source=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <BloggingDbContext>().UseSqlite(connection).Options;

            using (var context = new BloggingDbContext(options))
            {
                context.GetService <IRelationalDatabaseCreator>().CreateTables();
            }

            return(connection);
        }
        public void Add_EmptyTitle_ShouldFail()
        {
            Blog twitter;
            Post post;
            int  beforeInsertCount;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                bool hasTwitter = dbContext.Blogs
                                  .Where(b => b.Url.Contains("twitter"))
                                  .Count() > 0;
                if (!hasTwitter)
                {
                    dbContext.Blogs.Add(new Blog
                    {
                        Url = "www.twitter.com"
                    });
                    dbContext.SaveChanges();
                }
                twitter = dbContext.Blogs
                          .Where(b => b.Url.Contains("twitter"))
                          .Single();

                post = new Post
                {
                    Title   = "",
                    Content = "lahfpwqohavskjlsbatuwqhaps;vddsah",
                    Blog    = twitter
                };
                beforeInsertCount = dbContext.Posts.Count();
            }

            int result;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                result = service.Add(post.Title, post.Content, twitter.Url);
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                Assert.AreEqual(0, result);
                Assert.AreEqual(beforeInsertCount, dbContext.Posts.Count());
            }
        }
        public void Add_ValidInputData_ShouldSucceed()
        {
            Blog blog = new Blog
            {
                Url = "www.instagram.com"
            };
            Post post = new Post
            {
                Title   = "How summer activities changed how we think about death",
                Content = "lksahfpwqohavskjlsbatuwqhaps;vddsah",
                Blog    = blog
            };
            int beforeInsertCount;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                dbContext.Blogs.Add(blog);
                dbContext.SaveChangesAsync();
                beforeInsertCount = dbContext.Posts.Count();
            }

            int result;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                result = service.Add(post.Title, post.Content, blog.Url);
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                Assert.AreEqual(1, result);
                Assert.AreEqual(beforeInsertCount + 1, dbContext.Posts.Count());
                var newlyAddedPost = dbContext.Posts
                                     .Include(p => p.Blog)
                                     .Where(p => p.Title.Contains("How summer activities changed how we think about death"))
                                     .Single();
                Assert.AreEqual(post.Title, newlyAddedPost.Title);
                Assert.AreEqual(post.Content, newlyAddedPost.Content);
                Assert.IsNotNull(newlyAddedPost.Blog);
                Assert.AreEqual(post.Blog.Url, newlyAddedPost.Blog.Url);
            }
        }
        public void UpdateTitle_Always_ShouldSucceed()
        {
            Blog facebook;
            Post post;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                facebook = dbContext.Blogs
                           .Where(b => b.Url.Contains("facebook"))
                           .Single();
                post = new Post
                {
                    Title   = "Why game jobs are killing you",
                    Content = "ouvaisdhvsa",
                    Blog    = facebook
                };
                dbContext.Posts.AddRange(post);
                dbContext.SaveChangesAsync();
            }

            string newTitle = "Why hybrid supercars are afraid of the truth";

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                service.UpdateTitle(
                    post.Title,
                    newTitle
                    );
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                var updatedPost = dbContext.Posts
                                  .Include(p => p.Blog)
                                  .Where(p => p.Title == newTitle)
                                  .Single();
                Assert.AreEqual(post.Content, updatedPost.Content);
                Assert.AreEqual(post.PostId, updatedPost.PostId);
                Assert.AreEqual(post.BlogId, updatedPost.BlogId);
                Assert.AreEqual(post.Blog.Url, updatedPost.Blog.Url);
            }
        }
        public void UpdateUrl_InvalidUrl_ShouldFail(string invalidUrl)
        {
            Post existingPost;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                existingPost = dbContext.Posts
                               .OrderBy(p => Guid.NewGuid().ToString())
                               .FirstOrDefault();
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);

                Assert.Throws(
                    typeof(InvalidOperationException),
                    () => service.UpdateUrl(existingPost.Title, invalidUrl));
            }
        }
        public void UpdateUrl_BlogWithInputUrlDoesNotExistInOtherDatabase_ShouldFail()
        {
            Post existingPost;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                existingPost = dbContext.Posts
                               .OrderBy(p => Guid.NewGuid().ToString())
                               .FirstOrDefault();
            }

            var nonexistentNewUrl = "localhost";

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);

                Assert.Throws(
                    typeof(InvalidOperationException),
                    () => service.UpdateUrl(existingPost.Title, nonexistentNewUrl));
            }
        }
        public void UpdateUrl_Always_ShouldSucceed()
        {
            Blog instagram = new Blog
            {
                Url = "www.instagram.com"
            };
            Post existingPost;

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                dbContext.Blogs.Add(instagram);
                dbContext.SaveChangesAsync();
                existingPost = dbContext.Posts
                               .OrderBy(p => Guid.NewGuid().ToString())
                               .First();
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                PostService service = new PostService(dbContext);
                service.UpdateUrl(
                    existingPost.Title,
                    instagram.Url
                    );
            }

            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                var updatedPost = dbContext.Posts
                                  .Include(p => p.Blog)
                                  .Where(p => p.Title == existingPost.Title)
                                  .Single();
                Assert.AreEqual(existingPost.Content, updatedPost.Content);
                Assert.AreEqual(existingPost.PostId, updatedPost.PostId);
                Assert.AreEqual(instagram.Url, updatedPost.Blog.Url);
            }
        }
Example #19
0
        public static void UseInMemoryDbContext(Action <DbContextOptions <BloggingDbContext> > action)
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <BloggingDbContext>()
                              .UseSqlite(connection)
                              .Options;

                using (var dbContext = new BloggingDbContext(options))
                {
                    dbContext.Database.EnsureCreated();
                }

                action(options);
            }
            finally
            {
                connection.Close();
            }
        }
Example #20
0
 public BloggingController(BloggingDbContext dbContext)
 {
     _dbContext = dbContext;
 }
 protected override void Seed(BloggingDbContext dbContext)
 {
     //每次运行Update-Database,都会运行这个函数
     BloggingDatabaseSeed.Seed(dbContext);
 }
Example #22
0
 public PostService(BloggingDbContext context)
 {
     _context = context;
 }
Example #23
0
 public BlogService(ServiceContext context, BloggingDbContext db) : base(context)
 {
     _db = db;
 }
Example #24
0
 public BlogService(IServiceProvider context, BloggingDbContext db) : base(context)
 {
     _db = db;
 }
Example #25
0
 public BlogQuery(BloggingDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Example #26
0
 public IdentityController(BloggingDbContext context)
 {
     this.context = context;
 }
        private void SeedData()
        {
            using (var dbContext = new BloggingDbContext(_contextOptions))
            {
                dbContext.Database.EnsureCreatedAsync();

                Blog google = new Blog {
                    Url = "www.google.com"
                };
                Blog facebook = new Blog {
                    Url = "www.facebook.com"
                };
                Blog lttStore = new Blog {
                    Url = "www.lttstore.com"
                };

                //adding blogs
                dbContext.Blogs.AddRangeAsync(
                    google,
                    facebook,
                    lttStore
                    );

                dbContext.Posts.AddRangeAsync(
                    new Post
                {
                    Title   = "How to be unpopular in the business insurance world",
                    Content = "fslahfsdakjfds",
                    Blog    = google
                },
                    new Post
                {
                    Title   = "Why your accessory never works out the way you plan",
                    Content = "fslahfsdakjfds",
                    Blog    = google
                },
                    new Post
                {
                    Title   = "The 17 worst songs about special olympic world games",
                    Content = "fslahfsdakjfds",
                    Blog    = google
                },
                    new Post
                {
                    Title   = "The 8 worst home tech gadgets in history",
                    Content = "fslahfsdakjfds",
                    Blog    = facebook
                },
                    new Post
                {
                    Title   = "14 things your boss expects you know about football highlights",
                    Content = "fslahfsdakjfds",
                    Blog    = facebook
                },
                    new Post
                {
                    Title   = "10 things your boss expects you know about popular songs",
                    Content = "fslahfsdakjfds",
                    Blog    = facebook
                },
                    new Post
                {
                    Title   = "Why you'll never succeed at vaccination schedules",
                    Content = "fslahfsdakjfds",
                    Blog    = lttStore
                },
                    new Post
                {
                    Title   = "Why celebrity cruises will change your life",
                    Content = "fslahfsdakjfds",
                    Blog    = lttStore
                },
                    new Post
                {
                    Title   = "Why hairstyles are killing you",
                    Content = "fslahfsdakjfds",
                    Blog    = lttStore
                }
                    );
                dbContext.SaveChangesAsync();
            }
        }
Example #28
0
 public BlogService(BloggingDbContext dbContext)
 {
     _dbContext = dbContext;
 }