Exemplo n.º 1
0
        public async Task GetOrderByIdAsyncShouldReturnOrderSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var ordersService = new OrdersService(dbContext, mapper, usersService);

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var order = new Order
            {
                UserId = user.Id,
            };

            await dbContext.Orders.AddAsync(order);

            await dbContext.SaveChangesAsync();

            var expected = await ordersService.GetOrderByIdAsync(order.Id);

            Assert.Equal(expected.Id, order.Id);
        }
        public async Task GetCategoryByProgramIdAsyncShouldReturnCategoryByProgramIdSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var programCategory = new ProgramCategory
            {
                Id          = CategoryId,
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(programCategory);

            await dbContext.SaveChangesAsync();

            var program = new Program
            {
                Title       = ProgramTitle,
                Description = ProgramDescription,
                CategoryId  = programCategory.Id,
            };

            await dbContext.Programs.AddAsync(program);

            await dbContext.SaveChangesAsync();

            var expected = await categoriesService.GetCategoryByProgramIdAsync(program.Id);

            Assert.Equal(expected.Id, programCategory.Id);
        }
Exemplo n.º 3
0
        public async Task GetAddressByIdAsyncShouldReturnAddressByIdSuccessfully()
        {
            var option = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                         .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            var dbContext = new MyCalisthenicAppDbContext(option);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var addressService = new AddressesService(dbContext, mapper);

            var city = new City
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Id      = AddressId,
                Country = AddressCountryName,
                Street  = AddressStreet,
                CityId  = city.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var expected = await addressService.GetAddressByIdAsync(AddressId);

            Assert.Equal(expected.Id, address.Id);
        }
Exemplo n.º 4
0
        public async Task GetImagesByProductIdAsyncShouldReturnImagesSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var postsService = new PostsService(dbContext, mapper, usersService, categoriesService);

            var productsService = new ProductsService(dbContext, mapper, usersService, categoriesService);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService);

            var imagesService = new ImagesService(dbContext, mapper, postsService, exercisesService, productsService, programsService);

            for (int i = 0; i < 4; i++)
            {
                var image = new Image
                {
                    Url       = ImageUrl,
                    ProductId = ProductId,
                };

                await dbContext.Images.AddAsync(image);

                await dbContext.SaveChangesAsync();
            }

            var expected = await imagesService.GetImagesByProductIdAsync(ProductId);

            var counter = 0;

            foreach (var img in expected)
            {
                counter++;
            }

            Assert.Equal(4, counter);
        }
Exemplo n.º 5
0
        public async Task EditImageAsyncShouldEditImageSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var postsService = new PostsService(dbContext, mapper, usersService, categoriesService);

            var productsService = new ProductsService(dbContext, mapper, usersService, categoriesService);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService);

            var imagesService = new ImagesService(dbContext, mapper, postsService, exercisesService, productsService, programsService);

            var image = new Image
            {
                Id  = ImageId,
                Url = ImageUrl,
            };

            await dbContext.Images.AddAsync(image);

            await dbContext.SaveChangesAsync();

            var imageModel = new ImageAdminEditViewModel
            {
                Id  = ImageId,
                Url = ImageEditUrl,
            };

            await imagesService.EditImageAsync(imageModel);

            var actual = dbContext.Images.FirstOrDefaultAsync(i => i.Id == image.Id);

            Assert.Equal(imageModel.Url, actual.Result.Url);
        }
