Example #1
0
        public void Insert()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.Insert(It.IsAny <DbCategory>()))
            .Callback((DbCategory c) =>
            {
                c.Id         = 6;
                c.RowVersion = new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 };
                store.Categories.Add(c);
            }).Verifiable();
            this.categoryRepository = mockCategoryRepository.Object;

            DbCategory category = new DbCategory
            {
                Title = "Category 6"
            };

            this.categoryRepository.Insert(category);

            mockCategoryRepository.Verify(r => r.Insert(It.IsAny <DbCategory>()), Times.Once());
            Assert.AreEqual(6, store.Categories.Count);
            Assert.AreEqual(6, store.Categories.Last().Id);
        }
Example #2
0
        public async Task GetAsync_FilteredAndOrdered()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.GetAsync(It.IsAny <Expression <Func <DbCategory, bool> > >(),
                                                         It.IsAny <Func <IQueryable <DbCategory>, IOrderedQueryable <DbCategory> > >(),
                                                         string.Empty))
            .Returns((Expression <Func <DbCategory, bool> > filter,
                      Func <IQueryable <DbCategory>, IOrderedQueryable <DbCategory> > orderBy,
                      string includeProperties) =>
            {
                var filtered = store.Categories.Where(filter.Compile()).AsQueryable();
                var ordered  = orderBy.Invoke(filtered);
                return(Task.FromResult(ordered.AsEnumerable()));
            });
            this.categoryRepository = mockCategoryRepository.Object;

            var result = await this.categoryRepository.GetAsync(filter : c => c.Id >= 2,
                                                                orderBy : q => q.OrderByDescending(c => c.Id));

            Assert.AreEqual(4, result.Count());
            Assert.AreEqual(5, result.First().Id);
            Assert.AreEqual(2, result.Last().Id);
        }
Example #3
0
        public async Task FetchImagesAsync_Half()
        {
            VolatileMainDataset   dataSet     = new VolatileMainDataset();
            Mock <IImagesManager> mockService = new Mock <IImagesManager>();

            mockService
            .Setup(s => s.FetchImagesAsync(
                       null,
                       It.IsAny <int[]>(),
                       It.IsAny <string>(),
                       null,
                       null,
                       It.IsAny <string>()))
            .Returns <int[], int[], string, string, string, string>(
                (tagsIds, categoriesIds, name, description, uploader, extension) =>
                Task.FromResult <List <Image> >(
                    dataSet.Images
                    .Where(i =>
                           (i.IdCategory.HasValue && categoriesIds.Contains(i.IdCategory.Value)) &&
                           (i.Name != null && i.Name.Contains(name)) &&
                           i.FileExtension == extension)
                    .Select(el => el.ToExposed())
                    .ToList()
                    )
                );
            this.imagesManager = mockService.Object;

            List <Image> images = await this.imagesManager.FetchImagesAsync(
                categoriesIds : new int[] { 3 },
                name : "Image",
                extension : "gif"
                );

            Assert.AreEqual(2, images.Count);
        }
Example #4
0
        public async Task AddCategoryAsync()
        {
            VolatileMainDataset     store       = new VolatileMainDataset();
            Mock <IBaseMainService> mockService = new Mock <IBaseMainService>();

            mockService.Setup(s => s.CreateAsync(It.Is <DbCategory>(a => a.Title == "Added title")))
            .ReturnsAsync(8);
            this.service = mockService.Object;

            int id = await this.service.CreateAsync(this.categoryToAdd);

            Assert.AreEqual(8, id);
        }
Example #5
0
        public async Task GetByIdAsync_DoesntExist()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.GetByIdAsync(It.IsNotIn <int>(1, 2, 3, 4, 5, 6)))
            .Returns <int>(id => Task.FromResult <DbCategory>(null));
            this.categoryRepository = mockCategoryRepository.Object;

            DbCategory category = await this.categoryRepository.GetByIdAsync(10);

            Assert.AreEqual(null, category);
        }
Example #6
0
        public void GetById_DoesntExist()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.GetById(It.IsNotIn <int>(1, 2, 3, 4, 5, 6)))
            .Returns <DbCategory>(null);
            this.categoryRepository = mockCategoryRepository.Object;

            DbCategory category = this.categoryRepository.GetById(10);

            Assert.AreEqual(null, category);
        }
Example #7
0
        public void GetById()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.GetById(It.IsInRange <int>(1, 6, Range.Inclusive)))
            .Returns <int>(id => store.Categories.Find(el => el.Id == id));
            this.categoryRepository = mockCategoryRepository.Object;

            DbCategory category       = this.categoryRepository.GetById(1);
            DbCategory storedCategory = store.Categories.Single(el => el.Id == 1);

            Assert.AreEqual(category.Title, storedCategory.Title);
        }
