public async Task FilterShouldWork()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "FilterShouldWork").Options;
            var dbContext = new ApplicationDbContext(options);

            var postsRepo    = new EfDeletableEntityRepository <Post>(dbContext);
            var makesRepo    = new EfDeletableEntityRepository <Make>(dbContext);
            var makesService = new MakesService(makesRepo);
            var postsService = new PostsService(postsRepo, makesService);

            // BMW M5
            var inputModel = new PostDetailsInputModel()
            {
                Make       = 1,
                Model      = "M5",
                MaxMileage = 15000,
            };

            var post1 = new Post()
            {
                Make = new Make()
                {
                    Name = "BMW"
                },
                Model = new Model()
                {
                    Name = "M5"
                },
                Mileage = 14000,
            };

            var post2 = new Post()
            {
                Make = new Make()
                {
                    Name = "BMW"
                },
                Model = new Model()
                {
                    Name = "M5"
                },
                Mileage = 22000,
            };

            await postsService.AddAsync(post1);

            await postsService.AddAsync(post2);

            Assert.Single(postsService.Filter(inputModel));
        }
        public async Task AddAsyncShouldThrowNullExceptionForInvalidPost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddAsyncShouldThrowNullExceptionForInvalidPost").Options;
            var dbContext = new ApplicationDbContext(options);
            await dbContext.SaveChangesAsync();

            var postsRepo    = new EfDeletableEntityRepository <Post>(dbContext);
            var makesRepo    = new EfDeletableEntityRepository <Make>(dbContext);
            var makesService = new MakesService(makesRepo);
            var postsService = new PostsService(postsRepo, makesService);

            await Assert.ThrowsAsync <ArgumentNullException>(() => postsService.AddAsync(null));
        }
Пример #3
0
        public async Task AddAsyncThrowsWhenTheUserIdIsNull()
        {
            // Arrange
            var mapperMock     = new Mock <IMapper>();
            var repositoryMock = new Mock <IDeletableEntityRepository <Post> >();

            var postsService = new PostsService(repositoryMock.Object, mapperMock.Object);

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                // Act
                await postsService.AddAsync(new PostInputModel(), new ImageInputModel(), null);
            });
        }
        public async Task AddAsyncShouldWork()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddAsyncShouldWork").Options;
            var dbContext = new ApplicationDbContext(options);

            var postsRepo    = new EfDeletableEntityRepository <Post>(dbContext);
            var makesRepo    = new EfDeletableEntityRepository <Make>(dbContext);
            var makesService = new MakesService(makesRepo);
            var postsService = new PostsService(postsRepo, makesService);

            Assert.Empty(postsService.GetAll());

            await postsService.AddAsync(new Post());

            Assert.Single(postsService.GetAll());
        }
Пример #5
0
        public PostsModule(ApiDbContext context) : base("/posts")
        {
            PostsService postservice = new PostsService(context);

            // получаем пост по id
            Get("/{id}", name: "GetPostById", action: async(__params, __token) =>
            {
                this.RequiresAuthentication();

                Guid id = __params.Id;

                return(postservice.GetPostByIdAsync(id, __token));
            });

            Post("/", name: "AddPost", action: async(__, __token) =>
            {
                this.RequiresAuthentication();

                Post post = this.Bind();

                return(postservice.AddAsync(post, __token));
            });

            Put("/", name: "UpdatePost", action: async(__, __token) =>
            {
                this.RequiresAuthentication();
                this.RequiresClaims(c => c.Type == ClaimTypes.Role && c.Value == "admin");

                Post post = this.Bind();

                return(postservice.UpdateAsync(post, __token));
            });

            Delete("/{id}", name: "DeletePost", action: async(__params, __token) =>
            {
                this.RequiresAuthentication();
                this.RequiresClaims(c => c.Type == ClaimTypes.Role && c.Value == "admin");

                Guid id = __params.Id;

                return(postservice.RemoveAsync(id, __token));
            });
        }
Пример #6
0
        public async Task AddMapsTheInputModelsSetsUserIdAndAddsToTheRepository(
            string categoryId,
            string text,
            string title,
            bool nullImage,
            string imageSource,
            string userId)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);

            var saved = false;

            var postsList      = new List <Post>();
            var repositoryMock = new Mock <IDeletableEntityRepository <Post> >();

            repositoryMock.Setup(x => x.AddAsync(It.IsAny <Post>()))
            .Callback((Post post) =>
            {
                postsList.Add(post);
            });

            repositoryMock.Setup(x => x.SaveChangesAsync())
            .Callback(() =>
            {
                saved = true;
            });

            var postsService = new PostsService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            var postInputModel = new PostInputModel()
            {
                CategoryId = categoryId,
                Text       = text,
                Title      = title,
            };

            var imageInputModel = new ImageInputModel()
            {
                Source = imageSource,
            };

            if (nullImage)
            {
                imageInputModel = null;
            }

            // Act
            await postsService.AddAsync(postInputModel, imageInputModel, userId);

            // Assert
            var actualPost = postsList[0];

            Assert.True(saved);
            Assert.Equal(categoryId, actualPost.CategoryId);
            Assert.Equal(text, actualPost.Text);
            Assert.Equal(title, actualPost.Title);
            Assert.Equal(userId, actualPost.ApplicationUserId);

            var actualImage = actualPost.Image;

            if (nullImage)
            {
                Assert.Null(actualImage);
            }
            else
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
        }