public void should_notequals_othertype()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var spec       = !startWithJ;

            Assert.False(spec.Equals(startWithJ));
        }
Esempio n. 2
0
        public void SpecLinquery_Default_ReturnsAll()
        {
            //Arrange
            const int numOfAuthors = 20;

            using (var uow = Stub.CreateMemoryUoW(typeof(Author)))
            {
                uow.AddAuthors(numOfAuthors);

                var linqProviderFactoryStub = new Mock <ILinqProviderFactory>();
                linqProviderFactoryStub.Setup(a => a.Create(It.IsAny <String>(), It.IsAny <String>())).Returns(uow.Linq);

                var handler = new LinqRequestHandler(linqProviderFactoryStub.Object);

                IQuery <IQriteria, IEnumerable <AuthorDto> > query = new SpecLinqQuery <IQriteria, Author, IEnumerable <AuthorDto> >(
                    LinqSpec <Author> .All(),
                    handler,
                    serviceLocator.GetInstance <IEnumLinqConvertor <AuthorDto> >()
                    );

                //Act
                var actual = query.Ask(new NameQriteria());

                //Assert
                Assert.AreEqual(numOfAuthors, actual.Count());
            }
        }
        public void simple_adhoc_equals_itself()
        {
            var specification = LinqSpec.For <string>(n => n.StartsWith("J"));

            Assert.True(specification.Equals(specification));
            Assert.Equal(specification.GetHashCode(), specification.Expression.GetHashCode());
        }
Esempio n. 4
0
        public void FuncSpec_WithCustomFunc()
        {
            using (var uow = CreateMemoryUoW(typeof(Author)))
            {
                const int numOfAuthorsA = 10, numOfAuthorsB = 12;
                var       authorAQrit = new EF.NameQriteria {
                    Name = "A."
                };
                var authorBQrit = new EF.NameQriteria {
                    Name = "B."
                };
                AddAuthors(uow, numOfAuthorsA, authorAQrit.Name);
                AddAuthors(uow, numOfAuthorsB, authorBQrit.Name);

                //Arrange
                var spec = LinqSpec <Author> .FilterFunc <EF.NameQriteria>(
                    (qrit) => author => author.Name.StartsWith(qrit.Name)
                    );

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

                //Assert
                Assert.AreEqual(numOfAuthorsB, actual.Count());
                Assert.IsTrue(actual.Any(a => a.Name.StartsWith(authorBQrit.Name)));
            }
        }
        public void equals_return_true_when_the_negated_spec_are_equals()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));

            var spec = !startWithJ;

            spec.Should().Be.EqualTo(!startWithJ);
        }
        public void should_equals_itself()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var spec       = !startWithJ;

            Assert.True(spec.Equals(spec));
            Assert.Equal(spec.GetHashCode(), spec.GetHashCode());
        }
Esempio n. 7
0
        public AndSpec(LinqSpec <T> spec1, LinqSpec <T> spec2)
        {
            this.spec1 = spec1;
            this.spec2 = spec2;

            // combines the expressions without the need for Expression.Invoke which fails on EntityFramework
            this.expression = spec1.Expression.And(spec2.Expression);
        }
        public void should_not_equal_null()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithE  = LinqSpec.For <string>(n => n.EndsWith("e"));
            var spec       = startWithJ || endsWithE;

            Assert.False(spec.Equals(null));
        }
Esempio n. 9
0
        public void should_not_equals_othertype()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithE  = LinqSpec.For <string>(n => n.EndsWith("e"));
            var spec       = startWithJ & endsWithE;

            Assert.False(spec.Equals(startWithJ | endsWithE));
        }
Esempio n. 10
0
        public void equals_return_false_when_both_sides_are_not_equals()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithE  = LinqSpec.For <string>(n => n.EndsWith("e"));
            var endsWithF  = LinqSpec.For <string>(n => n.EndsWith("f"));
            var spec       = startWithJ & endsWithE;

            spec.Should().Not.Be.EqualTo(startWithJ & endsWithF);
        }
Esempio n. 11
0
            public static LinqSpec <CardTransaction> ForToday(Guid cardId, TimeZoneInfo timeZone)
            {
                var now        = DateTime.UtcNow;
                var localTime  = TimeZoneInfo.ConvertTimeFromUtc(now, timeZone);
                var startOfDay = localTime.Date;
                var endOfDay   = localTime.Date.AddDays(1);

                return(LinqSpec.For <CardTransaction>(x => x.Card.Id == cardId && x.CreatedDateUtc >= startOfDay && x.CreatedDateUtc < endOfDay));
            }