Example #8
0
        public async Task GetAsync_Filtered()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.GetAsync(It.IsAny <Expression <Func <DbCategory, bool> > >(), null, string.Empty))
            .Returns((Expression <Func <DbCategory, bool> > filter,
                      Func <IQueryable <DbCategory>, IOrderedQueryable <DbCategory> > orderBy,
                      string includeProperties) => Task.FromResult(store.Categories.Where(filter.Compile())));
            this.categoryRepository = mockCategoryRepository.Object;

            var result = await this.categoryRepository.GetAsync(filter : c => c.Id >= 2);

            Assert.AreEqual(4, result.Count());
        }
Example #9
0
        public async Task FetchTagsWithMatchingName_Meh()
        {
            VolatileMainDataset dataSet     = new VolatileMainDataset();
            Mock <ITagsManager> mockService = new Mock <ITagsManager>();

            mockService.Setup(s => s.FetchTagsWithMatchingName(It.IsAny <string>()))
            .Returns <string>(term => Task.FromResult <List <Tag> >(
                                  dataSet.Tags
                                  .Where(el => el.Name.Contains(term))
                                  .Select(el => el.ToExposed())
                                  .ToList())
                              );
            this.tagsManager = mockService.Object;

            List <Tag> tags = await this.tagsManager.FetchTagsWithMatchingName("Meh");

            Assert.AreEqual(1, tags.Count);
        }
Example #10
0
        public void Get_Ordered()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.Get(null, It.IsAny <Func <IQueryable <DbCategory>, IOrderedQueryable <DbCategory> > >(), string.Empty))
            .Returns((Expression <Func <DbCategory, bool> > filter,
                      Func <IQueryable <DbCategory>, IOrderedQueryable <DbCategory> > orderBy,
                      string includeProperties) => orderBy.Invoke(store.Categories.AsQueryable()));
            this.categoryRepository = mockCategoryRepository.Object;

            var result = this.categoryRepository.Get(orderBy: q => q.OrderByDescending(a => a.Id));

            Assert.AreEqual(5, result.Count());
            Assert.AreEqual(1, result.Last().Id);
            Assert.AreEqual(5, result.First().Id);
        }
Example #11
0
        public async Task FetchUsersWithMatchingLogin_One()
        {
            VolatileMainDataset  dataSet     = new VolatileMainDataset();
            Mock <IUsersManager> mockService = new Mock <IUsersManager>();

            mockService.Setup(s => s.FetchUsersWithMatchingLogin(It.IsAny <string>()))
            .Returns <string>(term => Task.FromResult <List <User> >(
                                  dataSet.Users
                                  .Where(el => el.Login.Contains(term))
                                  .Select(el => el.ToExposed())
                                  .ToList())
                              );
            this.usersManager = mockService.Object;

            List <User> users = await this.usersManager.FetchUsersWithMatchingLogin("b");

            Assert.AreEqual(1, users.Count);
        }
        public async Task FetchCategoriesWithMatchingTitle_One()
        {
            VolatileMainDataset       dataSet     = new VolatileMainDataset();
            Mock <ICategoriesManager> mockService = new Mock <ICategoriesManager>();

            mockService.Setup(s => s.FetchCategoriesWithMatchingTitle(It.IsAny <string>()))
            .Returns <string>(term => Task.FromResult <List <Category> >(
                                  dataSet.Categories
                                  .Where(el => el.Title.Contains(term))
                                  .Select(el => el.ToExposed())
                                  .ToList())
                              );
            this.categoriesManager = mockService.Object;

            List <Category> categories = await this.categoriesManager.FetchCategoriesWithMatchingTitle("1");

            Assert.AreEqual(1, categories.Count);
        }
Example #13
0
        public void DeleteById()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.Delete(It.IsAny <int>()))
            .Callback((object a) =>
            {
                var index = store.Categories.FindIndex(el => el.Id == (int)a);
                store.Categories.RemoveAt(index);
            }).Verifiable();
            this.categoryRepository = mockCategoryRepository.Object;

            this.categoryRepository.Delete(1);

            mockCategoryRepository.Verify(r => r.Delete(It.IsAny <int>()), Times.Once());
            Assert.AreEqual(4, store.Categories.Count);
        }
Example #14
0
        public void Delete()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.Delete(It.IsAny <DbCategory>()))
            .Callback((DbCategory c) =>
            {
                store.Categories.Remove(c);
            }).Verifiable();
            this.categoryRepository = mockCategoryRepository.Object;

            DbCategory category = store.Categories.ElementAt(4);

            this.categoryRepository.Delete(category);

            mockCategoryRepository.Verify(r => r.Delete(It.IsAny <DbCategory>()), Times.Once());
            Assert.AreEqual(4, store.Categories.Count);
        }
