/// <summary> /// Inserts an address entity in the database /// </summary> /// <param name="address">The address entity to be inserted</param> /// <returns>The same address entity with its new ID assigned</returns> /// <exception cref="ArgumentNullException">If address parameter is null</exception> /// <remarks>Not tested</remarks> public int Insert(Address address) { if (address == null) { throw new ArgumentNullException(nameof(address), "Address object can't be null"); } _db.Add(address); var changedRows = _db.SaveChanges(); return(changedRows); }
/// <summary> /// Inserts an author entity in the database. /// </summary> /// <param name="author">An author entity instance</param> /// <returns>The updated author entity</returns> /// <exception cref="ArgumentNullException">If the author entity instance is null</exception> public int Insert(Author author) { if (author == null) { throw new ArgumentNullException(nameof(author), "Author object can't be null"); } _db.Add(author); var changedRows = _db.SaveChanges(); return(changedRows); }
public void FindAllReturnsAPopulatedList() { // setup var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; using (var context = new GTLContext(options)) { var material = new Material { Isbn = _isbn, Title = _title, Description = _description, Lendable = _lendable, Language = _language, Type = _materialType, }; context.Materials.Add(material); context.SaveChanges(); } // action using (var context = new GTLContext(options)) { var controller = ControllerFactory.CreateMaterialController(context); var materialList = controller.FindAll(); Assert.That(materialList, Is.Not.Empty); } }
public void DeleteDeletesCorrectly() { var methodName = MethodBase.GetCurrentMethod().Name; var options = new DbContextOptionsBuilder <GTLContext>().UseInMemoryDatabase(methodName).Options; // add volumes to db using (var context = new GTLContext(options)) { context.AddRange(volumes); context.SaveChanges(); } // delete volume using (var context = new GTLContext(options)) { var volumeController = ControllerFactory.CreateVolumeController(context); var volumeToDelete = volumes[1]; volumeController.Delete(volumeToDelete); var fetchedVolume = context.Volumes.Find(volumeToDelete.VolumeId); var fetchedVolumes = context.Volumes.ToList(); Assert.Multiple(() => { // assert that the fetched volume is null - deleted from db Assert.That(fetchedVolume, Is.Null); // assert that number of volumes decreased by 1 Assert.AreEqual(fetchedVolumes.Count, volumes.Count - 1); }); } }
public void FindByIdFindsAnExistingAuthor() { // setup int authorId; var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; // action using (var context = new GTLContext(options)) { var author = new Author { FirstName = authorFirstName, LastName = authorLastName }; context.Add(author); context.SaveChanges(); authorId = author.AuthorId; } // assertion using (var context = new GTLContext(options)) { var authorController = ControllerFactory.CreateAuthorController(context); var fetchedAuthor = authorController.FindByID(authorId); Assert.That(fetchedAuthor, Has .Property(nameof(Author.FirstName)).EqualTo(authorFirstName).And .Property(nameof(Author.LastName)).EqualTo(authorLastName)); } }
public void CreateCreatesCorrectAddressInstance() { // setup var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; using (var context = new GTLContext(options)) { context.Add(zip); context.SaveChanges(); } // action using (var context = new GTLContext(options)) { var addressController = ControllerFactory.CreateAddressController(context); var address = addressController.Create(street, additionalInfo, zip.Code); // assertion Assert.That(address, Has .Property(nameof(Address.Street)).EqualTo(street).And .Property(nameof(Address.AdditionalInfo)).EqualTo(additionalInfo).And .Property(nameof(Address.Zip))); } }
public void CreateCreatesCorrectly() { // setup var bookType = new MaterialType { Type = "Book" }; var programmingSubject = new MaterialSubject { SubjectName = "Programming" }; var author = new Author { FirstName = "Nikola", LastName = "Velichkov" }; var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; using (var context = new GTLContext(options)) { context.Add(bookType); context.Add(programmingSubject); context.Add(author); context.SaveChanges(); } // action using (var context = new GTLContext(options)) { var controller = ControllerFactory.CreateMaterialController(context); var material = controller.Create( _isbn, _title, _language, _lendable, _description, bookType, new List <MaterialSubject> { programmingSubject }, new List <Author> { author } ); // assertion Assert.That(material, Has .Property(nameof(Material.Isbn)).EqualTo(_isbn).And .Property(nameof(Material.Title)).EqualTo(_title).And .Property(nameof(Material.Language)).EqualTo(_language).And .Property(nameof(Material.Lendable)).EqualTo(_lendable).And .Property(nameof(Material.Description)).EqualTo(_description).And .Property(nameof(Material.Type)).And .Property(nameof(Material.MaterialSubjects)).And .Property(nameof(Material.MaterialAuthors))); } }
public void CreateCreatesCorrectly() { var methodName = MethodBase.GetCurrentMethod().Name; var options = new DbContextOptionsBuilder <GTLContext>().UseInMemoryDatabase(methodName).Options; // setup using (var context = new GTLContext(options)) { var zip = new ZipCode { City = "Aalborg", Code = 9000 }; // zip has to be saved in db in order for the address to be created context.Add(zip); context.SaveChanges(); var homeAddress = new Address { Street = "Main road 4", AdditionalInfo = "4.floor, 10", Zip = zip }; var currentAddress = new Address { Street = "Library road 5", AdditionalInfo = "1.floor", Zip = zip }; var material = new Material { Isbn = "1234321", Title = "Mat title", Description = "A description", Language = "English", Lendable = true }; context.Add(homeAddress); context.Add(currentAddress); context.Add(material); context.SaveChanges(); var volumeController = ControllerFactory.CreateVolumeController(context); var createdVolume = volumeController.Create(materialId: material.MaterialId, homeLocationId: homeAddress.AddressId, currentLocationId: currentAddress.AddressId); Assert.Multiple(() => { Assert.AreEqual(createdVolume.Material.MaterialId, material.MaterialId); Assert.AreEqual(createdVolume.HomeLocation.AddressId, homeAddress.AddressId); Assert.AreEqual(createdVolume.CurrentLocation.AddressId, currentAddress.AddressId); }); } }
public void UpdateUpdatesCorrectly() { // setup var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; int materialId; var newTitle = "Some other title"; var newLanguage = "Danish"; using (var context = new GTLContext(options)) { var material = new Material { Isbn = _isbn, Title = _title, Language = _language, Lendable = _lendable, Description = _description, Type = _materialType, }; context.Add(material); context.SaveChanges(); materialId = material.MaterialId; } // action using (var context = new GTLContext(options)) { var controller = ControllerFactory.CreateMaterialController(context); var material = context.Materials.Find(materialId); material.Title = newTitle; material.Language = newLanguage; controller.Update(material); } // assertion using (var context = new GTLContext(options)) { var material = context.Materials.Find(materialId); Assert.That(material, Has .Property(nameof(Material.Isbn)).EqualTo(_isbn).And .Property(nameof(Material.Title)).EqualTo(newTitle).And .Property(nameof(Material.Language)).EqualTo(newLanguage).And .Property(nameof(Material.Lendable)).EqualTo(_lendable).And .Property(nameof(Material.Description)).EqualTo(_description).And .Property(nameof(Material.Type)) ); } }
private void InsertDummyData(GTLContext context) { //this should be passed by reference, and as such work with differnet objects without carrying the data over. ZipCode zipCode = new ZipCode { City = "Georgia", Code = zip }; context.ZipCodes.Add(zipCode); context.MemberTypes.AddRange(mTypes); context.SaveChanges(); }
// should be more tests with the same conditions for different properties of Material // (Isbn, Title, Language, Description, Lendable?) public void CreateThrowsAnArgumentNullExceptionOnNullOrEmptyTitle() { // setup var bookType = new MaterialType { Type = "Book" }; var programmingSubject = new MaterialSubject { SubjectName = "Programming" }; var author = new Author { FirstName = "Nikola", LastName = "Velichkov" }; var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; using (var context = new GTLContext(options)) { context.Add(bookType); context.Add(programmingSubject); context.Add(author); context.SaveChanges(); } // assertion using (var context = new GTLContext(options)) { var controller = ControllerFactory.CreateMaterialController(context); Assert.Throws <ArgumentNullException>(() => controller.Create( _isbn, "", _language, _lendable, _description, bookType, new List <MaterialSubject> { programmingSubject }, new List <Author> { author } )); } }
public void FindMaterialsFetchesMaterialsWithMultipleAuthors() { // setup var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .UseLazyLoadingProxies() .Options; var authorOne = new Author { FirstName = authorFirstName, LastName = authorLastName }; var authorTwo = new Author { FirstName = "Gergana", LastName = "Petkova" }; // action // add authors to materials materials.ForEach(material => { material.MaterialAuthors = new List <MaterialAuthor> { new MaterialAuthor { Author = authorOne, Material = material }, new MaterialAuthor { Author = authorTwo, Material = material } }; }); using (var context = new GTLContext(options)) { context.AddRange(materials); context.SaveChanges(); } // assertion using (var context = new GTLContext(options)) { var authorController = ControllerFactory.CreateAuthorController(context); var fetchedAuthor = context.Authors.FirstOrDefault(a => a.FirstName == authorOne.FirstName); var fetchedMaterials = authorController.FindMaterials(fetchedAuthor); Assert.That(fetchedMaterials, Has.Exactly(materials.Count).Items); } }
public void CreateCreatesCorrectly() { // setup var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; int memberId; int volumeId; using (var context = new GTLContext(options)) { context.Add(_member); context.Add(_volume); context.SaveChanges(); memberId = _member.MemberId; volumeId = _volume.VolumeId; } // action using (var context = new GTLContext(options)) { var member = context.Members.Find(memberId); var volume = context.Volumes.Find(volumeId); var controller = ControllerFactory.CreateLoanController(context); var loan = controller.Create(member, volume); // assertion var today = DateTime.Today; var dueDate = DateTime.Today.AddDays(LoanController.LendingPeriod); Assert.That(loan, Has .Property(nameof(Loan.Member)).Not.Null.And .Property(nameof(Loan.Volume)).Not.Null.And .Property(nameof(Loan.LoanDate)).EqualTo(today).And .Property(nameof(Loan.DueDate)).EqualTo(dueDate).And .Property(nameof(Loan.ReturnedDate)).Null.And .Property(nameof(Loan.Extensions)).EqualTo(0) ); } }
public void FindByIdFindsAnExistingMaterial() { // setup var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; // action int materialId; using (var context = new GTLContext(options)) { var material = new Material { Isbn = _isbn, Title = _title, Language = _language, Lendable = _lendable, Description = _description, Type = _materialType, }; context.Add(material); context.SaveChanges(); materialId = material.MaterialId; } // assertion using (var context = new GTLContext(options)) { var controller = ControllerFactory.CreateMaterialController(context); var material = controller.FindByID(materialId); Assert.That(material, Has .Property(nameof(Material.Isbn)).EqualTo(_isbn).And .Property(nameof(Material.Title)).EqualTo(_title).And .Property(nameof(Material.Language)).EqualTo(_language).And .Property(nameof(Material.Lendable)).EqualTo(_lendable).And .Property(nameof(Material.Description)).EqualTo(_description).And .Property(nameof(Material.Type))); } }
public void DeleteDeletesCorrectly() { // setup var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; int materialId; using (var context = new GTLContext(options)) { var material = new Material { Isbn = _isbn, Title = _title, Language = _language, Lendable = _lendable, Description = _description, Type = _materialType, }; context.Add(material); context.SaveChanges(); materialId = material.MaterialId; } // action using (var context = new GTLContext(options)) { var controller = ControllerFactory.CreateMaterialController(context); var material = context.Materials.Find(materialId); controller.Delete(material); } // assertion using (var context = new GTLContext(options)) { var material = context.Materials.Find(materialId); Assert.That(material, Is.Null); } }
public void CreateThrowsAnArgumentOutOfRangeExceptionWithEmptyAdditionalInfoParam() { // setup additionalInfo = ""; var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; using (var context = new GTLContext(options)) { context.Add(zip); context.SaveChanges(); } // action using (var context = new GTLContext(options)) { var addressController = ControllerFactory.CreateAddressController(context); // assertion Assert.Throws <ArgumentOutOfRangeException>(() => addressController.Create(street, additionalInfo, zip.Code)); } }
public void FindByIdFindsAnAddressInstance() { // setup int addressId; var options = new DbContextOptionsBuilder <GTLContext>() .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name) .Options; using (var context = new GTLContext(options)) { var address = new Address { AdditionalInfo = additionalInfo, Street = street, Zip = zip, }; context.Add(address); context.SaveChanges(); addressId = address.AddressId; } // action using (var context = new GTLContext(options)) { var addressController = ControllerFactory.CreateAddressController(context); var address = addressController.FindByID(addressId); // assertion Assert.That(address, Has .Property(nameof(Address.Street)).EqualTo(street).And .Property(nameof(Address.AdditionalInfo)).EqualTo(additionalInfo).And .Property(nameof(Address.Zip))); } }
/// <summary> /// Deletes the material entity passed from the database /// </summary> /// <param name="material">The material to be deleted</param> /// <returns>Number of rows that have changed</returns> public int Delete(Material material) { _context.Remove(material); return(_context.SaveChanges()); }
public int Delete(Member t) { //doesn't work due a lack of Cascading deletes. db.Remove(t); return(db.SaveChanges()); }
/// <summary> /// Deletes a volume entry from the database /// </summary> /// <param name="volume"></param> /// <returns>The number of rows changed</returns> public int Delete(Volume volume) { _context.Remove(volume); return(_context.SaveChanges()); }