Exemplo n.º 6
0
        public async Task EditAddressAsyncShouldThrowExceptionIfAddressIsNull()
        {
            var option = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                         .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            var dbContext = new MyCalisthenicAppDbContext(option);

            var addressService = new AddressesService(dbContext, null);

            var city = new City
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Country = AddressCountryName,
                Street  = AddressStreet,
                CityId  = city.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var addressViewModel = new AddressAdminEditViewModel
            {
                Id = Guid.NewGuid().ToString(),
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await addressService.EditAddressAsync(addressViewModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Exemplo n.º 7
0
        public async Task EditImageAsyncShouldThrowExceptionIfProgramIdIsIncorrect()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var postsService = new PostsService(dbContext, mapper, usersService, categoriesService);

            var productsService = new ProductsService(dbContext, mapper, usersService, categoriesService);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService);

            var imagesService = new ImagesService(dbContext, mapper, postsService, exercisesService, productsService, programsService);

            var image = new Image
            {
                Id  = ImageId,
                Url = ImageUrl,
            };

            await dbContext.Images.AddAsync(image);

            await dbContext.SaveChangesAsync();

            var imageModel = new ImageAdminEditViewModel
            {
                Url       = ImageUrl,
                ProgramId = ProgramId,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await imagesService.EditImageAsync(imageModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
        public async Task CreateProgramAsyncShouldCreateProgramSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var category = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(category);

            await dbContext.SaveChangesAsync();

            var programModel = new ProgramAdminCreateViewModel
            {
                Title       = ProgramTitle,
                Description = ProgramDescription,
                Rating      = ProgramRating,
                CategoryId  = category.Id,
            };

            await programsService.CreateProgramAsync(programModel);

            var actual = await dbContext.Programs.FirstOrDefaultAsync();

            Assert.Equal(programModel.Title, actual.Title);
        }
Exemplo n.º 9
0
        public async Task CreateExerciseAsyncShouldCreateExerciseSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService);

            var programCategory = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(programCategory);

            await dbContext.SaveChangesAsync();

            var exerciseModel = new ExerciseAdminCreateViewModel
            {
                Name              = ExerciseName,
                Description       = ExerciseDescription,
                VideoUrl          = ExerciseVideoUrl,
                ProgramCategoryId = programCategory.Id,
            };

            await exercisesService.CreateExerciseAsync(exerciseModel);

            var actual = await dbContext.Exercises.FirstOrDefaultAsync(e => e.Name == exerciseModel.Name);

            Assert.Equal(actual.Name, exerciseModel.Name);
        }
Exemplo n.º 10
0
        public async Task EditExerciseAsyncShouldThrowExptionIfProgramCategoryIsNull()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService);

            var programCategory = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(programCategory);

            await dbContext.SaveChangesAsync();

            var exerciseModel = new ExerciseAdminEditViewModel
            {
                Id                = ExerciseId,
                Name              = ExerciseName,
                Description       = ExerciseDescription,
                VideoUrl          = ExerciseVideoUrl,
                ProgramCategoryId = ProgramCategoryId,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await exercisesService.EditExerciseAsync(exerciseModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Exemplo n.º 11
0
        public async Task EditSupplierAsyncShouldEditSupplierSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var suppliersService = new SuppliersService(dbContext, mapper);

            var supplier = new Supplier
            {
                Id            = SupplierId,
                Name          = SupplierName,
                PriceToHome   = SupplierPriceToHome,
                PriceToOffice = SupplierPriceToOffice,
            };

            await dbContext.Suppliers.AddAsync(supplier);

            await dbContext.SaveChangesAsync();

            var supplierModel = new SupplierAdminEditViewModel
            {
                Id            = supplier.Id,
                Name          = SupplierEditName,
                PriceToHome   = SupplierPriceToHome,
                PriceToOffice = SupplierPriceToOffice,
            };

            await suppliersService.EditSupplierAsync(supplierModel);

            var actual = await dbContext.Suppliers.FirstOrDefaultAsync(s => s.Name == supplier.Name);

            Assert.Equal(supplierModel.Name, actual.Name);
        }
Exemplo n.º 12
0
        public async Task EditUserAsyncShouldReturnUserSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var usersService = new UsersService(httpContextAccessor, dbContext, mapper);

            var user = new ApplicationUser
            {
                Id        = UserId,
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var userModel = new UserAdminEditViewModel
            {
                Id            = UserId,
                HasMembership = true,
            };

            await usersService.EditUserAsync(userModel);

            var actual = await dbContext.Users.FirstOrDefaultAsync(u => u.Id == UserId);

            Assert.True(actual.HasMembership);
        }
Exemplo n.º 13
0
        public async Task EditCommentAsyncShouldThrowExceptionIfInvalidProgramId()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var commentsService = new CommentsService(dbContext, mapper, usersService);

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var commentModel = new CommentAdminEditViewModel
            {
                Text      = CommentText,
                AuthorId  = user.Id,
                ProgramId = ProgramId,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await commentsService.EditCommentAsync(commentModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Exemplo n.º 14
0
        public async Task GetAllMembershipPlansAsyncShouldReturnAllMemberships()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var membershipsService = new MembershipsService(dbContext, mapper);

            for (int i = 0; i < 4; i++)
            {
                var membership = new Membership
                {
                    Name        = MembershipName,
                    YearlyPrice = MembershipYearlyPrice,
                };

                await dbContext.Memberships.AddAsync(membership);

                await dbContext.SaveChangesAsync();
            }

            var expected = await membershipsService.GetAllMembershipPlansAsync();

            var counter = 0;

            foreach (var mms in expected)
            {
                counter++;
            }

            Assert.Equal(4, counter);
        }
Exemplo n.º 15
0
        public async Task EditMembershipAsyncShouldEditMembershipSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var membershipsService = new MembershipsService(dbContext, mapper);

            var membership = new Membership
            {
                Id   = MembershipId,
                Name = MembershipName,
            };

            await dbContext.Memberships.AddAsync(membership);

            await dbContext.SaveChangesAsync();

            var membershipModel = new MembershipAdminEditViewModel
            {
                Id          = MembershipId,
                Name        = MembershipName,
                YearlyPrice = MembershipYearlyPrice,
            };

            await membershipsService.EditMembershipAsync(membershipModel);

            var actual = await dbContext.Memberships.FirstOrDefaultAsync(m => m.Id == MembershipId);

            Assert.Equal(actual.YearlyPrice, membershipModel.YearlyPrice);
        }
Exemplo n.º 16
0
        public async Task GetAllRequestsAsyncShouldReturnUserRequestsSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var usersService = new UsersService(httpContextAccessor, dbContext, mapper);

            for (int i = 0; i < 3; i++)
            {
                var request = new UserRequest
                {
                    FullName = UserRequestFullName,
                    Content  = UserRequestContent,
                    Email    = UserRequestEmail,
                };

                await dbContext.UserRequests.AddAsync(request);

                await dbContext.SaveChangesAsync();
            }

            var expected = await usersService.GetAllRequestsAsync();

            Assert.Equal(3, expected.Count);
        }
