public void ToEF_Successful() { //ARRANGE var instrument = new InstrumentTO { Id = 4, Name = "Saxophone" }; var libUser = new LibUserTO { Id = 1, IdentityUserId = 24 }; var category = new CategoryTO { Id = 2, Name = "Jazz" }; var sheet = new SheetTO { Id = 3, Name = "Sam's Dixie", Category = category, IsCurrent = true, IsIndependance = true, IsGarde = false, }; var sheetPart = new SheetPartTO { Id = 2, Instrument = instrument, Sheet = sheet, Path = "Path" }; //ACT var instrumentEF = instrument.ToEF(); var libUserEF = libUser.ToEF(); var categoryEF = category.ToEF(); var sheetEF = sheet.ToEF(); var sheetPartEF = sheetPart.ToEF(); //Assert Assert.AreEqual(instrument.Name, instrumentEF.Name); Assert.AreEqual(libUser.IdentityUserId, libUserEF.IdentityUserId); Assert.AreEqual(category.Name, categoryEF.Name); Assert.AreEqual(sheet.Name, sheetEF.Name); Assert.AreEqual(sheetPart.Sheet.Name, sheetPartEF.Sheet.Name); }
public InstrumentTO Update(InstrumentTO entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } if (entity.Id <= 0) { throw new ArgumentException("Instrument To Update Invalid Id"); } if (!libraryContext.Instruments.Any(x => x.Id == entity.Id)) { throw new KeyNotFoundException($"Update(InstrumentTO) Can't find instrument to update."); } var editedEntity = libraryContext.Instruments.FirstOrDefault(e => e.Id == entity.Id); if (editedEntity != default) { editedEntity.UpdateFromDetached(entity.ToEF()); } var tracking = libraryContext.Instruments.Update(editedEntity); tracking.State = EntityState.Detached; libraryContext.SaveChanges(); //return editedEntity.ToTransferObject(); return(tracking.Entity.ToTransferObject()); //return editedEntity.ToTransferObject(); }
public void ToTEF_ProvidingNull_ThrowException() { InstrumentTO instrument = null; LibUserTO libUser = null; CategoryTO category = null; SheetTO sheet = null; SheetPartTO sheetPart = null; //ACT Assert.ThrowsException <ArgumentNullException>(() => instrument.ToEF()); Assert.ThrowsException <ArgumentNullException>(() => libUser.ToEF()); Assert.ThrowsException <ArgumentNullException>(() => category.ToEF()); Assert.ThrowsException <ArgumentNullException>(() => sheet.ToEF()); Assert.ThrowsException <ArgumentNullException>(() => sheetPart.ToEF()); }
public InstrumentTO Add(InstrumentTO entity) { if (entity is null) { throw new ArgumentNullException(); } if (entity.Id != 0) { return(entity); } var entityEF = entity.ToEF(); var result = libraryContext.Instruments.Add(entityEF); libraryContext.SaveChanges(); return(result.Entity.ToTransferObject()); }