コード例 #1
0
ファイル: UnitTest1.cs プロジェクト: carloscadena/Cereal
        public async void DeleteBasketItemTest()
        {
            DbContextOptions <CerealDBContext> options =
                new DbContextOptionsBuilder <CerealDBContext>()
                .UseInMemoryDatabase("Baskets")
                .Options;

            using (CerealDBContext context = new CerealDBContext(options))
            {
                //CREATE
                BasketItems basket = new BasketItems();
                basket.ID        = 4;
                basket.ProductID = 2;
                basket.Quantity  = 2;
                basket.UserID    = "userid";
                basket.Purchased = false;

                context.BasketItems.Add(basket);

                await context.SaveChangesAsync();

                //READ
                var myBasket = await context.BasketItems.FirstOrDefaultAsync(r => r.ID == basket.ID);

                //DELETE
                context.BasketItems.Remove(myBasket);
                context.SaveChanges();

                var deletedBasketItems = await context.BasketItems.FirstOrDefaultAsync(r => r.ID == myBasket.ID);

                Assert.True(deletedBasketItems == null);
            }
        }
コード例 #2
0
ファイル: UnitTest1.cs プロジェクト: carloscadena/Cereal
        public async void UpdateBasketItemTest()
        {
            DbContextOptions <CerealDBContext> options =
                new DbContextOptionsBuilder <CerealDBContext>()
                .UseInMemoryDatabase("Baskets")
                .Options;

            using (CerealDBContext context = new CerealDBContext(options))
            {
                //CREATE
                BasketItems basket = new BasketItems();
                basket.ID        = 3;
                basket.ProductID = 2;
                basket.Quantity  = 2;
                basket.UserID    = "userid";
                basket.Purchased = false;

                context.BasketItems.Add(basket);

                await context.SaveChangesAsync();

                //READ
                var myBasket = await context.BasketItems.FirstOrDefaultAsync(r => r.ID == basket.ID);

                //UPDATE
                myBasket.Quantity = 4;
                context.Update(myBasket);
                context.SaveChanges();

                var newBasket = await context.BasketItems.FirstOrDefaultAsync(r => r.ID == myBasket.ID);

                Assert.Equal(4, newBasket.Quantity);
            }
        }