Пример #1
0
        public async Task GetCategoryByIdQueryHandler_WhenCalled_ReturnGetCategoryByIdLookupModel()
        {
            var returnModel = new GetCategoryByIdLookupModel
            {
                CategoryId = 1,
                Name       = "Category1"
            };

            _categoryServiceMock
            .Setup(x => x.GetCategoryByIdAsync(It.IsAny <int>(), CancellationToken.None))
            .ReturnsAsync(returnModel);

            var query = new GetCategoryByIdQuery {
                CategoryId = 1
            };

            var container = Registrations();

            await using var scope = container.BeginLifetimeScope();

            var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >();

            await using var context = new TechnicalTestDbContext(options);

            await SeedMockDataAsync(context);

            var handler = new GetCategoryByIdQueryHandler(_categoryServiceMock.Object);

            var result = await handler.Handle(query, CancellationToken.None);

            Assert.AreEqual("Category1", result.Name);
        }
 public void CleanData()
 {
     using (var context = new TechnicalTestDbContext())
     {
         var repository = new CompaniesRepository(context);
         var allItems   = repository.Get().ToList();
         allItems.ForEach(repository.Delete);
         context.SaveChanges();
     }
 }
Пример #3
0
        public async Task GetAllCategoriesAsync_WhenCalled_ReturnGetCategoryListReturnModel()
        {
            var container = Registrations();

            await using var scope = container.BeginLifetimeScope();

            var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >();

            await using var context = new TechnicalTestDbContext(options);

            await SeedMockDataAsync(context);

            var service = new CategoryService(context);

            var result = await service.GetAllCategoriesAsync(CancellationToken.None);

            Assert.That(result.Categories.Count.Equals(2));
        }
Пример #4
0
        public async Task GetProductByIdAsync_WhenFailed_ReturnNull()
        {
            var container = Registrations();

            await using var scope = container.BeginLifetimeScope();

            var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >();

            await using var context = new TechnicalTestDbContext(options);

            await SeedMockDataAsync(context);

            var service = new ProductService(context);

            var result = await service.GetProductByIdAsync(10, CancellationToken.None);

            Assert.That(result == null);
        }
Пример #5
0
        public async Task GetProductByIdAsync_WhenSucceded_ReturnGetProductByIdQueryLookupModel()
        {
            var container = Registrations();

            await using var scope = container.BeginLifetimeScope();

            var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >();

            await using var context = new TechnicalTestDbContext(options);

            await SeedMockDataAsync(context);

            var service = new ProductService(context);

            var result = await service.GetProductByIdAsync(1, CancellationToken.None);

            Assert.That(result.Name.Equals("Product1"));
        }
Пример #6
0
        public async Task CheckCategoryIdAsync_WhenCalled_ReturnBoolean()
        {
            var container = Registrations();

            await using var scope = container.BeginLifetimeScope();

            var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >();

            await using var context = new TechnicalTestDbContext(options);

            await SeedMockDataAsync(context);

            var service = new CheckDbService(context);

            var result = await service.CheckCategoryIdAsync(1, CancellationToken.None);

            Assert.That(result.Equals(true));
        }
Пример #7
0
        public async Task GetCategoyListQueryHandler_WhenCalled_ReturnGetCategoryByIdLookupModel()
        {
            var returnModel = new GetCategoryListReturnModel
            {
                Categories = new List <GetCategoryListLookupModel>
                {
                    new GetCategoryListLookupModel
                    {
                        CategoryId = 1,
                        Name       = "Category1"
                    },
                    new GetCategoryListLookupModel
                    {
                        CategoryId = 2,
                        Name       = "Category2"
                    }
                }
            };

            _categoryServiceMock
            .Setup(x => x.GetAllCategoriesAsync(CancellationToken.None))
            .ReturnsAsync(returnModel);

            var query = new GetCategoryListQuery();

            var container = Registrations();

            await using var scope = container.BeginLifetimeScope();

            var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >();

            await using var context = new TechnicalTestDbContext(options);

            await SeedMockDataAsync(context);

            var handler = new GetCategoyListQueryHandler(_categoryServiceMock.Object);

            var result = await handler.Handle(query, CancellationToken.None);

            Assert.AreEqual(2, result.Categories.Count);
            Assert.AreEqual("Category1", result.Categories[0].Name);
            Assert.AreEqual("Category2", result.Categories[1].Name);
        }
        public void GivenIHaveTheFollowingCompaniesWithDefaultInformation(Table table)
        {
            var items = table.CreateSet <CompanyDetailsSpecflowItem>();

            using (var context = new TechnicalTestDbContext())
            {
                var repository = new CompaniesRepository(context);
                var allItems   = repository.Get().ToList();
                allItems.ForEach(repository.Delete);

                items.ForEach(x =>
                {
                    repository.Insert(new Company {
                        Name = x.CompanyName
                    });
                });
                context.SaveChanges();
            }
        }
Пример #9
0
        public async Task GetProductListByCategoryIdQueryHandler_WhenCalled_ReturnGetCategoryByIdLookupModel()
        {
            var returnModel = new GetProductListByCategoryIdReturnModel
            {
                Products = new List <GetProductListByCategoryIdQueryLookupModel>
                {
                    new GetProductListByCategoryIdQueryLookupModel
                    {
                        ProductId = 1,
                        Name      = "Product1"
                    },
                    new GetProductListByCategoryIdQueryLookupModel
                    {
                        ProductId = 2,
                        Name      = "Product2"
                    }
                }
            };

            _productServiceMock
            .Setup(x => x.GetAllProductsByCategoryIdAsync(It.IsAny <int>(), CancellationToken.None))
            .ReturnsAsync(returnModel);

            var query = new GetProductListByCategoryIdQuery();

            var container = Registrations();

            await using var scope = container.BeginLifetimeScope();

            var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >();

            await using var context = new TechnicalTestDbContext(options);

            await SeedMockDataAsync(context);

            var handler = new GetProductListByCategoryIdQueryHandler(_productServiceMock.Object);

            var result = await handler.Handle(query, CancellationToken.None);

            Assert.AreEqual(2, result.Products.Count);
            Assert.AreEqual("Product1", result.Products[0].Name);
            Assert.AreEqual("Product2", result.Products[1].Name);
        }
Пример #10
0
        protected async Task SeedMockDataAsync(TechnicalTestDbContext context)
        {
            context.Database.EnsureCreated();

            await context.Categories.AddRangeAsync(
                new Categories
            {
                CategoryId = 1, Name = "Category1", Deleted = false
            },
                new Categories
            {
                CategoryId = 2, Name = "Category2", Deleted = false
            }
                );

            await context.Products.AddRangeAsync(
                new Products
            {
                ProductId  = 1,
                Name       = "Product1",
                CategoryId = 1
            },
                new Products
            {
                ProductId  = 2,
                Name       = "Product2",
                CategoryId = 2
            },
                new Products
            {
                ProductId  = 3,
                Name       = "Product3",
                CategoryId = 1
            }
                );

            context.SaveChanges();
        }
Пример #11
0
 protected Repository(TechnicalTestDbContext context)
 {
     _context = context;
     _dbSet   = _context.Set <T>();
 }
 public CompaniesRepository(TechnicalTestDbContext context) : base(context)
 {
 }