Exemplo n.º 17
0
        public async Task EditMembershipAsyncShouldThrowExceptionIfmembershipIsNull()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var membershipsService = new MembershipsService(dbContext, mapper);

            var membership = new Membership
            {
                Name = MembershipName,
            };

            await dbContext.Memberships.AddAsync(membership);

            await dbContext.SaveChangesAsync();

            var membershipModel = new MembershipAdminEditViewModel
            {
                Id   = MembershipId,
                Name = MembershipName,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await membershipsService.EditMembershipAsync(membershipModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Exemplo n.º 18
0
        public async Task GetProductsBySearchTextAsyncShouldReturnNullIfNoSuchProducts()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var searchesService = new SearchesService(dbContext, mapper);

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var category = new ProductCategory
            {
                Name = CategoryName,
            };

            await dbContext.ProductCategories.AddAsync(category);

            await dbContext.SaveChangesAsync();

            for (int i = 0; i < 3; i++)
            {
                var product = new Product
                {
                    Name        = ProductName,
                    Description = ProductDescription,
                    Price       = ProductPrice,
                    CategoryId  = category.Id,
                };

                product.Images.Add(new Image
                {
                    Url = ImageUrl,
                });

                product.Comments.Add(new Comment
                {
                    Text      = CommentText,
                    ProductId = product.Id,
                    AuthorId  = user.Id,
                });

                await dbContext.Products.AddAsync(product);

                await dbContext.SaveChangesAsync();
            }

            var searchModel = new SearchViewModel
            {
                Text = SearchText,
            };

            var expected = await searchesService.GetProductsBySearchTextAsync(searchModel);

            Assert.Null(expected);
        }
Exemplo n.º 19
0
        public async Task GetAllCommentsByUserIdAsyncShouldReturnCommentsSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var usersService = new UsersService(httpContextAccessor, dbContext, mapper);

            var postCategory = new BlogCategory
            {
                Name = PostCategoryName,
            };

            await dbContext.BlogCategories.AddAsync(postCategory);

            await dbContext.SaveChangesAsync();

            var post = new Post
            {
                Title       = PostTitle,
                Description = PostDescription,
                CategoryId  = postCategory.Id,
            };

            await dbContext.Post.AddAsync(post);

            await dbContext.SaveChangesAsync();

            var productCategory = new ProductCategory
            {
                Name = ProductCategoryName,
            };

            await dbContext.ProductCategories.AddAsync(productCategory);

            await dbContext.SaveChangesAsync();

            var product = new Product
            {
                Name        = ProductName,
                Description = ProductDescription,
                CategoryId  = productCategory.Id,
            };

            await dbContext.Products.AddAsync(product);

            await dbContext.SaveChangesAsync();

            var programCategory = new ProgramCategory
            {
                Name        = ProgramCategoryName,
                Description = ProgramDescription,
            };

            await dbContext.ProgramCategories.AddAsync(programCategory);

            await dbContext.SaveChangesAsync();

            var program = new Program
            {
                Title       = ProgramTitle,
                Description = ProgramDescription,
                CategoryId  = productCategory.Id,
            };

            await dbContext.Programs.AddAsync(program);

            await dbContext.SaveChangesAsync();

            var user = new ApplicationUser
            {
                Id        = UserId,
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            for (int i = 0; i < 3; i++)
            {
                var comment = new Comment
                {
                    Text    = CommentText,
                    Author  = user,
                    Post    = post,
                    Product = product,
                    Program = program,
                };

                await dbContext.Comments.AddAsync(comment);

                await dbContext.SaveChangesAsync();
            }

            var expected = await usersService.GetAllCommentsByUserIdAsync(UserId);

            Assert.Equal(3, expected.Count);
        }
Exemplo n.º 20
0
        public async Task GetAllProductsByOrderIdAsyncShouldReturnProductsSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var usersService = new UsersService(httpContextAccessor, dbContext, mapper);

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var order = new Order
            {
                Id     = OrderId,
                UserId = user.Id,
            };

            await dbContext.Orders.AddAsync(order);

            await dbContext.SaveChangesAsync();

            var productCategory = new ProductCategory
            {
                Name = ProductCategoryName,
            };

            await dbContext.ProductCategories.AddAsync(productCategory);

            await dbContext.SaveChangesAsync();

            var product = new Product
            {
                Name        = ProductName,
                Description = ProductDescription,
                CategoryId  = productCategory.Id,
            };

            await dbContext.Products.AddAsync(product);

            await dbContext.SaveChangesAsync();

            var orderProduct = new OrderProduct
            {
                OrderId   = order.Id,
                ProductId = product.Id,
                Product   = product,
            };

            await dbContext.OrderProducts.AddAsync(orderProduct);

            await dbContext.SaveChangesAsync();

            var expected = await usersService.GetAllProductsByOrderIdAsync(OrderId);

            Assert.Equal(1, expected.Count);
        }
        public async Task GetFivePopularProgramsAsyncShouldReturnFiveProgramsSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var category = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(category);

            await dbContext.SaveChangesAsync();

            for (int i = 0; i < 7; i++)
            {
                var program = new Program
                {
                    Title       = ProgramTitle,
                    Description = ProgramDescription,
                    CategoryId  = category.Id,
                };

                program.Images.Add(new Image
                {
                    Url = ImageUrl,
                });

                await dbContext.Programs.AddAsync(program);

                await dbContext.SaveChangesAsync();
            }

            var expected = await programsService.GetFivePopularProgramsAsync();

            var counter = 0;

            foreach (var program in expected)
            {
                counter++;
            }

            Assert.Equal(5, counter);
        }
        public async Task GetProgramByIdAsyncShouldReturnProgramSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService);

            var category = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(category);

            await dbContext.SaveChangesAsync();

            for (int i = 0; i < 3; i++)
            {
                var program = new Program
                {
                    Title       = ProgramTitle,
                    Description = ProgramDescription,
                    CategoryId  = category.Id,
                };

                await dbContext.Programs.AddAsync(program);

                await dbContext.SaveChangesAsync();
            }

            var programToReturn = new Program
            {
                Id          = ProgramId,
                Title       = ProgramTitle,
                Description = ProgramDescription,
                CategoryId  = category.Id,
            };

            await dbContext.Programs.AddAsync(programToReturn);

            await dbContext.SaveChangesAsync();

            var expected = await programsService.GetProgramByIdAsync(ProgramId);

            Assert.Equal(expected.Id, programToReturn.Id);
        }
