public void DeleteMedia_Successful() { var connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MediaLibraryContext(connection)) { IRepo mediaRepo = new MediaRepo(context); //ARRANGE var media = new MediaTO { Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; var media2 = new MediaTO { Name = "C# pour les nulls", Type = MediaType.Book, Path = "C:/Book/Code" }; var media3 = new MediaTO { Name = "The IT Crowd", Type = MediaType.Serie, Path = "C:/Films/Series" }; var result = mediaRepo.CreateMedia(media); var result2 = mediaRepo.CreateMedia(media2); var result3 = mediaRepo.CreateMedia(media3); context.SaveChanges(); //ACT var test = mediaRepo.DeleteMedia(result); context.SaveChanges(); //ASSERT Assert.AreEqual(2, context.Medias.Count()); } }
public MediaTO ModifierMedia(MediaTO entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } if (entity.Id <= 0) { throw new ArgumentException("Media To Update Invalid Id"); } if (!context.Medias.Any(x => x.Id == entity.Id)) { throw new KeyNotFoundException($"Update(MediaTO) Can't find media to update."); } var editedEntity = context.Medias.FirstOrDefault(e => e.Id == entity.Id); if (editedEntity != default) { editedEntity = entity.ToEF(); } context.SaveChanges(); return(editedEntity.ToTransferObject()); }
public void ToTEF_ProvidingNull_ThrowException() { //ARRANGE MediaTO media = null; //ACT Assert.ThrowsException <ArgumentNullException>(() => media.ToEF()); }
public void ToTrackedEF_ProvidingNullEF_ThrowException() { //ARRANGE MediaTO media = null; MediaEF mediaToModify = null; //ACT Assert.ThrowsException <ArgumentNullException>(() => media.ToTrackedEF(mediaToModify)); }
public void ModifyMedia_ProvidingNonExistingId_ThrowException() { var connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MediaLibraryContext(connection)) { IRepo mediaRepo = new MediaRepo(context); var media = new MediaTO {Id=1450, Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; Assert.ThrowsException<KeyNotFoundException>(() => mediaRepo.ModifyMedia(media)); } }
public void ToTrackedEF_ProvidingNullTO_ThrowException() { //ARRANGE MediaTO media = null; var mediaToModify = new MediaEF { Id = 1, Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; //ACT Assert.ThrowsException <ArgumentNullException>(() => media.ToTrackedEF(mediaToModify)); }
public ActionResult Edit(int id, MediaTO media) { try { var temp = mediaRepo.ModifyMedia(media); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Delete(int id, MediaTO media) { try { mediaRepo.DeleteMedia(media); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Create(MediaTO media) { try { mediaRepo.CreateMedia(media); return(RedirectToAction("Index")); } catch { return(View()); } }
public void DeleteMedia_ProvidingNonExistingMedia_ThrowException() { var connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MediaLibraryContext(connection)) { IRepo mediaRepo = new MediaRepo(context); var media = new MediaTO { Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; Assert.ThrowsException <ArgumentException>(() => mediaRepo.DeleteMedia(media)); } }
public static MediaEF ToEF(this MediaTO media) { if (media is null) { throw new ArgumentNullException(nameof(media)); } return(new MediaEF { Id = media.Id, Name = media.Name, Path = media.Path, Type = media.Type }); }
public void ToEF_Successfull() { //ARRANGE var media = new MediaTO { Id = 1, Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; //ACT var result = media.ToEF(); //Assert Assert.AreEqual(media.Name, result.Name); Assert.AreEqual(media.Id, result.Id); Assert.AreEqual(media.Type, result.Type); Assert.AreEqual(media.Path, result.Path); }
public MediaTO CreerMedia(MediaTO entity) { if (entity is null) { throw new ArgumentNullException(); } if (entity.Id != 0) { return(entity); } var entityEF = context.Medias.Add(entity.ToEF()); context.SaveChanges(); return(entityEF.ToTransferObject()); }
public void GetMediaById_ProvidingNoId_ThrowException() { var connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MediaLibraryContext(connection)) { IRepo mediaRepo = new MediaRepo(context); //ARRANGE var media = new MediaTO { Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; //ASSERT Assert.ThrowsException <ArgumentException>(() => mediaRepo.GetById(media.Id)); } }
public bool DeleteMedia(MediaTO entity) { if (entity is null) { throw new KeyNotFoundException(); } if (entity.Id <= 0) { throw new ArgumentException("Media To Delete Invalid Id"); } var media = context.Medias.FirstOrDefault(x => x.Id == entity.Id); context.Medias.Remove(media); context.SaveChanges(); return(true); }
public static MediaEF ToTrackedEF(this MediaTO media, MediaEF mediaToModify) { if (mediaToModify is null) { throw new ArgumentNullException(nameof(mediaToModify)); } if (media is null) { throw new ArgumentNullException(nameof(media)); } mediaToModify.Id = media.Id; mediaToModify.Name = media.Name; mediaToModify.Type = media.Type; mediaToModify.Path = media.Path; return(mediaToModify); }
public void CreateMedia_AddExistingMedia_DoNotInsertTwiceInDb() { var connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MediaLibraryContext(connection)) { IRepo mediaRepo = new MediaRepo(context); var media = new MediaTO { Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; var addedMedia = mediaRepo.CreateMedia(media); context.SaveChanges(); mediaRepo.CreateMedia(addedMedia); context.SaveChanges(); Assert.AreEqual(1, context.Medias.Count()); } }
public bool SupprimerMedia(MediaTO entity) { var media = context.Medias.FirstOrDefault(x => x.Id == entity.Id); if (media is null) { throw new KeyNotFoundException(); } try { context.Medias.Remove(media); context.SaveChanges(); return(true); } catch (ArgumentException) { return(false); } }
public void ModifyMedia_Successful() { var connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MediaLibraryContext(connection)) { IRepo mediaRepo = new MediaRepo(context); //ARRANGE var media = new MediaTO { Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; var result = mediaRepo.CreateMedia(media); context.SaveChanges(); //ACT result.Name = "Cendrillon Remaster"; var test = mediaRepo.ModifyMedia(result); context.SaveChanges(); //ASSERT Assert.AreEqual("Cendrillon Remaster", test.Name); } }
public void CreateMedia_Successful() { var connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MediaLibraryContext(connection)) { IRepo mediaRepo = new MediaRepo(context); //ACT var media = new MediaTO { Name = "Cendrillon", Type = MediaType.Film, Path = "C:/Films/Animation" }; var result = mediaRepo.CreateMedia(media); context.SaveChanges(); //ASSERT Assert.IsNotNull(result); Assert.AreEqual("Cendrillon", result.Name); Assert.AreEqual(1, result.Id); } }