public async Task CreateAsyncShouldCreateCategorySuccessfully()
        {
            await _categoriesService.CreateAsync("Traditional");

            Assert.Equal(1, _categoriesRepository.All().Count());
            Assert.Equal("Traditional", _categoriesRepository.All().First().Name);
        }
예제 #2
0
        public async Task CreateMethodShouldAddRightCategoryInDatabase()
        {
            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new ForumDbContext(options);
            var dateTimeProvider = new Mock <IDateTimeProvider>();

            dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            var categoriesService = new CategoriesService(db, null, dateTimeProvider.Object);
            await categoriesService.CreateAsync("Test");

            var expected = new Category
            {
                Id        = 1,
                Name      = "Test",
                CreatedOn = dateTimeProvider.Object.Now(),
                IsDeleted = false
            };

            var actual = await db.Categories.FirstOrDefaultAsync();

            actual.Should().BeEquivalentTo(expected);
        }
예제 #3
0
        public async Task DeleteByIdAsync_WithCorrectData_ShouldSuccessfullyDelete()
        {
            var testName = "TestName";

            // Arrange
            var context            = ApplicationDbContextInMemoryFactory.InitializeContext();
            var categoryRepository = new EfDeletableEntityRepository <Category>(context);
            var categoriesService  = new CategoriesService(categoryRepository);

            var inputModel = new CreateCategoryInputModel()
            {
                Name = testName,
            };

            await categoriesService.CreateAsync(inputModel);

            var category = categoryRepository.All().FirstOrDefault(c => c.Name == testName);

            // Act
            var expectedCateogiesCount = 0;
            await categoriesService.DeleteByIdAsync(category.Id);

            var actualCategoriesCount = categoryRepository.All().Count();

            // Assert
            Assert.Equal(expectedCateogiesCount, actualCategoriesCount);
        }
예제 #4
0
        public async Task EditAsync_WithCorrectData_ShouldSuccessfullyEdit()
        {
            var testName = "TestName";

            // Arrange
            var context            = ApplicationDbContextInMemoryFactory.InitializeContext();
            var categoryRepository = new EfDeletableEntityRepository <Category>(context);
            var categoriesService  = new CategoriesService(categoryRepository);

            var inputModel = new CreateCategoryInputModel()
            {
                Name = testName,
            };

            await categoriesService.CreateAsync(inputModel);

            var category = categoryRepository.All().FirstOrDefault(c => c.Name == testName);

            // Act
            var expectedCategoryName = "Edited_TestName";
            var editInputModel       = new CategoryInfoViewModel()
            {
                Id   = category.Id,
                Name = expectedCategoryName,
            };
            await categoriesService.EditAsync(editInputModel);

            var actualCategoryName = category.Name;

            // Assert
            category = await categoryRepository.GetByIdWithDeletedAsync(category.Id);

            Assert.Equal(expectedCategoryName, actualCategoryName);
        }
예제 #5
0
        public async Task GetIdByName_WithCorrectData_ShouldReturnId()
        {
            var testName = "TestName";

            // Arrange
            var context            = ApplicationDbContextInMemoryFactory.InitializeContext();
            var categoryRepository = new EfDeletableEntityRepository <Category>(context);
            var categoriesService  = new CategoriesService(categoryRepository);

            var inputModel = new CreateCategoryInputModel()
            {
                Name = testName,
            };

            await categoriesService.CreateAsync(inputModel);

            var category = categoryRepository.All().FirstOrDefault(c => c.Name == testName);

            // Act
            var expectedCategoryId = category.Id;
            var actualCategoryId   = categoriesService.GetIdByName(testName);

            // Assert
            Assert.Equal(expectedCategoryId, actualCategoryId);
        }
예제 #6
0
        public async Task GetCategoryById_WithCorrectData_ShouldReturnCorrectResult()
        {
            var testName = "CategoryTestName";

            // Arrange
            var context            = ApplicationDbContextInMemoryFactory.InitializeContext();
            var categoryRepository = new EfDeletableEntityRepository <Category>(context);
            var categoriesService  = new CategoriesService(categoryRepository);

            var inputModel = new CreateCategoryInputModel()
            {
                Name = testName,
            };

            await categoriesService.CreateAsync(inputModel);

            var category = categoryRepository.All().FirstOrDefault(x => x.Name == testName);

            // Act
            var expectedCategoryId = category.Id;
            var expectedReturnType = typeof(CategoryInfoViewModel);
            var actualReturnType   = categoriesService.GetCategoryByName(testName).GetType();
            var actualCategoryId   = categoriesService.GetCategoryByName(testName).Id;

            // Assert
            Assert.True(expectedReturnType == actualReturnType);
            Assert.Equal(expectedCategoryId, actualCategoryId);
        }
예제 #7
0
        public async Task <IActionResult> CreateCategory([FromBody] Category model)
        {
            var response = await _categoriesService.CreateAsync(model);

            if (response.IsSuccess)
            {
                return(Ok(response));
            }
            return(BadRequest(response));
        }
        public async Task CreateMethodShouldAddNewCategorie()
        {
            var list     = new List <Category>();
            var mockRepo = new Mock <IDeletableEntityRepository <Category> >();

            mockRepo.Setup(x => x.AddAsync(It.IsAny <Category>())).Callback((Category category) => list.Add(category));
            var service = new CategoriesService(mockRepo.Object);
            await service.CreateAsync("Cardio", "Cardio", "Description", "urlUrlurl");

            Assert.Single(list);
        }
        public async Task TestCreateAsyncCategory()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository      = new EfDeletableEntityRepository <Category>(new ApplicationDbContext(options.Options));
            var categoryService = new CategoriesService(repository);
            await categoryService.CreateAsync("test", "test", "test");

            AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly);
            Assert.Equal(1, repository.All().Count());
        }
        public void TestCreateAsync()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());

            var repository = new EfDeletableEntityRepository <Category>(new ApplicationDbContext(options.Options));
            var service    = new CategoriesService(repository);

            service.CreateAsync("test").GetAwaiter().GetResult();

            var counter = repository.All().Count();

            Assert.Equal(1, counter);
        }
예제 #11
0
        public async Task CreateMethodShouldAddCategoryInDatabase()
        {
            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new ForumDbContext(options);
            var dateTimeProvider = new Mock <IDateTimeProvider>();

            dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            var categoriesService = new CategoriesService(db, null, dateTimeProvider.Object);
            await categoriesService.CreateAsync("Test");

            db.Categories.Should().HaveCount(1);
        }
예제 #12
0
        public async Task CreateAsyncWorksCorrectly()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <Category>(dbContext);

            var service = new CategoriesService(repository);

            await service.CreateAsync(new CategoryServiceModel());

            var categoriesInDbCount = repository.All().ToList().Count();

            Assert.Equal(1, categoriesInDbCount);
        }
예제 #13
0
        public async Task CreateAsync_ShouldSuccessfullyCreate()
        {
            // Arrange
            var context            = ApplicationDbContextInMemoryFactory.InitializeContext();
            var categoryRepository = new EfDeletableEntityRepository <Category>(context);
            var categoriesService  = new CategoriesService(categoryRepository);

            var inputModel = new CreateCategoryInputModel()
            {
                Name = "TestName",
            };

            // Act
            var expectedCategoriesCount = 1;
            await categoriesService.CreateAsync(inputModel);

            var actualCategoriesCount = categoryRepository.All().Count();

            // Assert
            Assert.Equal(expectedCategoriesCount, actualCategoriesCount);
        }