public async Task QueryWithComplexPredicate() { QueryResult <Account> acctualResult; var accountQuery = Initializer.Container.Resolve <IQuery <Account> >(); var expecterResult = new QueryResult <Account>(new List <Account> { accountIvan }, 1); var predicate = new CompositePredicate( new List <IPredicate> { new SimplePredicate(nameof(Account.Email), ValueComparingOperator.StringContains, ".com"), new SimplePredicate(nameof(Account.Email), ValueComparingOperator.StringContains, "@ivan"), }); using (unitOfWorkProvider.Create()) { acctualResult = await accountQuery.Where(predicate).ExecuteAsync(); } Assert.AreEqual(expecterResult, acctualResult); }
protected override IQuery <Auction> ApplyWhereClause(IQuery <Auction> query, AuctionFilterDto filter) { var definedPredicates = new List <IPredicate>(); AddIfDefined(FilterItemName(filter), definedPredicates); AddIfDefined(FilterSeller(filter), definedPredicates); AddIfDefined(FilterCategories(filter), definedPredicates); AddIfDefined(FilterOnlyActive(filter), definedPredicates); AddIfDefined(FilterMaximalPrice(filter), definedPredicates); AddIfDefined(FilterMinimalPrice(filter), definedPredicates); AddIfDefined(FilterMinimalEndTime(filter), definedPredicates); AddIfDefined(FilterMaximalEndTime(filter), definedPredicates); if (definedPredicates.Count == 0) { return(query); } if (definedPredicates.Count == 1) { return(query.Where(definedPredicates.First())); } var resultPredicate = new CompositePredicate(definedPredicates); return(query.Where(resultPredicate)); }
public async Task ExecuteAsync_ComplexWherePredicate_ReturnsCorrectQueryResult() { QueryResult <Freelancer> actualQueryResult; var categoryQuery = Initializer.Container.Resolve <IQuery <Freelancer> >(); var expectedQueryResult = new QueryResult <Freelancer>(new List <Freelancer> { new Freelancer(Sex.MALE, new DateTime(1968, 12, 5), "Bardejov", "*****@*****.**", "Karol Kovach", "som super") }, 1); var predicate = new CompositePredicate(new List <IPredicate> { new SimplePredicate(nameof(Freelancer.Sex), ValueComparingOperator.Equal, Sex.MALE), new CompositePredicate(new List <IPredicate> { new SimplePredicate(nameof(Freelancer.Location), ValueComparingOperator.Equal, "Brno"), new SimplePredicate(nameof(Freelancer.Location), ValueComparingOperator.Equal, "Liberec") }, LogicalOperator.OR) }); using (unitOfWorkProvider.Create()) { actualQueryResult = await categoryQuery.Where(predicate).ExecuteAsync(); } Assert.AreEqual(actualQueryResult, expectedQueryResult); }
public async Task ApplyWhereClause_FilterByUserAge_ReturnsCorrectPredicate() { const int filteredUserAge = 30; var mockManager = new QueryMockManager(); var agePredicates = new List <IPredicate> { new SimplePredicate(nameof(Post.HasAgeRestriction), ValueComparingOperator.Equal, false) }; var innerAgePredicates = new List <IPredicate> { new SimplePredicate(nameof(Post.AgeRestrictionTo), ValueComparingOperator.LessThanOrEqual, filteredUserAge), new SimplePredicate(nameof(Post.AgeRestrictionFrom), ValueComparingOperator.GreaterThanOrEqual, filteredUserAge) }; agePredicates.Add(new CompositePredicate(innerAgePredicates, LogicalOperator.AND)); var expectedPredicate = new CompositePredicate( new List <IPredicate> { new CompositePredicate(agePredicates, LogicalOperator.OR) }, LogicalOperator.AND); var mapperMock = mockManager.ConfigureMapperMock <Post, PostDto, PostFilterDto>(); var queryMock = mockManager.ConfigureQueryMock <Post>(); var postQueryObject = new PostQueryObject(mapperMock.Object, queryMock.Object); var filter = new PostFilterDto { UserAge = filteredUserAge }; var temp = await postQueryObject.ExecuteQuery(filter); Assert.AreEqual(expectedPredicate, mockManager.CapturedPredicate); }
public async Task ApplyWhereClause_FilterByGenderRestriction_ReturnsCorrectPredicate() { const Gender filteredGenderRestriction = Gender.Female; var mockManager = new QueryMockManager(); var expectedPredicate = new CompositePredicate( new List <IPredicate> { new CompositePredicate( new List <IPredicate> { new SimplePredicate(nameof(Post.GenderRestriction), ValueComparingOperator.Equal, filteredGenderRestriction), new SimplePredicate(nameof(Post.GenderRestriction), ValueComparingOperator.Equal, Gender.NoInformation) }, LogicalOperator.OR) }, LogicalOperator.AND); var mapperMock = mockManager.ConfigureMapperMock <Post, PostDto, PostFilterDto>(); var queryMock = mockManager.ConfigureQueryMock <Post>(); var postQueryObject = new PostQueryObject(mapperMock.Object, queryMock.Object); var filter = new PostFilterDto { GenderRestriction = filteredGenderRestriction }; var temp = await postQueryObject.ExecuteQuery(filter); Assert.AreEqual(expectedPredicate, mockManager.CapturedPredicate); }
public async Task ExecuteAsync_ComplexWherePredicate_ReturnsCorrectQueryResult() { QueryResult <Category> actualQueryResult; var categoryQuery = Initializer.Container.Resolve <IQuery <Category> >(); var expectedQueryResult = new QueryResult <Category>(new List <Category> { new Category { Id = androidCategoryId, Name = "Android", ParentId = smartphonesCategoryId } }, 1); var predicate = new CompositePredicate(new List <IPredicate> { new SimplePredicate(nameof(Category.ParentId), ValueComparingOperator.Equal, smartphonesCategoryId), new CompositePredicate(new List <IPredicate> { new SimplePredicate(nameof(Category.Name), ValueComparingOperator.Equal, "Android"), new SimplePredicate(nameof(Category.Name), ValueComparingOperator.Equal, "Windows 10") }, LogicalOperator.OR) }); using (unitOfWorkProvider.Create()) { actualQueryResult = await categoryQuery.Where(predicate).ExecuteAsync(); } Assert.AreEqual(actualQueryResult, expectedQueryResult); }
private Expression CombineBinaryExpressions(CompositePredicate compositePredicate) { if (compositePredicate.Predicates.Count == 0) { throw new InvalidOperationException("At least one simple predicate must be given"); } var expression = compositePredicate.Predicates.First() is CompositePredicate composite ? CombineBinaryExpressions(composite) : BuildBinaryExpression(compositePredicate.Predicates.First()); for (var i = 1; i < compositePredicate.Predicates.Count; i++) { if (compositePredicate.Predicates[i] is CompositePredicate predicate) { expression = compositePredicate.Operator == LogicalOperator.OR ? Expression.OrElse(expression, CombineBinaryExpressions(predicate)) : Expression.AndAlso(expression, CombineBinaryExpressions(predicate)); } else { expression = compositePredicate.Operator == LogicalOperator.OR ? Expression.OrElse(expression, BuildBinaryExpression(compositePredicate.Predicates[i])) : Expression.AndAlso(expression, BuildBinaryExpression(compositePredicate.Predicates[i])); } } return(expression); }
public void use_multiple_predicates() { var composite = new CompositePredicate <string>(); composite += x => x.StartsWith("a"); composite += x => x.EndsWith("x"); composite.MatchesAll("a").ShouldBeFalse(); composite.MatchesAll("x").ShouldBeFalse(); composite.MatchesAll("abx").ShouldBeTrue(); }
protected override IQuery <Friendship> ApplyWhereClause(IQuery <Friendship> query, FriendshipFilterDto filter) { var wherePredicate = new CompositePredicate(new List <IPredicate> { new SimplePredicate(nameof(Friendship.User1Id), ValueComparingOperator.Equal, filter.UserId), new SimplePredicate(nameof(Friendship.User2Id), ValueComparingOperator.Equal, filter.UserId) }, LogicalOperator.OR); return(filter.UserId.Equals(null) ? query : query.Where(wherePredicate)); }
public void no_predicates_means_that_it_always_true() { var composite = new CompositePredicate<string>(); composite.MatchesAll("a").ShouldBeTrue(); composite.MatchesAll("b").ShouldBeTrue(); composite.MatchesAll("c").ShouldBeTrue(); composite.MatchesAll("d").ShouldBeTrue(); composite.MatchesAll("e").ShouldBeTrue(); composite.MatchesAll("f").ShouldBeTrue(); composite.MatchesAll("g").ShouldBeTrue(); }
public async Task ApplyWhereClause_ComplexFilterByAllParameters_ReturnsCorrectPredicate() { const int filteredUserAge = 30; Gender filteredGenderRestriction = Gender.Female; PostVisibility filteredPostVisibility = PostVisibility.FriendsOnly; Guid filteredUserId = Guid.NewGuid(); var mockManager = new QueryMockManager(); var agePredicates = new List <IPredicate> { new SimplePredicate(nameof(Post.HasAgeRestriction), ValueComparingOperator.Equal, false) }; var innerAgePredicates = new List <IPredicate> { new SimplePredicate(nameof(Post.AgeRestrictionTo), ValueComparingOperator.LessThanOrEqual, filteredUserAge), new SimplePredicate(nameof(Post.AgeRestrictionFrom), ValueComparingOperator.GreaterThanOrEqual, filteredUserAge) }; agePredicates.Add(new CompositePredicate(innerAgePredicates, LogicalOperator.AND)); var genderPredicate = new CompositePredicate( new List <IPredicate> { new SimplePredicate(nameof(Post.GenderRestriction), ValueComparingOperator.Equal, filteredGenderRestriction), new SimplePredicate(nameof(Post.GenderRestriction), ValueComparingOperator.Equal, Gender.NoInformation) }, LogicalOperator.OR); var userIdPredicate = new SimplePredicate(nameof(Post.UserId), ValueComparingOperator.Equal, filteredUserId); var visibilityPredicate = new SimplePredicate(nameof(Post.Visibility), ValueComparingOperator.Equal, filteredPostVisibility); var expectedPredicate = new CompositePredicate( new List <IPredicate> { new CompositePredicate(agePredicates, LogicalOperator.OR), genderPredicate, userIdPredicate, visibilityPredicate }, LogicalOperator.AND); var mapperMock = mockManager.ConfigureMapperMock <Post, PostDto, PostFilterDto>(); var queryMock = mockManager.ConfigureQueryMock <Post>(); var postQueryObject = new PostQueryObject(mapperMock.Object, queryMock.Object); var filter = new PostFilterDto { UserAge = filteredUserAge, GenderRestriction = filteredGenderRestriction, UserId = filteredUserId }; var temp = await postQueryObject.ExecuteQuery(filter); Assert.AreEqual(expectedPredicate, mockManager.CapturedPredicate); }
protected override IQuery <Chat> ApplyWhereClause(IQuery <Chat> query, ChatFilterDto filter) { var predicate = new CompositePredicate(new List <IPredicate> { new SimplePredicate(nameof(Chat.ReceiverId), ValueComparingOperator.Equal, filter.CharacterId), new SimplePredicate(nameof(Chat.SenderId), ValueComparingOperator.Equal, filter.CharacterId) }, LogicalOperator.OR); return(!filter.CharacterId.HasValue ? query : query.Where(predicate)); }
public void registered_predicates_are_used() { var composite = new CompositePredicate <string>(); composite.MatchesAll("a").ShouldBeTrue(); composite.MatchesAll("b").ShouldBeTrue(); composite += x => x == "a"; composite.MatchesAll("a").ShouldBeTrue(); composite.MatchesAll("b").ShouldBeFalse(); }
public void no_predicates_means_that_it_always_true() { var composite = new CompositePredicate <string>(); composite.MatchesAll("a").ShouldBeTrue(); composite.MatchesAll("b").ShouldBeTrue(); composite.MatchesAll("c").ShouldBeTrue(); composite.MatchesAll("d").ShouldBeTrue(); composite.MatchesAll("e").ShouldBeTrue(); composite.MatchesAll("f").ShouldBeTrue(); composite.MatchesAll("g").ShouldBeTrue(); }
public void ApplyFilter_EventTypeThreeOtherTag_OneVisible() { // Arrange timelineVM.Filters.Active = false; CompositePredicate <TimelineEventVM> fifttEventTypePredicate = ((CompositePredicate <TimelineEventVM>)timelineVM.EventTypesPredicate.Elements [4]); // Act fifttEventTypePredicate.Elements [2].Active = true; // Assert Assert.AreEqual(1, timelineVM.FullTimeline.Count(e => e.Visible)); Assert.IsTrue(timelineVM.FullTimeline.ElementAt(7).Visible); }
public ParameterMatcher(Type typeToCreate, IEnumerable <ConstructorParameter> constructorArguments, IParameterMatchingConventionsPolicy conventions) { Guard.AssertNotNull(constructorArguments, "ctorArgs"); Guard.AssertNotNull(conventions, "conventions"); this._typeToCreate = typeToCreate; this.constructorArguments = constructorArguments; this.conventions = conventions; this.filters = new CompositePredicate <ConstructorInfo>(); this.filters += this.ConstructorDoesNotTakeAllArguments; this.filters += this.NonSatisfiedPrimitiveArgs; }
public void ApplyFilter_EventTypeTwoBad_NoneVisible() { // Arrange timelineVM.Filters.Active = false; CompositePredicate <TimelineEventVM> secondEventTypePredicate = ((CompositePredicate <TimelineEventVM>)timelineVM.EventTypesPredicate.Elements [1]); // Act secondEventTypePredicate.Elements [1].Active = true; // Assert Assert.IsTrue(timelineVM.FullTimeline.All(e => !e.Visible)); }
public void matches_none() { var composite = new CompositePredicate<string>(); composite.MatchesNone("a").ShouldBeFalse(); composite += x => x == "b"; composite.MatchesNone("a").ShouldBeTrue(); composite += x => x == "a"; composite.MatchesNone("a").ShouldBeFalse(); }
protected void UpdatePreviousPredicatesList(CompositePredicate <TimelineEventVM> compositePredicate) { foreach (var predicate in compositePredicate) { if (!previousPredicateList.Exists(p => p.Name == predicate.Name)) { previousPredicateList.Add(predicate); } else { previousPredicateList.FirstOrDefault(p => p.Name == predicate.Name).Active = predicate.Active; } } }
public void or_testing() { var composite = new CompositePredicate <string>(); composite.MatchesAny("a").ShouldBeTrue(); composite += x => x == "b"; composite.MatchesAny("a").ShouldBeFalse(); composite += x => x.StartsWith("a"); composite.MatchesAny("a").ShouldBeTrue(); }
public void or_testing() { var composite = new CompositePredicate<string>(); composite.MatchesAny("a").ShouldBeTrue(); composite += x => x == "b"; composite.MatchesAny("a").ShouldBeFalse(); composite += x => x.StartsWith("a"); composite.MatchesAny("a").ShouldBeTrue(); }
public IQuery <TEntity> MergePredicates(List <IPredicate> predicates, LogicalOperator logicalOperator = LogicalOperator.AND) { if (predicates.Count == 0) { return(Query); } if (predicates.Count == 1) { return(Query.Where(predicates.First())); } var wherePredicate = new CompositePredicate(predicates, logicalOperator); return(Query.Where(wherePredicate)); }
public async Task ApplyWhereClause_SimpleFilterWithMinimalAndMaximalPrice_ReturnsCorrectCompositePredicate() { var mockManager = new QueryMockManager(); var expectedPredicate = new CompositePredicate(new List <IPredicate> { new SimplePredicate(nameof(Song.Price), ValueComparingOperator.GreaterThanOrEqual, 1000m), new SimplePredicate(nameof(Song.Price), ValueComparingOperator.LessThanOrEqual, 3000m) }); var mapperMock = mockManager.ConfigureMapperMock <Song, ProductDto, ProductFilterDto>(); var queryMock = mockManager.ConfigureQueryMock <Song>(); var productQueryObject = new ProductQueryObject(mapperMock.Object, queryMock.Object); var unused = await productQueryObject.ExecuteQuery(new ProductFilterDto { MinimalPrice = 1000, MaximalPrice = 3000 }); Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate); }
protected override IQuery <Album> ApplyWhereClause(IQuery <Album> query, AlbumFilterDto filter) { if (filter.Names == null || !filter.Names.Any()) { return(query); } var albumNamePredicates = new List <IPredicate>(filter.Names .Select(name => new SimplePredicate( nameof(Album.Name), ValueComparingOperator.Equal, name))); var predicate = new CompositePredicate(albumNamePredicates, LogicalOperator.OR); return(query.Where(predicate)); }
public void ApplyFilter_EventTypeOneBad_TwoVisible() { // Arrange timelineVM.Filters.Active = false; CompositePredicate <TimelineEventVM> firstEventTypePredicate = ((CompositePredicate <TimelineEventVM>)timelineVM.EventTypesPredicate.Elements [0]); // Act firstEventTypePredicate.Elements [1].Active = true; // Assert Assert.AreEqual(2, timelineVM.FullTimeline.Count(e => e.Visible)); Assert.IsTrue(timelineVM.FullTimeline.ElementAt(1).Visible); Assert.IsTrue(timelineVM.FullTimeline.ElementAt(3).Visible); }
protected override IQuery<Category> ApplyWhereClause(IQuery<Category> query, CategoryFilterDto filter) { if (filter.Names == null || !filter.Names.Any()) { return query; } var categoryNamePredicates = new List<IPredicate>(filter.Names .Select(name => new SimplePredicate( nameof(Category.Name), ValueComparingOperator.Equal, name))); var predicate = new CompositePredicate(categoryNamePredicates, LogicalOperator.OR); return query.Where(predicate); }
protected override IQuery <Question> ApplyWhereClause(IQuery <Question> query, QuestionFilterDto filter) { if (filter.Keywords == null || !filter.Keywords.Any()) { return(query); } var predicates = new List <IPredicate>(filter.Keywords .Select(keyword => new SimplePredicate( nameof(Question.Text), ValueComparingOperator.StringContains, keyword))); var predicate = new CompositePredicate(predicates, LogicalOperator.OR); return(query.Where(predicate)); }
public void matches_none() { var composite = new CompositePredicate <string>(); composite.MatchesNone("a").ShouldBeFalse(); composite += x => x == "b"; composite.MatchesNone("a").ShouldBeTrue(); composite += x => x == "a"; composite.MatchesNone("a").ShouldBeFalse(); }
public static string BuildCompositePredicate(this CompositePredicate compositePredicate) { if (compositePredicate.Predicates.Count == 0) { throw new InvalidOperationException("At least one simple predicate must be given"); } var sql = SqlConstants.OpenParenthesis; sql += ChoosePredicate(compositePredicate, 0); for (var i = 1; i < compositePredicate.Predicates.Count; i++) { sql += compositePredicate.Operator == LogicalOperator.OR ? SqlConstants.Or : SqlConstants.And; sql += ChoosePredicate(compositePredicate, i); } return(sql + SqlConstants.CloseParenthesis); }
public async Task ApplyWhereClause_SimpleFilterWithName_ReturnsCorrectCompositePredicate() { var mockManager = new QueryMockManager(); var predicateList = new List <IPredicate> { new SimplePredicate(nameof(Patient.Name), ValueComparingOperator.Equal, "Harry"), new SimplePredicate(nameof(Patient.Surname), ValueComparingOperator.Equal, "Maybourne") }; var expectedPredicate = new CompositePredicate(predicateList); var mapperMock = mockManager.ConfigureMapperMock <Patient, PatientDto, PatientFilterDto>(); var queryMock = mockManager.ConfigureQueryMock <Patient>(); var patientQueryObject = new PatientQueryObject(mapperMock.Object, queryMock.Object); var unused = await patientQueryObject.ExecuteQuery(new PatientFilterDto { FullName = "Harry Maybourne" }); Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate); }
public async Task ApplyWhereClause_SimpleFilterWithName_ReturnsCorrectCompositePredicate() { var mockManager = new QueryMockManager(); var predicateList = new List <IPredicate> { new SimplePredicate(nameof(Doctor.Name), ValueComparingOperator.Equal, "John"), new SimplePredicate(nameof(Doctor.Surname), ValueComparingOperator.Equal, "Doe") }; var expectedPredicate = new CompositePredicate(predicateList); var mapperMock = mockManager.ConfigureMapperMock <Doctor, DoctorDto, DoctorFilterDto>(); var queryMock = mockManager.ConfigureQueryMock <Doctor>(); var doctorQueryObject = new DoctorQueryObject(mapperMock.Object, queryMock.Object); var unused = await doctorQueryObject.ExecuteQuery(new DoctorFilterDto { FullName = "John Doe" }); Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate); }
protected override IQuery <Review> ApplyWhereClause(IQuery <Review> query, ReviewFilterDto filter) { var definedPredicates = new List <IPredicate>(); AddIfDefined(FilterUsers(filter), definedPredicates); AddIfDefined(FilterEvaluations(filter), definedPredicates); if (definedPredicates.Count == 0) { return(query); } if (definedPredicates.Count == 1) { return(query.Where(definedPredicates.First())); } var wherePredicate = new CompositePredicate(definedPredicates); return(query.Where(wherePredicate)); }
protected override IQuery <ItemCategory> ApplyWhereClause(IQuery <ItemCategory> query, ItemCategoryFilterDto filter) { var definedPredicates = new List <IPredicate>(); AddIfDefined(FilterUser(filter), definedPredicates); AddIfDefined(FilterCategory(filter), definedPredicates); if (definedPredicates.Count == 0) { return(query); } if (definedPredicates.Count == 1) { return(query.Where(definedPredicates.First())); } var wherePredicate = new CompositePredicate(definedPredicates); return(query.Where(wherePredicate)); }
public void registered_predicates_are_used() { var composite = new CompositePredicate<string>(); composite.MatchesAll("a").ShouldBeTrue(); composite.MatchesAll("b").ShouldBeTrue(); composite += x => x == "a"; composite.MatchesAll("a").ShouldBeTrue(); composite.MatchesAll("b").ShouldBeFalse(); }
public void use_multiple_predicates() { var composite = new CompositePredicate<string>(); composite += x => x.StartsWith("a"); composite += x => x.EndsWith("x"); composite.MatchesAll("a").ShouldBeFalse(); composite.MatchesAll("x").ShouldBeFalse(); composite.MatchesAll("abx").ShouldBeTrue(); }