Beispiel #1
0
        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)));
            }
        }
Beispiel #2
0
        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));
            }
        }
Beispiel #3
0
        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)));
            }
        }
Beispiel #4
0
        // 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
                }
                                                          ));
            }
        }
Beispiel #5
0
 public int Insert(Member t)
 {
     if (t == null)
     {
         throw new ArgumentNullException(nameof(t), "member object can't be null");
     }
     db.Add(t);
     return(db.SaveChanges());
 }
        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 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);
                });
            }
        }
Beispiel #8
0
        /// <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);
        }
Beispiel #9
0
        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))
                            );
            }
        }
        /// <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);
        }
Beispiel #11
0
        public void FindMaterialsDoesNotFetchAnyMaterialsWithOneAuthorAndZeroMaterials()
        {
            // 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 = authorTwo, Material = material
                    }
                };
            });

            using (var context = new GTLContext(options))
            {
                context.Add(authorOne);
                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 == authorFirstName);

                var fetchedMaterials = authorController.FindMaterials(fetchedAuthor);

                Assert.That(fetchedMaterials, Is.Empty.And.Not.Null);
            }
        }
Beispiel #12
0
        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);
            }
        }
Beispiel #13
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)));
            }
        }
Beispiel #14
0
        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));
            }
        }
Beispiel #15
0
        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>
 /// Saves a new material entity to the database
 /// </summary>
 /// <param name="material"></param>
 /// <returns></returns>
 public int Insert(Material material)
 {
     _context.Add(material);
     return(_context.SaveChanges());
 }