/// <summary> /// Добавить магазин в БД /// </summary> /// <param name="shop_"></param> public Shop Create(IShop shop_) { var shop = EntitiesFactory.Get().CreateShop(shop_.Id, shop_.Name, shop_.Address); var sh = _dbContext.Shop.Add(shop); _dbContext.SaveChanges(); return(sh); }
/// <summary> /// Добавить группу в БД /// </summary> /// <param name="group_"></param> public Group Create(IGroup group_) { var group = EntitiesFactory.Get().CreateGroup(group_.Id, group_.IdParent, group_.Name); var g = _dbContext.Group.Add(group); _dbContext.SaveChanges(); return(g); }
/// <summary> /// Добавить товар в БД /// </summary> /// <param name="item_"></param> public Item Create(IItem item_) { var item = EntitiesFactory.Get().CreateItem(item_.Id, item_.GId, item_.Name); var it = _dbContext.Item.Add(item); _dbContext.SaveChanges(); return(it); }
/// <summary> /// Задать поведение моку /// </summary> private void setPurchaseBehavior() { var fBus = EntitiesFactory.Get(); //= new Mock<IExpensesRepository>(); //Список покупок _purchRepMock.Setup(m => m.Entities).Returns(_purchaseList.AsQueryable()); //Добавить новую покупку _purchRepMock.Setup <Purchase>(m => m.Create( It.IsAny <IPurchase>())).Returns( (IPurchase purchase_) => { var purchase = fBus.CreatePurchase( _purchaseList.Max(p => p.Id) + 1, purchase_.Shop_Id, purchase_.Item_Id, purchase_.Price, purchase_.Count, purchase_.Date); _purchaseList.Add(purchase); _purchRepMock.Setup(m => m.Entities).Returns(_purchaseList.AsQueryable()); return(purchase); }); //Редактировать существующую покупку _purchRepMock.Setup(m => m.Update( It.IsAny <IPurchase>())).Callback( (IPurchase purchase_) => { var purchase = _purchaseList.Where(p => p.Id == purchase_.Id).First(); if (purchase == null) { return; } purchase.Item_Id = purchase_.Item_Id; purchase.Shop_Id = purchase_.Shop_Id; purchase.Price = purchase_.Price; purchase.Count = purchase_.Count; purchase.Date = purchase_.Date; }); //Удалить покупку _purchRepMock.Setup(m => m.Delete(It.IsAny <IPurchase>())) .Callback( (IPurchase purchase_) => { _purchaseList.RemoveAll(p => p.Id == purchase_.Id); _purchRepMock.Setup(m => m.Entities).Returns(_purchaseList.AsQueryable()); }); //Получить все покупки за день _purchRepMock.Setup(m => m.SelectPurchaseByBeginAndEndDates(It.IsAny <DateTime>(), It.IsAny <DateTime>())).Returns( (DateTime bDate_, DateTime eDate_) => { return(SelectPurchasesByBeginAndEndDates(bDate_, eDate_)); }); //Вернуть все доступные годы покупок _purchRepMock.Setup(m => m.SelectAllYears()).Returns(() => SelectAllYears()); }
/// <summary> /// Добавить покупку в БД /// </summary> /// <param name="purchase_"></param> public Purchase Create(IPurchase purchase_) { var purchase = EntitiesFactory.Get().CreatePurchase( purchase_.Id, purchase_.Shop_Id, purchase_.Item_Id, purchase_.Price, purchase_.Count, purchase_.Date ); var purch = _dbContext.Purchase.Add(purchase); _dbContext.SaveChanges(); return(purch); }
/// <summary> /// Установить для мока поведение для работы с магазинами /// </summary> private void setShopBehavior() { var fBus = EntitiesFactory.Get(); _shRepMock.SetupGet(m => m.Entities).Returns ( () => _shopsList.AsQueryable() ); //Текущий магазин(get) _shRepMock.SetupGet(m => m.CurrentShopId).Returns( () => _currentShop); //Текущий магазин(set) _shRepMock.SetupSet(m => m.CurrentShopId = It.IsAny <int?>()).Callback( (int?shopId_) => { _currentShop = shopId_; _shRepMock.SetupGet(m => m.CurrentShopId).Returns( () => _currentShop); }); //Добавить новый магазин _shRepMock.Setup <Shop>(m => m.Create(It.IsAny <IShop>())) .Returns((IShop shop_) => { var shop = fBus.CreateShop(_shopsList.Max(sh => sh.Id) + 1, shop_.Name, shop_.Address); _shopsList.Add(shop); _shRepMock.Setup(m => m.Entities).Returns(_shopsList.AsQueryable()); return(shop); }); //Редактировать существующий магазин _shRepMock.Setup(m => m.Update(It.IsAny <IShop>())) .Callback((IShop shop_) => { var shop = _shopsList.Where(sh => sh.Id == shop_.Id).FirstOrDefault(); if (shop == null) { return; } shop.Name = shop_.Name; shop.Address = shop_.Address; }); //Удалить магазин _shRepMock.Setup(m => m.Delete(It.IsAny <IShop>())) .Callback( (IShop shop_) => { _shopsList.RemoveAll(sh => sh.Id == shop_.Id); _shRepMock.Setup(m => m.Entities).Returns(_shopsList.AsQueryable()); }); }
private void setItemBehavior() { var fBus = EntitiesFactory.Get(); //Текущий товар(get) _itRepMock.SetupGet(m => m.CurrentIId).Returns( () => _currentItemId); //Текущий товар(set) _itRepMock.SetupSet(m => m.CurrentIId = It.IsAny <int?>()).Callback( (int?itemId_) => { _currentItemId = itemId_; _itRepMock.SetupGet(m => m.CurrentIId).Returns( () => _currentItemId); }); //Товары _itRepMock.SetupGet(m => m.Entities).Returns(_itemList.AsQueryable()); //Добавить новый товар _itRepMock.Setup <Item>(m => m.Create(It.IsAny <IItem>())) .Returns((IItem item_) => { var item = fBus.CreateItem(_itemList.Max(it => it.Id) + 1, item_.GId, item_.Name); _itemList.Add(item); _itRepMock.Setup(m => m.Entities).Returns(_itemList.AsQueryable()); return(item); }); //Редактировать существующий товар _itRepMock.Setup(m => m.Update(It.IsAny <IItem>())) .Callback((IItem shop_) => { var item = _itemList.Where(it => it.Id == shop_.Id).FirstOrDefault(); if (item == null) { return; } item.GId = shop_.GId; item.Name = shop_.Name; }); //Удалить товар _itRepMock.Setup(m => m.Delete(It.IsAny <IItem>())) .Callback( (IItem item_) => { _itemList.RemoveAll(it => it.Id == item_.Id); _itRepMock.Setup(m => m.Entities).Returns(_itemList.AsQueryable()); }); }
/// <summary> /// Создать Moq с товарами/группами/магазинами /// </summary> private void defineMockDbContext() { var fBus = EntitiesFactory.Get(); MockExpensesRep = new Mock <IExpensesRepository>(); //репозиторий товаров MockExpensesRep.Setup(m => m.ItemRep).Returns(ItemRepMock.Get().Mock.Object); //репозиторий групп товаров MockExpensesRep.Setup(m => m.GroupRep).Returns(GroupRepMock.Get().GpRepMock.Object); //репозиторий магазинов MockExpensesRep.Setup(m => m.ShopRep).Returns(ShopRepMock.Get().Mock.Object); //репозиторий покупок MockExpensesRep.Setup(m => m.PurchaseRep).Returns(PurchaseRepMock.Get().Mock.Object); }
private void setGroupBehavior() { var fBus = EntitiesFactory.Get(); //Текущая группа(get) _gpRepMock.SetupGet(m => m.CurrentGId).Returns( () => _currentGId); //Текущая группа(set) _gpRepMock.SetupSet(m => m.CurrentGId = It.IsAny <int?>()).Callback( (int?gId_) => { _currentGId = gId_; _gpRepMock.SetupGet(m => m.CurrentGId).Returns( () => _currentGId); }); //Группы товаров _gpRepMock.SetupGet(m => m.Entities).Returns(_groupsList.AsQueryable()); //Группы товаров, с расширенными названиями _gpRepMock.SetupGet(m => m.GroupExt).Returns(_groupsExtList.AsQueryable()); //Добавить новую группу _gpRepMock.Setup <Group>(m => m.Create(It.IsAny <IGroup>())) .Returns((IGroup group_) => { var group = fBus.CreateGroup(_groupsList.Max(it => it.Id) + 1, group_.IdParent, group_.Name); _groupsList.Add(group); _groupsExtList.Add(group); _gpRepMock.Setup(m => m.Entities).Returns(_groupsList.AsQueryable()); _gpRepMock.Setup(m => m.Entities).Returns(_groupsExtList.AsQueryable()); return(group); }); //Редактировать существующую группу товаров _gpRepMock.Setup(m => m.Update(It.IsAny <IGroup>())) .Callback((IGroup group_) => { var group = _groupsList.Where(gp => gp.Id == group_.Id).FirstOrDefault(); if (group == null) { return; } group.IdParent = group_.IdParent; group.Name = group_.Name; group = _groupsExtList.Where(gp => gp.Id == group_.Id).FirstOrDefault(); if (group == null) { return; } group.IdParent = group_.IdParent; group.Name = group_.Name; }); //Удалить группу товаров _gpRepMock.Setup(m => m.Delete(It.IsAny <IGroup>())) .Callback( (IGroup group_) => { _groupsList.RemoveAll(gp => gp.Id == group_.Id); _groupsExtList.RemoveAll(gp => gp.Id == group_.Id); _gpRepMock.Setup(m => m.Entities).Returns(_groupsList.AsQueryable()); _gpRepMock.Setup(m => m.Entities).Returns(_groupsExtList.AsQueryable()); }); }