public void Assign_Book_To_Student()
        {
            var options = new DbContextOptionsBuilder <SchoolLibraryContext>()
                          .UseInMemoryDatabase("Book_Assignment_Student")
                          .Options;

            using (var context = new SchoolLibraryContext(options))
            {
                context.Students.Add(new Student
                {
                    Id   = 100,
                    Name = "Ali"
                });

                context.Books.Add(new Book
                {
                    Id       = 555,
                    Name     = "How to become a millinaire",
                    IsIssued = false,
                    Author   = "XYZ"
                });

                context.SaveChanges();
            }

            using (var context = new SchoolLibraryContext(options))
            {
                var service = new BookAssignmentService(context);
                service.AssignBook(555, 100);
                var book = context.Books.Find(555);
                Assert.True(book.IsIssued);
            }
        }
示例#2
0
        public void Add_New_Book()
        {
            var options = new DbContextOptionsBuilder <SchoolLibraryContext>()
                          .UseInMemoryDatabase("Add_New_Book")
                          .Options;

            using (var context = new SchoolLibraryContext(options))
            {
                var service = new BookService(context);

                service.Add(new Book
                {
                    Id = 123
                });

                Assert.Equal(123, context.Books.Single().Id);
            }
        }
        public void Add_New_Student()
        {
            var options = new DbContextOptionsBuilder <SchoolLibraryContext>()
                          .UseInMemoryDatabase("Add_New_Student")
                          .Options;

            using (var context = new SchoolLibraryContext(options))
            {
                var service = new StudentService(context);

                service.Add(new Student
                {
                    Id = 100
                });

                Assert.Equal(100, context.Students.Single().Id);
            }
        }
        public void Not_Assign_Same_Book_To_Multiple_Students()
        {
            var options = new DbContextOptionsBuilder <SchoolLibraryContext>()
                          .UseInMemoryDatabase("Not_Assign_Same_Book_To_Multiple_Students")
                          .Options;

            using (var context = new SchoolLibraryContext(options))
            {
                context.Students.Add(new Student
                {
                    Id   = 100,
                    Name = "Ali"
                });

                context.Students.Add(new Student
                {
                    Id   = 200,
                    Name = "Rehmat"
                });

                context.Books.Add(new Book
                {
                    Id       = 555,
                    Name     = "How to become a millinaire",
                    IsIssued = false,
                    Author   = "XYZ"
                });

                context.SaveChanges();
            }

            using (var context = new SchoolLibraryContext(options))
            {
                var service = new BookAssignmentService(context);
                service.AssignBook(555, 100);

                var bookAssignedTo100 = context.BookAssignToStudents.SingleOrDefault(a => a.Student.Id == 100);
                Assert.NotNull(bookAssignedTo100);

                service.AssignBook(555, 200);
                var bookAssignedTo200 = context.BookAssignToStudents.SingleOrDefault(a => a.Student.Id == 200);
                Assert.Null(bookAssignedTo200);
            }
        }
        public void Extend_DueDate_Of_Assigned_Book()
        {
            var options = new DbContextOptionsBuilder <SchoolLibraryContext>()
                          .UseInMemoryDatabase("Extend_DueDate_Of_Assigned_Book")
                          .Options;

            using (var context = new SchoolLibraryContext(options))
            {
                context.Students.Add(new Student
                {
                    Id   = 100,
                    Name = "Ali"
                });

                context.Books.Add(new Book
                {
                    Id       = 555,
                    Name     = "How to become a millinaire",
                    IsIssued = false,
                    Author   = "XYZ"
                });

                context.SaveChanges();
            }

            using (var context = new SchoolLibraryContext(options))
            {
                var service = new BookAssignmentService(context);
                service.AssignBook(555, 100);

                var assignedBook = service.GetAssignedBook(555);
                Assert.True(assignedBook.Book.IsIssued);

                var response = service.ExtendDueDate(assignedBook.BookId, assignedBook.StudentId, assignedBook.DueDate.AddDays(7));

                Assert.Equal("Success", response);
            }
        }
 public BookAssignmentService(SchoolLibraryContext context)
 {
     _context = context;
 }
示例#7
0
 public BookService(SchoolLibraryContext context)
 {
     _context = context;
 }
 public StudentService(SchoolLibraryContext context)
 {
     _context = context;
 }
        public void Get_Overdue_Books()
        {
            var options = new DbContextOptionsBuilder <SchoolLibraryContext>()
                          .UseInMemoryDatabase("Get_Overdue_Books")
                          .Options;

            using (var context = new SchoolLibraryContext(options))
            {
                context.Students.Add(new Student
                {
                    Id   = 100,
                    Name = "Ali"
                });

                context.Books.Add(new Book
                {
                    Id       = 555,
                    Name     = "How to become a millinaire",
                    IsIssued = false,
                    Author   = "XYZ"
                });

                context.Books.Add(new Book
                {
                    Id       = 556,
                    Name     = "Some New Book 1",
                    IsIssued = false,
                    Author   = "XYZ 1"
                });

                context.Books.Add(new Book
                {
                    Id       = 557,
                    Name     = "Some New Book 2",
                    IsIssued = false,
                    Author   = "XYZ 2"
                });

                context.SaveChanges();
            }

            using (var context = new SchoolLibraryContext(options))
            {
                var service = new BookAssignmentService(context);
                service.AssignBook(555, 100);
                service.AssignBook(556, 100);
                service.AssignBook(557, 100);

                var assignedBook1 = context.BookAssignToStudents.Single(b => b.BookId == 555);
                assignedBook1.DueDate = DateTime.Now.AddDays(-5);
                var assignedBook2 = context.BookAssignToStudents.Single(b => b.BookId == 557);
                assignedBook2.DueDate = DateTime.Now.AddDays(-5);

                context.UpdateRange(new object[] { assignedBook1, assignedBook2 });
                context.SaveChanges();

                var response = service.GetOverdueBooks();

                Assert.Equal(2, response.Count());
            }
        }