Exemple #1
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));
            }
        }
Exemple #2
0
        public void InsertInsertsCorrectly()
        {
            // setup
            var options = new DbContextOptionsBuilder <GTLContext>()
                          .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name)
                          .Options;

            // action
            using (var context = new GTLContext(options))
            {
                var authorController = ControllerFactory.CreateAuthorController(context);
                var author           = new Author {
                    FirstName = authorFirstName, LastName = authorLastName
                };

                authorController.Insert(author);
            }

            // assertion
            using (var context = new GTLContext(options))
            {
                var fetchedAuthor = context.Authors
                                    .FirstOrDefault(a => a.FirstName == authorFirstName && a.LastName == authorLastName);

                Assert.That(fetchedAuthor, Has
                            .Property(nameof(Author.FirstName)).EqualTo(authorFirstName).And
                            .Property(nameof(Author.LastName)).EqualTo(authorLastName));
            }
        }
Exemple #3
0
        public void InsertThrowsArgumentNullExceptionWithNullAuthorParam()
        {
            // setup
            var options = new DbContextOptionsBuilder <GTLContext>()
                          .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name)
                          .Options;

            using var context = new GTLContext(options);
            var authorController = ControllerFactory.CreateAuthorController(context);

            // assertion
            Assert.Throws <ArgumentNullException>(() => authorController.Insert(null));
        }
Exemple #4
0
        public void CreateThrowsArgumentExceptionWithEmptyFirstNameArgument()
        {
            // setup
            authorFirstName = "";
            var options = new DbContextOptionsBuilder <GTLContext>()
                          .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name)
                          .Options;

            using var context = new GTLContext(options);
            var authorController = ControllerFactory.CreateAuthorController(context);

            // assertion
            Assert.Throws <ArgumentException>(() =>
                                              authorController.Create(authorFirstName, authorLastName));
        }
Exemple #5
0
        public void FindByIdReturnsNullOnANonExistingAuthor()
        {
            // setup
            var options = new DbContextOptionsBuilder <GTLContext>()
                          .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name)
                          .Options;

            // action
            using var context = new GTLContext(options);
            var authorController = ControllerFactory.CreateAuthorController(context);
            // some random number, doesn't matter what
            var fetchedAuthor = authorController.FindByID(11);

            // assertion
            Assert.That(fetchedAuthor, Is.Null);
        }
Exemple #6
0
        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);
            }
        }
Exemple #7
0
        public void CreateCreatesCorrectAuthorInstance()
        {
            // setup
            var options = new DbContextOptionsBuilder <GTLContext>()
                          .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name)
                          .Options;

            using var context = new GTLContext(options);
            var authorController = ControllerFactory.CreateAuthorController(context);

            // action
            var author = authorController.Create(authorFirstName, authorLastName);

            // assertion
            Assert.That(author, Has
                        .Property(nameof(Author.FirstName)).EqualTo(authorFirstName).And
                        .Property(nameof(Author.LastName)).EqualTo(authorLastName));
        }
Exemple #8
0
        public void InsertDoesNotInsertWithNullAuthorParam()
        {
            // setup
            var options = new DbContextOptionsBuilder <GTLContext>()
                          .UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name)
                          .Options;

            // action
            using (var context = new GTLContext(options))
            {
                var authorController = ControllerFactory.CreateAuthorController(context);

                // silently catch the error that's supposed to be thrown by the Insert method
                // in order to test the after effect
                try { authorController.Insert(null); }
                catch (ArgumentNullException) { }
            }

            // assertion
            using (var context = new GTLContext(options))
            {
                Assert.That(context.Authors.Count(), Is.EqualTo(0));
            }
        }
 public AuthorController(GTLContext context)
 {
     _controller = ControllerFactory.CreateAuthorController(context);
 }