Example #15
0
        public async Task DeleteCategoryAsync()
        {
            VolatileMainDataset     store       = new VolatileMainDataset();
            Mock <IBaseMainService> mockService = new Mock <IBaseMainService>();

            mockService.Setup(r => r.DeleteAsync(It.IsAny <DbCategory>()))
            .Returns(Task.FromResult <object>(null))
            .Callback((DbCategory c) =>
            {
                store.Categories.Remove(c);
            }).Verifiable();
            this.service = mockService.Object;

            DbCategory article = store.Categories.ElementAt(4);

            await this.service.DeleteAsync(article);

            mockService.Verify(r => r.DeleteAsync(It.IsAny <DbCategory>()), Times.Once());
            Assert.AreEqual(4, store.Categories.Count);
        }
Example #16
0
        public void Update()
        {
            VolatileMainDataset       store   = new VolatileMainDataset();
            Mock <IImgBoardContext>   context = new Mock <IImgBoardContext>();
            Mock <CategoryRepository> mockCategoryRepository = new Mock <CategoryRepository>(context.Object);

            mockCategoryRepository.Setup(r => r.Update(It.IsAny <DbCategory>()))
            .Callback((DbCategory c) =>
            {
                var index = store.Categories.FindIndex(el => el.Id == c.Id);
                store.Categories[index] = c;
            }).Verifiable();
            this.categoryRepository = mockCategoryRepository.Object;

            DbCategory category = store.Categories.ElementAt(4);
            string     newTitle = category.Title = "c5";

            this.categoryRepository.Update(category);

            mockCategoryRepository.Verify(r => r.Update(It.IsAny <DbCategory>()), Times.Once());
            Assert.AreEqual(newTitle, store.Categories.ElementAt(4).Title);
        }
Example #17
0
        public async Task FetchImagesAsync_ByTwoTags()
        {
            VolatileMainDataset   dataSet     = new VolatileMainDataset();
            Mock <IImagesManager> mockService = new Mock <IImagesManager>();

            mockService
            .Setup(s => s.FetchImagesAsync(It.IsAny <int[]>(), null, null, null, null, null))
            .Returns <int[], int[], string, string, string, string>(
                (tagsIds, categoriesIds, name, description, uploader, extension) =>
                Task.FromResult <List <Image> >(
                    dataSet.Images
                    .Where(i => i.Tags.Any(t => tagsIds.Contains(t.Id)))
                    .Select(el => el.ToExposed())
                    .ToList()
                    )
                );
            this.imagesManager = mockService.Object;

            List <Image> images = await this.imagesManager.FetchImagesAsync(tagsIds : new int[] { 1, 3 });

            Assert.AreEqual(6, images.Count);
        }
Example #18
0
        public async Task UpdateCategoryAsync()
        {
            VolatileMainDataset     store       = new VolatileMainDataset();
            Mock <IBaseMainService> mockService = new Mock <IBaseMainService>();

            mockService.Setup(r => r.ModifyAsync(It.IsAny <DbCategory>()))
            .Returns(Task.FromResult <object>(null))
            .Callback((DbCategory a) =>
            {
                var index = store.Categories.FindIndex(el => el.Id == a.Id);
                store.Categories[index] = a;
            }).Verifiable();
            this.service = mockService.Object;

            DbCategory category = store.Categories.ElementAt(4);
            string     newTitle = category.Title = "a5";

            await this.service.ModifyAsync(category);

            mockService.Verify(r => r.ModifyAsync(It.IsAny <DbCategory>()), Times.Once());
            Assert.AreEqual(newTitle, store.Categories.ElementAt(4).Title);
        }
Example #19
0
        public async Task FetchImagesAsync_ByExtension()
        {
            VolatileMainDataset   dataSet     = new VolatileMainDataset();
            Mock <IImagesManager> mockService = new Mock <IImagesManager>();

            mockService
            .Setup(s => s.FetchImagesAsync(null, null, null, null, null, It.IsAny <string>()))
            .Returns <int[], int[], string, string, string, string>(
                (tagsIds, categoriesIds, name, description, uploader, extension) =>
                Task.FromResult <List <Image> >(
                    dataSet.Images
                    .Where(i => i.FileExtension == extension)
                    .Select(el => el.ToExposed())
                    .ToList()
                    )
                );
            this.imagesManager = mockService.Object;

            List <Image> images = await this.imagesManager.FetchImagesAsync(extension : "jpg");

            Assert.AreEqual(4, images.Count);
        }