Ejemplo n.º 1
0
        public void GetProductsTest()
        {
            // Arrange

            // PM> Install-Package Microsoft.EntityFrameworkCore.InMemory
            var options = new DbContextOptionsBuilder <ShopContext>()
                          .UseInMemoryDatabase(databaseName: "getproducts")
                          .Options;

            using (var context = new ShopContext(options))
            {
                Generator.Generator generator = new Generator.Generator();
                context.Products.AddRange(generator.GetProducts(100));
                context.SaveChanges();
            }

            using (var context = new ShopContext(options))
            {
                IProductsService productsService = new DbProductsService(context);

                // Acts
                var products = productsService.Get();

                // Asserts
                Assert.NotEmpty(products);

                // PM> Install-Package FluentAssertions

                products.Should().NotBeNullOrEmpty();
            }
        }
Ejemplo n.º 2
0
        public void GetProductsTest()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ShopContext>()
                          .UseInMemoryDatabase(databaseName: "getproducts")
                          .Options;

            using (var context = new ShopContext(options))
            {
                Generator.Generator generator = new Generator.Generator();
                context.Products.AddRange(generator.GetProducts(100));
                context.SaveChanges();
            }
            using (var context = new ShopContext(options))
            {
                IProductsService productsService = new DbProductsService(context);

                //acts
                var products = productsService.Get();

                //asserts
                Assert.NotEmpty(products);

                products.Should().NotBeNullOrEmpty();
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            ContextFactory contextFactory = new DbServices.ContextFactory();
            ShopContext    context        = contextFactory.CreateDbContext(args);


            IProductsService productsService = new DbProductsService(context);

            //productsService.Remove(20);

            ProductSearchCriteria productSearch = new ProductSearchCriteria
            {
            };
            var colorProducts = productsService.Get(productSearch);

            //context.Database.EnsureDeleted();
            //context.Database.EnsureCreated();
            context.Database.Migrate();

            for (int i = 2; i < 10; i++)
            {
                //CreateOrder(context, $"ZA {i}");
            }

            //var customers = context.Customers.ToList();
            //var products = context.Products
            //    .Where(p => p.UnitPrice > 100)
            //    .OrderBy(p => p.Name)
            //    .Select(p => new { p.Name, p.Color })
            //    .ToList();

            //var productsByColor = context.Products
            //    .GroupBy(p => p.Color)
            //    .ToList();

            //var productsQuantityByColor = context.Products
            //    .GroupBy(p => p.Color)
            //    .Select(g => new { Color = g.Key, Quantity = g.Count() })
            //    .ToList();

            //string customers = configuration["Generator:Customers"];

            //CreateSampleData(context);

            //Console.WriteLine("Liczba klientów: " + customers);
            Console.WriteLine("DONE");
            Console.ReadKey();

            var tuple = GetTuple();
        }
Ejemplo n.º 4
0
        public void GetProductTest()
        {
            var options = new DbContextOptionsBuilder <ShopContext>()
                          .UseInMemoryDatabase(databaseName: "getproduct")
                          .Options;

            using (var context = new ShopContext(options))
            {
                IProductsService productsService = new DbProductsService(context);
                var product = productsService.Get(1);

                Assert.NotNull(product);
            }
        }
Ejemplo n.º 5
0
        public void GetProductTest()
        {
            var options = new DbContextOptionsBuilder <ShopContext>()
                          .UseInMemoryDatabase(databaseName: "getproducts")
                          .Options;

            //using (var context = new ShopContext(options))
            //{
            //    Generator.Generator generator = new Generator.Generator();
            //    context.Products.AddRange(generator.GetProducts(100));
            //    context.SaveChanges();
            //}

            using (var context = new ShopContext(options))
            {
                IProductsService productsService = new DbProductsService(context);

                // Acts
                var product = productsService.Get(1);

                // Asserts
                Assert.NotNull(product);
            }
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            // CreateOrderTest();

            ContextFactory contextFactory = new ContextFactory();
            ShopContext    context        = contextFactory.CreateDbContext(args);

            context.Database.Migrate();

            if (!context.Products.Any())
            {
                CreateSampleData(context);
            }

            for (int i = 2; i < 10; i++)
            {
                CreateOrder(context, $"ZA {i}");
            }


            var updateproducts = context.Products.OrderBy(p => p.Id).Take(10).ToList();

            context.Products.RemoveRange(updateproducts);
            context.SaveChanges();

            var product = context.Products.First();

            Console.WriteLine(context.Entry(product).State);

            product.UnitPrice = 400.00m;
            product.Color     = "red";

            Console.WriteLine(context.Entry(product).State);

            product.UnitPrice = 300.00m;

            Console.WriteLine(context.Entry(product).State);

            Console.WriteLine(context.Entry(product).Property(p => p.UnitPrice).OriginalValue);

            context.SaveChanges();

            Console.WriteLine(context.Entry(product).State);


            //  context.Products.Remove(product);
            Console.WriteLine(context.Entry(product).State);

            context.Entry(product).State = EntityState.Unchanged;

            context.SaveChanges();


            IProductsService productsService = new DbProductsService(context);

            productsService.Remove(20);

            ProductSearchCriteria searchCriteria = new ProductSearchCriteria
            {
            };

            var colorProducts = productsService.Get(searchCriteria);

            if (colorProducts.Any())
            {
            }

            //  context.Database.EnsureDeleted();
            // context.Database.EnsureCreated();

            context.Database.Migrate();

            var customers = context.Customers.ToList();

            var products = context.Products
                           .Where(p => p.UnitPrice > 100)
                           .Where(p => p.Name.StartsWith("N"))
                           .OrderBy(p => p.Name)
                           .ThenBy(p => p.Color)
                           .Select(p => new { p.Name, p.Color })
                           .ToList();


            var productsByColor = context.Products
                                  .GroupBy(p => p.Color)
                                  .ToList();

            var productsQtyByColor = context.Products
                                     .GroupBy(p => p.Color)
                                     .Select(g => new { Color = g.Key, Qty = g.Count() })
                                     .ToList();

            //var productsOver100 = new List<Product>();

            //foreach (var product in context.Products)
            //{
            //    if (product.UnitPrice > 100)
            //    {
            //        productsOver100.Add(product);
            //    }
            //}



            // string customers = configuration["Generator:Customers"];

            // Generowanie danych
            // CreateSampleData(context);

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();

            var tuple = GetTuple2();
        }
Ejemplo n.º 7
0
 private static void GetProductsTest()
 {
     IProductsService productsService = new DbProductsService();
     var products = productsService.Get();
 }