コード例 #1
0
        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());
        }
コード例 #2
0
ファイル: ExtensionsTests.cs プロジェクト: Orl4ne/MusicAgora
        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);
        }
コード例 #3
0
        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();
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
 public ActionResult DeleteInstrument(int id, InstrumentTO instrument)
 {
     try
     {
         _libraryUnitOfWork.InstrumentRepository.Delete(instrument);
         return(RedirectToAction(nameof(InstrumentsIndex)));
     }
     catch
     {
         return(View());
     }
 }
コード例 #7
0
ファイル: ExtensionsTests.cs プロジェクト: Orl4ne/MusicAgora
        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());
        }
コード例 #8
0
        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
            });
        }
コード例 #9
0
        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));
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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());
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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");
        }
コード例 #14
0
        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);
        }