public void AddInstrument_AddExistingInstrument_DoNotInsertTwiceInDb() { //Arrange var options = new DbContextOptionsBuilder <LibraryContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new LibraryContext(options); IInstrumentRepository instrumentRepository = new InstrumentRepository(context); //Act var instru = new InstrumentTO { Name = "Saxophone" }; var instru2 = new InstrumentTO { Id = 2, Name = "Saxophone" }; var result = instrumentRepository.Add(instru); var result2 = instrumentRepository.Add(instru2); context.SaveChanges(); //Assert Assert.IsNotNull(result); Assert.AreEqual(1, instrumentRepository.GetAll().Count()); }
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 UpdateInstrument_Successful() { //Arrange var options = new DbContextOptionsBuilder <LibraryContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new LibraryContext(options); IInstrumentRepository instrumentRepository = new InstrumentRepository(context); var instru = new InstrumentTO { Name = "Saxophone" }; var instru2 = new InstrumentTO { Name = "Trumpet" }; var instru3 = new InstrumentTO { Name = "Flute" }; var AddedInstru = instrumentRepository.Add(instru); var AddedInstru2 = instrumentRepository.Add(instru2); var AddedInstru3 = instrumentRepository.Add(instru3); context.SaveChanges(); //Act AddedInstru.Name = "PouetPouet"; var test = instrumentRepository.Update(AddedInstru); context.SaveChanges(); //Assert Assert.AreEqual(3, instrumentRepository.GetAll().Count()); Assert.AreEqual("PouetPouet", test.Name); }
public void GetInstrumentById_Successful() { //Arrange var options = new DbContextOptionsBuilder <LibraryContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new LibraryContext(options); IInstrumentRepository instrumentRepository = new InstrumentRepository(context); var instru = new InstrumentTO { Name = "Saxophone" }; var instru2 = new InstrumentTO { Name = "Trumpet" }; var instru3 = new InstrumentTO { Name = "Flute" }; var AddedInstru = instrumentRepository.Add(instru); var AddedInstru2 = instrumentRepository.Add(instru2); var AddedInstru3 = instrumentRepository.Add(instru3); context.SaveChanges(); //Act var result = instrumentRepository.GetById(1); var result2 = instrumentRepository.GetById(2); var result3 = instrumentRepository.GetById(3); //Assert Assert.AreEqual("Saxophone", result.Name); Assert.AreEqual("Trumpet", result2.Name); Assert.AreEqual("Flute", result3.Name); }
public ActionResult DeleteInstrument(int id, InstrumentTO instrument) { try { _libraryUnitOfWork.InstrumentRepository.Delete(instrument); return(RedirectToAction(nameof(InstrumentsIndex))); } catch { return(View()); } }
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 List <InstrumentTO> MockInstruments() { var instru = new InstrumentTO { Id = 1, Name = "Saxophone" }; var instru2 = new InstrumentTO { Id = 2, Name = "Trumpet" }; var instru3 = new InstrumentTO { Id = 3, Name = "Flute" }; return(new List <InstrumentTO> { instru, instru2, instru3 }); }
public void UpdateInstrument_ProvidingNonExistingCategory_ThrowException() { var options = new DbContextOptionsBuilder <LibraryContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new LibraryContext(options); IInstrumentRepository instrumentRepository = new InstrumentRepository(context); var instru = new InstrumentTO { Name = "Saxophone" }; //Act & Assert Assert.ThrowsException <ArgumentException>(() => instrumentRepository.Update(instru)); }
public bool Delete(InstrumentTO entity) { if (entity is null) { throw new KeyNotFoundException(); } if (entity.Id <= 0) { throw new ArgumentException("Instrument To Delete Invalid Id"); } var instrument = libraryContext.Instruments.FirstOrDefault(x => x.Id == entity.Id); libraryContext.Instruments.Remove(instrument); libraryContext.SaveChanges(); return(true); }
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()); }
public static InstrumentEF ToEF(this InstrumentTO Instrument) { if (Instrument is null) { throw new ArgumentNullException(nameof(Instrument)); } var instruEf = new InstrumentEF { Id = Instrument.Id, Name = Instrument.Name, }; instruEf.UserInstruments = Instrument.LibUserIds?.Select(x => new UserInstruEF { InstrumentId = instruEf.Id, LibUserId = x, }).ToList(); return(instruEf); }
public void AddInstrument_Successful() { //Arrange var options = new DbContextOptionsBuilder <LibraryContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new LibraryContext(options); IInstrumentRepository instrumentRepository = new InstrumentRepository(context); //Act var instru = new InstrumentTO { Name = "Saxophone" }; var result = instrumentRepository.Add(instru); context.SaveChanges(); //Assert Assert.IsNotNull(result); Assert.AreEqual(result.Name, "Saxophone"); }
public void UpdateSheetPart_Successful() { var options = new DbContextOptionsBuilder <LibraryContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new LibraryContext(options); ISheetRepository sheetRepository = new SheetRepository(context); ICategoryRepository categoryRepository = new CategoryRepository(context); ISheetPartRepository sheetPartRepository = new SheetPartRepository(context); IInstrumentRepository instrumentRepository = new InstrumentRepository(context); //Act var category = new CategoryTO { Name = "Musique de films" }; var addedCategory = categoryRepository.Add(category); var category2 = new CategoryTO { Name = "Musique de classique" }; var addedCategory2 = categoryRepository.Add(category2); context.SaveChanges(); var sheet = new SheetTO { Name = "BestOf", Arranger = "Jean-Luc", Category = addedCategory, Composer = "Morricone", IsCurrent = false, IsGarde = false, IsIndependance = true }; var sheet2 = new SheetTO { Name = "Youg Amadeus", Arranger = "Jan de Haan", Category = addedCategory2, Composer = "Mozart", IsCurrent = true, IsGarde = false, IsIndependance = true }; var addedSheet = sheetRepository.Add(sheet); var addedSheet2 = sheetRepository.Add(sheet2); context.SaveChanges(); var instru = new InstrumentTO { Name = "Saxophone" }; var instru2 = new InstrumentTO { Name = "Trumpet" }; var instru3 = new InstrumentTO { Name = "Flute" }; var addedInstru = instrumentRepository.Add(instru); var addedInstru2 = instrumentRepository.Add(instru2); var addedInstru3 = instrumentRepository.Add(instru3); context.SaveChanges(); var sheetPart = new SheetPartTO { Instrument = addedInstru, Sheet = addedSheet, Path = @"C:\sheet" }; var sheetPart2 = new SheetPartTO { Instrument = addedInstru2, Sheet = addedSheet }; var sheetPart3 = new SheetPartTO { Instrument = addedInstru, Sheet = addedSheet2 }; var addedSheetPart = sheetPartRepository.Add(sheetPart); var addedSheetPart2 = sheetPartRepository.Add(sheetPart2); var addedSheetPart3 = sheetPartRepository.Add(sheetPart3); context.SaveChanges(); //ACT addedSheetPart.Instrument = addedInstru3; var test = sheetPartRepository.Update(addedSheetPart); context.SaveChanges(); //Assert Assert.AreEqual(3, sheetPartRepository.GetAll().Count()); Assert.AreEqual("Flute", test.Instrument.Name); }