Esempio n. 12
0
        public void should_equal_self()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithE  = LinqSpec.For <string>(n => n.EndsWith("e"));
            var spec       = startWithJ || endsWithE;

            Assert.True(spec.Equals(spec));
            Assert.Equal(spec.GetHashCode(), spec.GetHashCode());
        }
        public void equals_return_false_when_the_negated_spec_are_not_equals()
        {
            var startWithJ       = LinqSpec.For <string>(n => n.StartsWith("J"));
            var anotherAdHocSpec = LinqSpec.For <string>(n => n.StartsWith("dasdas"));

            var spec = !startWithJ;

            spec.Should().Not.Be.EqualTo(!anotherAdHocSpec);
        }
        public LinqSpec <HistoricalTransaction> OriginalKey(params object[] keys)
        {
            Argument.NotNull(keys, "keys");
            Argument.Satisfies(keys, x => x.Length == 1, "keys");
            Argument.Satisfies(keys[0], x => x is Guid);

            var key = (Guid)keys[0];

            return(LinqSpec.For <HistoricalTransaction>(x => x.Id == key));
        }
Esempio n. 15
0
        public static DbQuery <UserBriefModel> ToDbQuery(this UsersQuery query)
        {
            var spec = LinqSpec.For <UserBriefModel>(x => true);

            if (query.Roles != null && query.Roles.Length > 0)
            {
                spec = spec && Specs.HasAtLeastOneRoleFrom(query.Roles);
            }
            return(DbQuery.PagedFor <UserBriefModel>().FromClientQuery(query).AndFilterBy(spec));
        }
        public void negate_operator_should_work()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));

            var result = new SampleRepository()
                         .Retrieve(!startWithJ);

            result.Satisfy(r => !r.Contains("Jose") &&
                           !r.Contains("Julian") &&
                           r.Contains("Manuel"));
        }
        public void simple_adhoc_should_work()
        {
            var specification = LinqSpec.For <string>(n => n.StartsWith("J"));

            var result = new SampleRepository()
                         .Retrieve(specification);

            result.Satisfy(r => r.Contains("Jose") &&
                           r.Contains("Julian") &&
                           !r.Contains("Manuel"));
        }
Esempio n. 18
0
        public void equals_return_true_when_both_sides_are_equals()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithE  = LinqSpec.For <string>(n => n.EndsWith("e"));
            var spec       = startWithJ & endsWithE;

            spec.Should().Be.EqualTo(startWithJ & endsWithE);

            spec.Should("andalso is not conmutable")
            .Not.Be.EqualTo(endsWithE & startWithJ);
        }
Esempio n. 19
0
        public void or_should_work()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithN  = LinqSpec.For <string>(n => n.EndsWith("n"));

            var result = new SampleRepository()
                         .Retrieve(startWithJ | endsWithN);

            result.Satisfy(r => Enumerable.Contains(r, "Jose") &&
                           Enumerable.Contains(r, "Julian") &&
                           !Enumerable.Contains(r, "Manuel"));
        }
Esempio n. 20
0
        public void CombineIntSpec()
        {
            var intGreaterThan4Spec        = LinqSpec.For <int>(i => i > 4);
            var stringLongerThan4CharsSpec = LinqSpec.OnProperty <string, int>(s => s.Length, intGreaterThan4Spec);

            var result = new SampleRepository()
                         .Retrieve(stringLongerThan4CharsSpec);

            result.Satisfy(r => !r.Contains("Jose") &&
                           r.Contains("Julian") &&
                           r.Contains("Manuel"));
        }
Esempio n. 21
0
        public void and_should_work()
        {
            var startWithJ   = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithE    = LinqSpec.For <string>(n => n.EndsWith("e"));
            var specfication = startWithJ && endsWithE;

            IEnumerable <string> result = new SampleRepository()
                                          .Retrieve(specfication);

            result.Satisfy(r => r.Contains("Jose") &&
                           !r.Contains("Julian") &&
                           !r.Contains("Manuel"));
        }
Esempio n. 22
0
        public void query_sample()
        {
            var startWithM = new StartsWithQuery("M");
            var endsWithN  = LinqSpec.For <string>(n => n.EndsWith("n"));

            IEnumerable <string> result = new SampleRepository()
                                          .Retrieve(startWithM | !endsWithN);

            result.Satisfy(r =>
                           Enumerable.Contains(r, "Jose") &&
                           !Enumerable.Contains(r, "Julian") &&
                           Enumerable.Contains(r, "Manuel"));
        }
