public void GetAllMyBooks()
        {
            var data = new List <MyBook>
            {
                new MyBook {
                    Id = 1, BookId = 1, UserId = 1
                },
                new MyBook {
                    Id = 2, BookId = 2, UserId = 2
                },
                new MyBook {
                    Id = 3, BookId = 3, UserId = 3
                },
            }.AsQueryable();

            var mockSet = new Mock <DbSet <MyBook> >();

            mockSet.As <IQueryable <MyBook> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <MyBook> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <MyBook> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <MyBook> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

            var mockContext = new Mock <BookContext>();

            mockContext.Setup(m => m.MyBooks).Returns(mockSet.Object);

            var myBookId        = 1;
            var service         = new MyBookService(mockContext.Object);
            var bookFound       = service.GetById(myBookId);
            var excpectedResult = data.Where(mb => mb.Id == myBookId).FirstOrDefault();

            Assert.AreEqual(excpectedResult.UserId, bookFound.UserId);
        }
Beispiel #2
0
        void DeleteBook(object sender, EventArgs s)
        {
            Button btn           = (Button)sender;
            var    myBookId      = int.Parse(btn.AccessibleName);
            var    myBookService = new MyBookService();

            myBookService.Delete(myBookId);
            MessageBox.Show("Successfull deleted!");
        }
Beispiel #3
0
        public void AddBookToMyBook(object sender, EventArgs s)
        {
            Button btn           = (Button)sender;
            var    bookId        = int.Parse(btn.AccessibleName);
            var    myBookService = new MyBookService();
            var    newMyBook     = new MyBook();

            newMyBook.BookId = bookId;
            newMyBook.UserId = Global.UserId;


            myBookService.Add(newMyBook);
            MessageBox.Show("Successfull added!");
        }
        public void MyBook()
        {
            var mockSet = new Mock <DbSet <MyBook> >();

            var mockContext = new Mock <BookContext>();

            mockContext.Setup(m => m.MyBooks).Returns(mockSet.Object);

            var service = new MyBookService(mockContext.Object);

            service.Add(new MyBook());

            mockSet.Verify(m => m.Add(It.IsAny <MyBook>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        public void GetAll()
        {
            var data = new List <MyBook>
            {
                new MyBook {
                    Id = 1, BookId = 1, UserId = 1
                },
                new MyBook {
                    Id = 2, BookId = 2, UserId = 2
                },
                new MyBook {
                    Id = 3, BookId = 3, UserId = 3
                },
            }.AsQueryable();

            var mockSet = new Mock <DbSet <MyBook> >();

            mockSet.As <IQueryable <MyBook> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <MyBook> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <MyBook> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <MyBook> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

            var mockContext = new Mock <BookContext>();

            mockContext.Setup(m => m.MyBooks).Returns(mockSet.Object);

            var service          = new MyBookService(mockContext.Object);
            var userId           = 1;
            var myBooks          = service.GetAll(userId);
            var excpectedResults = data.Where(mb => mb.UserId == userId).ToList();

            Assert.AreEqual(excpectedResults.Count, myBooks.Count);
            for (int i = 0; i < excpectedResults.Count; i++)
            {
                var excpectedResult = excpectedResults[i];
                var bookFound       = myBooks[i];
                Assert.AreEqual(excpectedResult.Id, bookFound.Id);
                Assert.AreEqual(excpectedResult.UserId, bookFound.UserId);
                Assert.AreEqual(excpectedResult.BookId, bookFound.BookId);
            }
        }
Beispiel #6
0
        private void GetMyBooks()
        {
            panelMyBooks.Controls.Clear();
            var           myBookService = new MyBookService();
            var           userId        = Global.UserId;
            List <MyBook> myBooks       = myBookService.GetAll(userId);

            label1.Text = myBooks.Count() + "";
            int i = 0;

            foreach (var myBook in myBooks)
            {
                var   coeficientIndex = 170;
                var   bookService     = new BookService();
                var   book            = bookService.GetById(myBook.BookId);
                Label lblName         = new Label();
                lblName.Text     = book.Name;
                lblName.Location = new Point(160, i * coeficientIndex + 20);
                lblName.Name     = "lblName" + i;
                lblName.Font     = new Font("Arial", 10, FontStyle.Bold);
                lblName.AutoSize = true;


                PictureBox pictureBox = new PictureBox();
                pictureBox.Location = new Point(5, i * coeficientIndex + 10);
                var request = WebRequest.Create(book.ImageUrl);
                using (var response = request.GetResponse())
                    using (var stream = response.GetResponseStream())
                    {
                        pictureBox.Image = Bitmap.FromStream(stream);
                    }
                pictureBox.Width    = 150;
                pictureBox.Height   = 150;
                pictureBox.Name     = "ptb" + i;
                pictureBox.SizeMode = PictureBoxSizeMode.Zoom;



                Label lblAuthor = new Label();
                lblAuthor.Text     = "Author: " + book.Author;
                lblAuthor.Location = new Point(160, i * coeficientIndex + 60);
                lblAuthor.Name     = "lblAuthor" + i;
                lblAuthor.Font     = new Font("Arial", 8);
                lblAuthor.AutoSize = true;

                Label lblGenre = new Label();
                lblGenre.Text     = "Genre: " + book.Genre;
                lblGenre.Location = new Point(160, i * coeficientIndex + 80);
                lblGenre.Name     = "lblGenre" + i;
                lblGenre.Font     = new Font("Arial", 8);
                lblGenre.AutoSize = true;

                Label lblDescription = new Label();
                lblDescription.Text     = book.Description;
                lblDescription.Location = new Point(160, i * coeficientIndex + 100);
                lblDescription.Name     = "lblDescription" + i;
                lblDescription.Font     = new Font("Arial", 8);
                lblDescription.AutoSize = true;

                Button btnDelete = new Button();
                btnDelete.Text           = "Remove book";
                btnDelete.Location       = new Point(480, i * coeficientIndex + 20);
                btnDelete.Name           = "btnDelete" + i;
                btnDelete.AccessibleName = myBook.Id.ToString();
                btnDelete.Click         += new System.EventHandler(this.DeleteBook);
                btnDelete.AutoSize       = true;
                btnDelete.Font           = new Font("Microsoft YaHei", 8);


                i++;
                panelMyBooks.Controls.Add(lblName);
                panelMyBooks.Controls.Add(pictureBox);
                panelMyBooks.Controls.Add(lblAuthor);
                panelMyBooks.Controls.Add(lblGenre);
                panelMyBooks.Controls.Add(lblDescription);
                panelMyBooks.Controls.Add(btnDelete);
            }
        }
 public MyBooksController(MyBookService myBookService)
 {
     _myBookService = myBookService;
 }