public async void TestSearch() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Post>(new ApplicationDbContext(options.Options)); var postService = new PostsService(repository); var userId = Guid.NewGuid().ToString(); await postService.CreateAsync("searchString", "test", 2, userId); await postService.CreateAsync("searchString2", "test", 2, userId); AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly); Assert.Equal(2, postService.Search <MyTestPost>("search").Count()); }
public async Task CreateAsync_WithValidData_ShouldAddToDatabase() { // Arrange var context = InMemoryDbContext.Initiliaze(); var postRepository = new EfDeletableEntityRepository <Post>(context); var postService = new PostsService(postRepository); // Act var model = new PostCreateModel { CreatorId = "creatorIdStringUnique", Description = "descriptionrandom", }; await postService.CreateAsync(model, "urltoimg.com", "dsahu2171dyh"); int expectedCount = postRepository.All().Count(); int actualCount = postRepository.All().Count(); var expectedPost = model; var actualPost = postRepository.All().FirstOrDefault(); // Assert Assert.Equal(expectedCount, actualCount); Assert.True(expectedPost.Description == actualPost.Description); Assert.True(expectedPost.CreatorId == actualPost.CreatorId); }
public async Task CreateMethodShouldAddPostInDatabase(string title, PostType type, string description, int categoryId) { var guid = Guid.NewGuid().ToString(); var options = new DbContextOptionsBuilder <ForumDbContext>() .UseInMemoryDatabase(guid) .Options; var db = new ForumDbContext(options); var dateTimeProvider = new Mock <IDateTimeProvider>(); var usersServiceMock = new Mock <IUsersService>(); dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27)); usersServiceMock.Setup(us => us.AddPointsAsync(guid, 1)).ReturnsAsync(1); var postsService = new PostsService(db, null, usersServiceMock.Object, dateTimeProvider.Object); var expectedIds = new[] { 1, 2, 3 }; var postId = await postsService.CreateAsync(title, type, description, guid, categoryId, expectedIds); var actual = await db.Posts.FirstOrDefaultAsync(); var actualTagIds = actual.Tags.Select(t => t.TagId).ToArray(); postId.Should().Be(1); db.Posts.Should().HaveCount(1); actualTagIds.Should().BeEquivalentTo(expectedIds); }
public async Task CreateAsyncShouldReturnCreatedPostIdUsingDbContext() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Post>(new ApplicationDbContext(options.Options)); repository.AddAsync(new Post { Id = 3, CategoryId = 1, Title = "test" }).GetAwaiter().GetResult(); repository.AddAsync(new Post { Id = 4, CategoryId = 1, Title = "b" }).GetAwaiter().GetResult(); repository.AddAsync(new Post { Id = 5, CategoryId = 2, Title = "c" }).GetAwaiter().GetResult(); repository.AddAsync(new Post { Id = 6, CategoryId = 2, Title = "d" }).GetAwaiter().GetResult(); repository.SaveChangesAsync().GetAwaiter().GetResult(); var postService = new PostsService(repository); AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly); var post = postService.CreateAsync("title", "description", 1, "userId"); //var byCategoryId = postService.GetByCategoryId<MyTestPost>(1); //var findPost = byCategoryId.Where(x => x.Title == "title").FirstOrDefault(); Assert.Equal(1, post.Id); }
public async Task PostsShouldBeAddedSuccessfully() { this.Setup(); var repo = new Mock <IDeletableEntityRepository <ForumPost> >(); repo.Setup(x => x.AddAsync(It.IsAny <ForumPost>())).Callback( (ForumPost post) => this.postsList.Add(post)); var categoryRepo = new Mock <IDeletableEntityRepository <ForumCategory> >(); var postsService = new PostsService(repo.Object, categoryRepo.Object); await postsService.CreateAsync(this.testPost, "testUser"); Assert.Single(this.postsList); Assert.Equal(this.testPost.Title, this.postsList.First().Title); await postsService.CreateAsync(this.testPost, "testUser"); Assert.Equal(2, this.postsList.Count); }
public async Task CreateMethodShouldAddNewPost() { var list = new List <Post>(); var mockRepo = new Mock <IDeletableEntityRepository <Post> >(); mockRepo.Setup(x => x.AddAsync(It.IsAny <Post>())).Callback((Post post) => list.Add(post)); var service = new PostsService(mockRepo.Object); await service.CreateAsync("Test", "Test", 1, "1"); Assert.Single(list); }
public async Task GetCountShouldReturnTheCorrectCount() { this.Setup(); var repo = new Mock <IDeletableEntityRepository <ForumPost> >(); repo.Setup(x => x.All()).Returns(this.postsList.AsQueryable()); repo.Setup(x => x.AddAsync(It.IsAny <ForumPost>())).Callback( (ForumPost post) => this.postsList.Add(post)); var categoryRepo = new Mock <IDeletableEntityRepository <ForumCategory> >(); var postsService = new PostsService(repo.Object, categoryRepo.Object); await postsService.CreateAsync(this.testPost, "testUser"); var count = postsService.GetCount(); Assert.Equal(1, count); await postsService.CreateAsync(this.testPost, "testUser"); count = postsService.GetCount(); Assert.Equal(2, count); }
public async Task <IActionResult> Create(PostCreateViewModel model) { if (ModelState.IsValid) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); await _postsService.CreateAsync(userId, model); return(RedirectToAction("Index")); } return(View(model)); }
public async void TestGetCountByCategoryId() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Post>(new ApplicationDbContext(options.Options)); var postService = new PostsService(repository); await postService.CreateAsync("test", "test", 2, Guid.NewGuid().ToString()); AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly); var post = postService.GetById <MyTestPost>(1); Assert.Equal(1, postService.GetCountByCategoryId(2)); }
public async Task GetByIdShouldReturnCorrectPost() { this.Setup(); var repo = new Mock <IDeletableEntityRepository <ForumPost> >(); repo.Setup(x => x.AllAsNoTracking()).Returns(this.postsList.AsQueryable()); repo.Setup(x => x.AddAsync(It.IsAny <ForumPost>())).Callback( (ForumPost post) => this.postsList.Add(post)); var categoryRepo = new Mock <IDeletableEntityRepository <ForumCategory> >(); var postsService = new PostsService(repo.Object, categoryRepo.Object); await postsService.CreateAsync(this.testPost, "testUser"); var postReturned = postsService.GetById <PostViewModel>(1); Assert.Equal("test", postReturned.Title); }
public async Task GetAllShouldReturnCorrectArticles() { this.Setup(); var repo = new Mock <IDeletableEntityRepository <ForumPost> >(); repo.Setup(x => x.All()).Returns(this.postsList.AsQueryable()); repo.Setup(x => x.AddAsync(It.IsAny <ForumPost>())).Callback( (ForumPost post) => this.postsList.Add(post)); var categoryRepo = new Mock <IDeletableEntityRepository <ForumCategory> >(); var postsService = new PostsService(repo.Object, categoryRepo.Object); await postsService.CreateAsync(this.testPost, "testUser"); var articles = postsService.GetAll <PostViewModel>(1, 10); Assert.Single(articles); }
public async Task CreateAsync_WithValidData_ShouldReturnTrue() { // Arrange var context = InMemoryDbContext.Initiliaze(); var postRepository = new EfDeletableEntityRepository <Post>(context); var postService = new PostsService(postRepository); // Act var model = new PostCreateModel { CreatorId = "creatorIdStringUnique", Description = "descriptionrandom", }; var result = await postService.CreateAsync(model, "urltoimg.com", "dsahu2171dyh"); // Assert Assert.True(result); }
public async Task CreateMethodShouldAddPostInDatabase(string title, PostType type, string description, int categoryId) { var guid = Guid.NewGuid().ToString(); var options = new DbContextOptionsBuilder <ForumDbContext>() .UseInMemoryDatabase(guid) .Options; var db = new ForumDbContext(options); var dateTimeProvider = new Mock <IDateTimeProvider>(); var usersServiceMock = new Mock <IUsersService>(); dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27)); usersServiceMock.Setup(us => us.AddPointsAsync(guid, 1)).ReturnsAsync(1); var postsService = new PostsService(db, null, usersServiceMock.Object, dateTimeProvider.Object); var postId = await postsService.CreateAsync(title, type, description, guid, categoryId); postId.Should().Be(1); db.Posts.Should().HaveCount(1); }