Exemplo n.º 23
0
        public async Task EditOrderAsyncShouldEditOrderSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var ordersService = new OrdersService(dbContext, mapper, usersService);

            var user = new ApplicationUser
            {
                Id        = UserId,
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var city = new City
            {
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Country = AddressCountry,
                Street  = AddressStreet,
                CityId  = city.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var order = new Order
            {
                Id     = OrderId,
                UserId = user.Id,
            };

            await dbContext.Orders.AddAsync(order);

            await dbContext.SaveChangesAsync();

            var orderModel = new OrderAdminEditViewModel
            {
                Id                = order.Id,
                UserId            = user.Id,
                DeliveryAddressId = address.Id,
                TotalPrice        = OrderTotalPrice,
            };

            await ordersService.EditOrderAsync(orderModel);

            var actual = await dbContext.Orders.FirstOrDefaultAsync(o => o.Id == order.Id);

            Assert.Equal(order.TotalPrice, actual.TotalPrice);
        }
Exemplo n.º 24
0
        public async Task EditOrderAsyncShouldThrowExceptionIfOrderIsNull()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var ordersService = new OrdersService(dbContext, mapper, usersService);

            var user = new ApplicationUser
            {
                Id        = UserId,
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var city = new City
            {
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Country = AddressCountry,
                Street  = AddressStreet,
                CityId  = city.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var orderModel = new OrderAdminEditViewModel
            {
                UserId            = user.Id,
                DeliveryAddressId = address.Id,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await ordersService.EditOrderAsync(orderModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Exemplo n.º 25
0
        public async Task GetProgramsBySearchTextAsyncShouldReturnProgramsSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var searchesService = new SearchesService(dbContext, mapper);

            var category = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(category);

            await dbContext.SaveChangesAsync();

            for (int i = 0; i < 3; i++)
            {
                var program = new Program
                {
                    Title       = ProgramTitle,
                    Description = ProgramDescription,
                    CategoryId  = category.Id,
                };

                program.Images.Add(new Image
                {
                    Url = ImageUrl,
                });

                await dbContext.Programs.AddAsync(program);

                await dbContext.SaveChangesAsync();
            }

            var searchProgram = new Program
            {
                Title       = ProgramTitle,
                Description = ProgramSearchDescription,
                CategoryId  = category.Id,
            };

            searchProgram.Images.Add(new Image
            {
                Url = ImageUrl,
            });

            await dbContext.Programs.AddAsync(searchProgram);

            await dbContext.SaveChangesAsync();

            var searchModel = new SearchViewModel
            {
                Text = SearchText,
            };

            var expected = await searchesService.GetProgramsBySearchTextAsync(searchModel);

            var counter = 0;

            foreach (var post in expected)
            {
                counter++;
            }

            Assert.Equal(1, counter);
        }
Exemplo n.º 26
0
        public async Task GetCommentsByProgramIdAsyncShouldReturnCommentsSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);

            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var commentsService = new CommentsService(dbContext, mapper, usersService);

            var programCategory = new ProgramCategory
            {
                Name        = CategoryName,
                Description = CategoryDescription,
            };

            await dbContext.ProgramCategories.AddAsync(programCategory);

            await dbContext.SaveChangesAsync();

            var program = new Program
            {
                Title       = ProgramTitle,
                Description = ProgramDescription,
                CategoryId  = programCategory.Id,
            };

            await dbContext.Programs.AddAsync(program);

            await dbContext.SaveChangesAsync();

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var comment = new Comment
            {
                Text      = CommentText,
                AuthorId  = user.Id,
                ProgramId = program.Id,
            };

            await dbContext.Comments.AddAsync(comment);

            await dbContext.SaveChangesAsync();

            var expected = await commentsService.GetCommentsByProgramIdAsync(program.Id);

            var counter = 0;

            foreach (var comm in expected)
            {
                counter++;
            }

            Assert.Equal(1, counter);
        }