示例#1
0
    public async Task GetAllTask()
    {
        var author1 = new Authors()
        {
            Author_id = "1", Author_name = "test author 1"
        };
        var author2 = new Authors()
        {
            Author_id = "2", Author_name = "test author 2"
        };
        var authors = new List <Authors> {
            author1, author2
        };

        var fakeRepositoryMock = new Mock <IAuthorsRepository>();

        fakeRepositoryMock.Setup(x => x.GetAll()).ReturnsAsync(authors);

        var coachService = new AuthorsService(fakeRepositoryMock.Object);

        var resultAuthors = await coachService.GetAll();

        Assert.Collection(resultAuthors, author =>
        {
            Assert.Equal("test author 1", author.Author_name);
        },
                          author =>
        {
            Assert.Equal("test author 2", author.Author_name);
        });
    }
示例#2
0
        public void ReturnCorrectResultWhenThereAreAuthors()
        {
            var firstAuthorName  = "test";
            var secondAuthorName = "test2";

            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Author> >();
            var authorsService   = new AuthorsService(mockedRepository.Object, mockedUnitOfWork.Object);

            var mockedFirstAuthor = new Mock <Author>().Object;

            mockedFirstAuthor.Name = firstAuthorName;

            var mockedSecondAuthor = new Mock <Author>().Object;

            mockedSecondAuthor.Name = secondAuthorName;

            var authors = new List <Author>();

            authors.Add(mockedFirstAuthor);
            authors.Add(mockedSecondAuthor);

            mockedRepository.Setup(x => x.GetAllWithDeleted).Returns(authors.AsQueryable <Author>);

            var result = authorsService.GetAuthorsWithDeleted();

            Assert.AreEqual(authors, result);
        }
        public void TestEditAuthorNegative()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            authorsController.Put("");
        }
        public void TestEditAuthorPositive()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            authorsController.Put("newUsr");
        }
示例#5
0
        public async Task GetAuthorByIdShouldReturnNullWhenDeleted()
        {
            var options = new DbContextOptionsBuilder <AlexandriaDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var db = new AlexandriaDbContext(options);

            await db.Authors.AddAsync(
                new Author
            {
                FirstName  = "first1",
                SecondName = "second1",
                LastName   = "last1",
                IsDeleted  = true,
                DeletedOn  = DateTime.UtcNow,
            });

            await db.SaveChangesAsync();

            var authorsService = new AuthorsService(db);

            var result = await authorsService.GetAuthorByIdAsync <AuthorTestModel>(1);

            Assert.Null(result);
        }
示例#6
0
    public async Task AddAndSaveTest()
    {
        var author1 = new Authors()
        {
            Author_id = "1", Author_name = "test author 1"
        };
        var author2 = new Authors()
        {
            Author_id = "2", Author_name = "test author 2"
        };
        var authors = new List <Authors> {
            author1, author2
        };

        var author3 = new Authors()
        {
            Author_id = "3", Author_name = "test author 3"
        };

        var fakeRepositoryMock = new Mock <IAuthorsRepository>();

        fakeRepositoryMock.Setup(x => x.Add(It.IsAny <Authors>())).Callback <Authors>(arg => authors.Add(arg));

        var coachService = new AuthorsService(fakeRepositoryMock.Object);

        await coachService.AddAndSave(author3);


        Assert.Equal(3, authors.Count);
    }
示例#7
0
        public async Task GetAuthorByIdShouldreturnAuthorWithSelectedId()
        {
            var optionsBuilder = new DbContextOptionsBuilder <BookTubeContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new BookTubeContext(optionsBuilder.Options);
            var service   = new AuthorsService(dbContext);

            var authors = new List <Author>
            {
                new Author {
                    Id = 1, Name = "Ivan Vazov", Bio = "Born in Sopot, Bulgaria"
                },
                new Author {
                    Id = 2, Name = "Aleko Konstantinov", Bio = "Born in Svishtov, Bulgaria"
                },
                new Author {
                    Id = 3, Name = "Elin Pelin", Bio = "Born in Bailovo, Bulgaria"
                }
            };

            foreach (var author in authors)
            {
                await dbContext.Authors.AddAsync(author);
            }
            await dbContext.SaveChangesAsync();

            var expectedAuthor = authors.First(x => x.Id == 2);
            var actual         = service.GetAuthorById(2);

            Assert.Equal(expectedAuthor, actual);
        }
示例#8
0
    public async Task DeleteAndSaveTest()
    {
        var author1 = new Authors()
        {
            Author_id = "1", Author_name = "test author 1"
        };
        var author2 = new Authors()
        {
            Author_id = "2", Author_name = "test author 2"
        };
        var authors = new List <Authors> {
            author1, author2
        };

        var fakeRepositoryMock = new Mock <IAuthorsRepository>();

        fakeRepositoryMock.Setup(x => x.Delete(It.IsAny <string>())).Callback <int>(arg => authors.RemoveAt(1));

        var coachService = new AuthorsService(fakeRepositoryMock.Object);

        await coachService.DeleteAndSave(author2.Author_id);

        Assert.Single(authors);
        Assert.Equal("test author 1", authors[0].Author_name);
    }
