Exemplo n.º 1
0
        public void ConventionPageSpec_Returns_Ordered()
        {
            //Arrange
            using (var uow = CreateMemoryUoW(typeof(Author)))
            {
                uow.Add(new Author(1, "A.D.Author"));
                uow.Add(new Author(2, "A.B.Author"));
                uow.Add(new Author(3, "B.A.Author"));
                uow.Add(new Author(4, "A.C.Author"));
                uow.Add(new Author(5, "B.E.Author"));

                var qrit = new PageQriteria <AuthorDto>
                {
                    Subject = new AuthorDto {
                        Name = "A"
                    },
                    OrderBy  = "Name",
                    PageNo   = 2,
                    PageSize = 2
                };
                var spec = new ConventionPagedSpec <Author, AuthorDto>();

                //Act
                var actual = spec.Query(uow.Linq, qrit)
                             .Cast <Author>();

                //Assert
                Assert.IsTrue(actual.All(a => a.Name.StartsWith(qrit.Subject.Name)));
                CollectionAssert.AreEqual(actual.ToArray(),
                                          uow.Linq.Query <Author>().Where(a => a.Name.StartsWith(qrit.Subject.Name)).OrderBy(a => a.Name).ToArray());
            }
        }
Exemplo n.º 2
0
        public void ConventionPageSpec_Returns_Filtered()
        {
            //Arrange
            using (var uow = CreateMemoryUoW(typeof(Author)))
            {
                uow.Add(new Author(1, "A.A.Author"));
                uow.Add(new Author(2, "A.B.Author"));
                uow.Add(new Author(3, "C.D.Author"));
                uow.Add(new Author(4, "C.D.Author"));
                uow.Add(new Author(5, "E.E.Author"));

                var qrit = new PageQriteria <AuthorDto>
                {
                    Subject = new AuthorDto {
                        Name = "C"
                    },
                    OrderBy  = "Name",
                    PageNo   = 2,
                    PageSize = 2
                };
                var spec = new ConventionPagedSpec <Author, AuthorDto>();                //LinqSpec<Author>.ByConvention(qrit);

                //Act
                var actual = spec.Query(uow.Linq, qrit)
                             .Cast <Author>();

                //Assert
                Assert.IsTrue(actual.All(a => a.Name.StartsWith(qrit.Subject.Name)));
            }
        }
Exemplo n.º 3
0
        public void QueryForPageQriteria_Returns_Page()
        {
            //Arrange
            const int authorCount = 25;

            using (var UoWStub = Stub.CreateMemoryUoW(typeof(Author))
                                 .Add(
                       authorCount,
                       (i) => new Author(i, "A" + (i % 2 == 0 ? "_" : "-") + Convert.ToString(i).PadLeft(2, '0'))
                       ))
            {
                var linqProviderFactoryStub = UoWStub.LinqProviderFactory();

                var handler = new LinqRequestHandler(linqProviderFactoryStub);

                var convertor = serviceLocator.GetInstance <IPageConvertor <AuthorDto> >();

                var projector = serviceLocator.GetInstance <IProjector>();

                var qrit = new PageQriteria <AuthorDto>
                {
                    PageNo   = 3,
                    PageSize = 5,
                    OrderBy  = "Name",
                    Subject  = new AuthorDto {
                        Name = "A"
                    }
                };


                var query = new ConventionPagedQuery <AuthorDto, AuthorDto>(handler, convertor);

                var expected = linqProviderFactoryStub.Create()
                               .Query <Author>()
                               .Where(a => a.Name.StartsWith(qrit.Subject.Name))
                               .OrderBy(a => a.Name)
                               .Skip((qrit.PageNo - 1) * qrit.PageSize)
                               .Take(qrit.PageSize)
                               .Project <AuthorDto>(projector)
                               .ToArray();

                //Act
                var actual = query.Ask(qrit);

                //Assert
                CollectionAssert.AreEqual(expected, actual.Members.ToArray(), new AuthorDtoComparer());
            }
        }
Exemplo n.º 4
0
        public void PageSpec_Returns_FilteredAndSortedByConvention()
        {
            //Arrange
            const int authorCount = 25;

            using (var UoWStub = Stub.CreateMemoryUoW(typeof(Author))
                                 .Add(
                       authorCount,
                       (i) => new Author(i, "A" + (i % 2 == 0 ? "=" : "-") + Convert.ToString(i).PadLeft(2, '0'))
                       ))
            {
                var linqProvider = UoWStub.Linq;

                var qrit = new PageQriteria <AuthorDto>
                {
                    PageNo   = 3,
                    PageSize = 5,
                    OrderBy  = "Name",
                    Subject  = new AuthorDto {
                        Name = "A"
                    }
                };

                var expected = linqProvider.Query <Author>()
                               .Where(a => a.Name.StartsWith(qrit.Subject.Name))
                               .OrderBy(a => a.Name)
                               .ToArray();

                var spec = new ConventionPagedSpec <Author, AuthorDto>();

                var stopWatch = new Stopwatch();

                //Act
                stopWatch.Start();
                var actual = spec.Query(linqProvider, qrit)
                             .Cast <Author>()
                             .ToArray();
                stopWatch.Stop();

                //Assert
                CollectionAssert.AreEqual(expected, actual);
                Assert.IsTrue(stopWatch.ElapsedMilliseconds < 100);
            }
        }