public async void GetAllOptionByProductIdAsync_ShouldGetOptions()
        {
            // Arrange
            //create in memory options
            var options = new DbContextOptionsBuilder <DataBaseContext>()
                          .UseInMemoryDatabase(databaseName: "GetAllOptionByProductIdAsync_ShouldGetOptions")
                          .Options;

            using (var context = new DataBaseContext(options))
            {
                await context.AddRangeAsync(optionsList);

                await context.SaveChangesAsync();

                Assert.Equal(2, await context.ProductOption.CountAsync());
            }

            // Act
            using (var context = new DataBaseContext(options))
            {
                var repo = new OptionRepository(context);

                var result = await repo.GetAllOptionByProductIdAsync(firstOption.ProductId);

                Assert.Equal(1, result.Count);

                Assert.Equal(firstOption.Id, result[0].Id);
            }
        }
Example #2
0
        public async Task Process(RequestMetric metrics)
        {
            IServiceScope   scope            = _serviceProvider.CreateScope();
            DataBaseContext _dataBaseContext = scope.ServiceProvider.GetRequiredService <DataBaseContext>();
            await _dataBaseContext.AddAsync(metrics);

            await _dataBaseContext.SaveChangesAsync();

            foreach (MiddlewareMetric m in metrics.MiddlewareMetrics)
            {
                m.RequestMetricId = metrics.Id;
                m.Id = null;
            }
            await _dataBaseContext.AddRangeAsync(metrics.MiddlewareMetrics);

            await _dataBaseContext.SaveChangesAsync();

            scope.Dispose();
        }
        public async void UpdateOptionAsync_ShouldUpdateOption()
        {
            // Arrange
            //create in memory options
            var options = new DbContextOptionsBuilder <DataBaseContext>()
                          .UseInMemoryDatabase(databaseName: "UpdateOptionAsync_ShouldUpdateOption")
                          .Options;

            using (var context = new DataBaseContext(options))
            {
                await context.AddRangeAsync(optionsList);

                await context.SaveChangesAsync();

                Assert.Equal(2, await context.ProductOption.CountAsync());
            }

            firstOption.Name        = "updated name";
            firstOption.Description = "updated des";

            // Act
            using (var context = new DataBaseContext(options))
            {
                var repo = new OptionRepository(context);

                var result = await repo.UpdateOptionAsync(firstOption);

                Assert.Equal(firstOption.Id, result.Id);
            }

            using (var context = new DataBaseContext(options))
            {
                var updatedoption = await context.ProductOption.FirstAsync(o => o.Id == firstOption.Id);

                Assert.Equal(firstOption.Name, updatedoption.Name);
                Assert.Equal(firstOption.Description, updatedoption.Description);
            }
        }
        public async void IsOptionExistedById_ShouldReturnBool()
        {
            // Arrange
            //create in memory options
            var options = new DbContextOptionsBuilder <DataBaseContext>()
                          .UseInMemoryDatabase(databaseName: "IsOptionExistedById_ShouldReturnBool")
                          .Options;

            using (var context = new DataBaseContext(options))
            {
                await context.AddRangeAsync(optionsList);

                await context.SaveChangesAsync();

                Assert.Equal(2, await context.ProductOption.CountAsync());
            }

            // Act
            using (var context = new DataBaseContext(options))
            {
                var repo = new OptionRepository(context);

                var result = await repo.IsOptionExistedById(firstOption.Id);

                Assert.Equal(true, result);
            }

            using (var context = new DataBaseContext(options))
            {
                var repo = new OptionRepository(context);

                var result = await repo.IsOptionExistedById(Guid.NewGuid());

                Assert.Equal(false, result);
            }
        }