示例#9
0
        public async Task AllShouldReturnAllAuthors()
        {
            var optionsBuilder = new DbContextOptionsBuilder <BookTubeContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new BookTubeContext(optionsBuilder.Options);
            var service   = new AuthorsService(dbContext);

            var authors = new List <Author>
            {
                new Author {
                    Id = 1, Name = "Ivan Vazov", Bio = "Born in Sopot, Bulgaria"
                },
                new Author {
                    Id = 2, Name = "Aleko Konstantinov", Bio = "Born in Svishtov, Bulgaria"
                },
                new Author {
                    Id = 3, Name = "Elin Pelin", Bio = "Born in Bailovo, Bulgaria"
                }
            };

            foreach (var author in authors)
            {
                await dbContext.Authors.AddAsync(author);
            }
            await dbContext.SaveChangesAsync();

            var sortedPublishers = authors.OrderBy(x => x.Name).ToList();
            var actual           = service.All();

            Assert.Equal(sortedPublishers, actual);
        }
        public void TestAddAuthorPositive()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            authorsController.Post("{'username': '******'}");
        }
示例#11
0
        public void ReturnCorrectAuthorWhenAuthorNameParameterIsValid()
        {
            var firstAuthorName  = "test";
            var secondAuthorName = "test2";

            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Author> >();
            var authorsService   = new AuthorsService(mockedRepository.Object, mockedUnitOfWork.Object);

            var mockedFirstAuthor = new Mock <Author>().Object;

            mockedFirstAuthor.Name = firstAuthorName;

            var mockedSecondAuthor = new Mock <Author>().Object;

            mockedSecondAuthor.Name = secondAuthorName;

            var authors = new List <Author>();

            authors.Add(mockedFirstAuthor);
            authors.Add(mockedSecondAuthor);

            mockedRepository.Setup(x => x.GetAll).Returns(authors.AsQueryable <Author>);

            var result = authorsService.GetBookAuthorByName(firstAuthorName);

            Assert.AreSame(mockedFirstAuthor, result);
        }
示例#12
0
        public ActionResult AddAuthor(string name)
        {
            AuthorsService service = new AuthorsService();
            int            id      = service.AddAuthor(name);

            return(RedirectToAction("AuthorsSelectList", new { selectedId = id }));
        }
示例#13
0
    public async Task UpdateAndSaveTest()
    {
        var author1 = new Authors()
        {
            Author_id = "1", Author_name = "test author 1"
        };
        var author2 = new Authors()
        {
            Author_id = "2", Author_name = "test author 2"
        };
        var authors = new List <Authors> {
            author1, author2
        };

        var newAuthor2 = new Authors()
        {
            Author_id = "2", Author_name = "new test author 2"
        };

        var fakeRepositoryMock = new Mock <IAuthorsRepository>();

        fakeRepositoryMock.Setup(x => x.Update(It.IsAny <Authors>())).Callback <Authors>(arg => authors[1] = arg);

        var coachService = new AuthorsService(fakeRepositoryMock.Object);

        await coachService.UpdateAndSave(newAuthor2);

        Assert.Equal("new test author 2", authors[1].Author_name);
    }
示例#14
0
        public async Task GetById_ShouldFindAuthor()
        {
            var author = await AuthorsService.Get(DefaultData.Authors.Flenagan.Id);

            Assert.That(author, Is.Not.Null);
            Assert.That(author.Id, Is.EqualTo(DefaultData.Authors.Flenagan.Id));
        }
        public void TestDeleteAuthorNegative()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            authorsController.Delete(-1);
        }
        public void TestDeleteAuthorPositive()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            //id of existing author
            authorsController.Delete(1);
        }
示例#17
0
        public void Update_InvalidAuthor_ShouldThrownAuthorIncorrectException()
        {
            var dto = new AuthorDto()
            {
                Id = DefaultData.Authors.Devis.Id
            };

            Assert.Throws <AuthorIncorrectException>(async() => await AuthorsService.Update(dto.Id, dto));
        }
        public void CreateInstanceOfAuthorServiceWhenParametersAreNotNull()
        {
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Author> >();

            var authorService = new AuthorsService(mockedRepository.Object, mockedUnitOfWork.Object);

            Assert.IsInstanceOf <AuthorsService>(authorService);
        }
示例#19
0
 public AuthorsServiceTest()
 {
     AddAuthorCommandMock    = new Mock <IAddAuthorCommand>();
     UpdateAuthorCommandMock = new Mock <IUpdateAuthorCommand>();
     ListAllAuthorsQueryMock = new Mock <IListAllAuthorsQuery>();
     ServiceUnderTest        = new AuthorsService(AddAuthorCommandMock.Object,
                                                  UpdateAuthorCommandMock.Object,
                                                  ListAllAuthorsQueryMock.Object);
 }
        public void TestGetAuthors()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            IEnumerable <string> result = authorsController.Get();

            Assert.IsNotNull(result);
        }
        public void TestGetAuthorNegative()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            string result = authorsController.Get(-1);

            Assert.IsTrue(result.Length == 0);
        }