Esempio n. 23
0
        public void and_operator_should_work()
        {
            var startWithJ = LinqSpec.For <string>(n => n.StartsWith("J"));
            var endsWithE  = LinqSpec.For <string>(n => n.EndsWith("e"));


            // & or && both operators behave as &&.

            IEnumerable <string> result = new SampleRepository()
                                          .Retrieve(startWithJ & endsWithE);

            result.Satisfy(r => r.Contains("Jose") &&
                           !r.Contains("Julian") &&
                           !r.Contains("Manuel"));
        }
Esempio n. 24
0
        public void can_cast_expression_as_queryspec()
        {
            Expression <Func <string, bool> > startExpr = n => n.StartsWith("M");
            Expression <Func <string, bool> > endExpr   = n => n.EndsWith("n");

            LinqSpec <string> startWithM = startExpr;
            LinqSpec <string> endsWithN  = endExpr;

            IEnumerable <string> result = new SampleRepository()
                                          .Retrieve(startWithM | !endsWithN);

            result.Satisfy(r =>
                           Enumerable.Contains(r, "Jose") &&
                           !Enumerable.Contains(r, "Julian") &&
                           Enumerable.Contains(r, "Manuel"));
        }
Esempio n. 25
0
        public void EmptyLinqSpec_ReturnsAll()
        {
            //Arrange
            const int numOfAuthors = 5;

            using (var uow = CreateMemoryUoW(typeof(Author)))
            {
                AddAuthors(uow, numOfAuthors);
                var spec = LinqSpec <Author> .All();

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

                //Assert
                Assert.AreEqual(numOfAuthors, actual.Count());
            }
        }
Esempio n. 26
0
        public void ExpressionSpec_WithExpression()
        {
            //Arrange
            const int numOfAuthors = 20, id = 10;

            using (var uow = CreateMemoryUoW(typeof(Author)))
            {
                AddAuthors(uow, numOfAuthors);

                //var linqProviderFactoryStub = new Mock<ILinqProviderFactory>();
                //linqProviderFactoryStub.Setup(a => a.Create(It.IsAny<String>(), It.IsAny<String>())).Returns(uow.Linq);
                //ILinqQueryHandler handler = new LinqQueryHandler(linqProviderFactoryStub.Object);

                var spec = LinqSpec <Author> .Filter <IQriteria>((a) => a.Id > id);

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

                //Assert
                Assert.IsFalse(actual.Any(a => a.Id <= id));
            }
        }
Esempio n. 27
0
        public void ConventionSpec_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 AuthorQrit {
                    Name = "C"
                };
                var spec = LinqSpec <Author> .ByConvention(qrit);

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

                //Assert
                Assert.IsTrue(actual.All(a => a.Name.StartsWith(qrit.Name)));
            }
        }
Esempio n. 28
0
 private IQueryable<Bet> GetBetsQuery(LinqSpec<Bet> specification)
 {
     return _repositoryOfBet.
         Get(specification).
         Include(bb => bb.OpenBetScreenshot).
         Include(bb => bb.CloseBetScreenshot).
         OrderByDescending(b => b.OpenDateTime);
 }
        public void should_not_equals_othertype()
        {
            var specification = LinqSpec.For <string>(n => n.StartsWith("J"));

            Assert.False(specification.Equals(specification | LinqSpec.For <string>(n => true)));
        }
        public void should_not_equals_null()
        {
            var specification = LinqSpec.For <string>(n => n.StartsWith("J"));

            Assert.False(specification.Equals(null));
        }
Esempio n. 31
0
    /// <summary>
    /// Allows reusing a specification for another Type by applying it to a property
    /// of the Type being targetted by the new specification.
    /// </summary>
    /// <typeparam name="T">The type of the new LinqSpec to be created</typeparam>
    /// <typeparam name="TNestedEntity">The type of the property on the T which is being accessed</typeparam>
    /// <param name="propertyExpression">The property expression to access property on T. Usually a lambda like t => t.MyProp</param>
    /// <param name="spec">A LinqSpec for type TNestedEntity</param>
    public static LinqSpec <T> OnProperty <T, TNestedEntity>(Expression <Func <T, TNestedEntity> > propertyExpression, LinqSpec <TNestedEntity> spec)
    {
        var replacer      = new ParameterReplaceVisitor(spec.Expression.Parameters.First(), propertyExpression.Body);
        var newExpression = replacer.Visit(spec.Expression.Body);
        var exp           = Expression.Lambda <Func <T, bool> >(newExpression, propertyExpression.Parameters);

        return(For(exp));
    }