示例#22
0
        public async Task Delete_NoneExistendAuthor_ShouldReturnNull()
        {
            var countBefore = (await AuthorsService.GetAll(It.IsAny <PagingParameterModel>())).Count();

            await AuthorsService.Delete(int.MaxValue);

            var countAfter = (await AuthorsService.GetAll(It.IsAny <PagingParameterModel>())).Count();

            Assert.That(countAfter, Is.EqualTo(countBefore));
        }
示例#23
0
        public void CallAuthorsRepositoryGetAllWithDeletedMethod()
        {
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Author> >();
            var authorsService   = new AuthorsService(mockedRepository.Object, mockedUnitOfWork.Object);

            var result = authorsService.GetAuthorsWithDeleted();

            mockedRepository.Verify(x => x.GetAllWithDeleted, Times.Once());
        }
示例#24
0
        public void ThrowArgumentExceptionWhenAuthorNameParameterIsEmptyStringOrWhiteSpace(string invalidAuthorName)
        {
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Author> >();

            var authorsService = new AuthorsService(mockedRepository.Object, mockedUnitOfWork.Object);

            Assert.That(() => authorsService.GetBookAuthorByName(invalidAuthorName),
                        Throws.ArgumentException.With.Message.Contains("authorName"));
        }
示例#25
0
        public void ThrowArgumentNullExceptionWithProperMessageWhenAuthorNameParameterTIsNull()
        {
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Author> >();

            var authorsService = new AuthorsService(mockedRepository.Object, mockedUnitOfWork.Object);

            Assert.That(() => authorsService.GetBookAuthorByName(null),
                        Throws.ArgumentNullException.With.Message.Contains("authorName"));
        }
        public void TestGetAuthorPositive()
        {
            AuthorsService    service           = new AuthorsService();
            AuthorsController authorsController = new AuthorsController(service);

            string result = authorsController.Get(1);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Length > 0);
        }
示例#27
0
文件: Program.cs 项目: kipy-be/KCL.Db
        private static void Test()
        {
            var authorsService  = new AuthorsService(_db);
            var articlesService = new ArticlesService(_db);

            var articles = _db.Select <Article>()
                           .Where(a => a.Author.FirstName == "Jean")
                           .GetMany();

            //var authors = authorsService.GetAll();
            //var article = articlesService.GetFromId(1);
            //article.Title = "test";

            //var author2 = new Author()
            //{
            //    Nick = "truc",
            //    FirstName = "Jean",
            //    LastName = "Bon"
            //};

            //Console.WriteLine(author2.Id);

            //_db.Insert(author2);

            //author2.LastName = "Bla";
            //_db.Update(author2);

            ////_db.Delete(author2);

            ////_db.UpdateWhere<Author>(new Dictionary<string, object>
            ////{
            ////    { "firstname", "Thierry" }
            ////},
            ////new Dictionary<string, object>
            ////{
            ////    { "nick", "truc" }
            ////});

            //_db.DeleteWhere<Author>(new Dictionary<string, object>
            //{
            //    { "nick", "truc" }
            //});

            //var transaction = _db.CreateTransaction();
            //var author3 = new Author()
            //{
            //    Nick = "machin",
            //    FirstName = "Pierre",
            //    LastName = "Test"
            //};

            //_db.Insert(author3);
            //transaction.Commit();
        }
示例#28
0
        public void Create_ExistsAuthor_ShouldThrownAuthorDublicateException()
        {
            var dto = new AuthorDto()
            {
                Lastname   = DefaultData.Authors.Flenagan.Lastname,
                Firstname  = DefaultData.Authors.Flenagan.Firstname,
                Middlename = DefaultData.Authors.Flenagan.Middlename,
            };

            Assert.Throws <AuthorDublicateException>(async() => await AuthorsService.Create(dto));
        }
示例#29
0
        public void CallAutorsRepositoryGetAllMethodWhenAuthorNameParameterIsValid()
        {
            var authorName = "test";

            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IRepository <Author> >();
            var authorsService   = new AuthorsService(mockedRepository.Object, mockedUnitOfWork.Object);

            var result = authorsService.GetBookAuthorByName(authorName);

            mockedRepository.Verify(x => x.GetAll, Times.Once());
        }
示例#30
0
    public void ExistsTest()
    {
        var fakeRepositoryMock = new Mock <IAuthorsRepository>();

        fakeRepositoryMock.Setup(x => x.AuthorExists(It.IsAny <string>())).Returns(true);

        var coachService = new AuthorsService(fakeRepositoryMock.Object);

        bool result = coachService.CoachExists("1");

        Assert.True(result);
    }