public void ThrowBadRequestIfQueriedPropertyIsNotAString() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules { MemberName = "IntProp", MemberValue = "15" }; var pocos = Enumerable.Range(1, 10).Select(i => new Poco { Prop = string.Format("La {0}a la", i) }).ToList(); // Act try { lambda.CreateContainsLambda <Poco>(contains.MemberName, contains.MemberValue); // Assert Assert.Fail("Should throw exception"); } catch (BadRequestException exception) { } catch (Exception e) { Assert.Fail("Should throw bad request exception"); } }
public void ParseObjectExpressionsSuccessfully() { var rules = new ContainsFilterRules { MemberName = "value", MemberValue = "10" }; var entities = Enumerable.Range(1, 10).Select(i => new { id = i, value = "lala" + i.ToString(CultureInfo.InvariantCulture) }).ToList(); var lamdaCreator = new LambdaCreator(); var filter = new EntityFilter(lamdaCreator); var res = filter.FilterWithContains(entities, rules); Assert.AreEqual(1, res.Count); Assert.AreEqual(10, res[0].id); }
public void PerformCaseInsensitiveSearchSuccessfully() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules { MemberName = "Prop", MemberValue = "10A" }; var pocos = Enumerable.Range(1, 10).Select(i => new Poco { Prop = string.Format("La {0}a la", i), IntProp = i }).ToList(); // Act var mce = lambda.CreateContainsLambda <Poco>(contains.MemberName, contains.MemberValue); var result = pocos.AsQueryable().Where(mce).ToList(); // Assert Assert.AreEqual(10, result.Single().IntProp); }
public void CreateWhereEqualsLambdaSuccessfully() { // Arrange const string propertyName = "IntProp"; const int propertyValue = 2; var entities = Enumerable.Range(1, 2).SelectMany(i => Enumerable.Repeat(new Poco { IntProp = i }, 10)).ToList(); var lambdaCreator = new LambdaCreator(); // Act Expression <Func <Poco, bool> > equalsLambda = lambdaCreator.CreateEqualsLambda <Poco>(propertyName, propertyValue); var result = entities.AsQueryable().Where(equalsLambda).ToList(); // Assert Assert.AreEqual(10, result.Count); Assert.IsTrue(result.All(i => i.IntProp == 2)); }
public void FilterWithEqualsSuccessfully() { // Arrange const string propertyName = "IntProp"; var equals = new EqualsFilterRules <int> { MemberName = propertyName, MemberValue = 2 }; var entities = Enumerable.Range(1, 2).SelectMany(i => Enumerable.Repeat(new LambdaCreatorShould.Poco { IntProp = i }, 10)).ToList(); var lambdaCreator = new LambdaCreator(); IFilter entityFilter = new EntityFilter(lambdaCreator); // Act List <LambdaCreatorShould.Poco> res = entityFilter.FilterWithEquals(entities, @equals); // Assert Assert.AreEqual(10, res.Count); Assert.IsTrue(res.All(elem => elem.IntProp == 2)); }
public void SkipElementOfCollectionIfItsQueriedPropertyIsNull() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules { MemberName = "Prop", MemberValue = "la" }; var pocos = Enumerable.Range(1, 10).Select(i => new Poco { Prop = string.Format("La {0}a la", i) }).ToList(); pocos.Add(new Poco()); // Prop = null. // Act var mce = lambda.CreateContainsLambda <Poco>(contains.MemberName, contains.MemberValue); var result = pocos.AsQueryable().Where(mce).ToList(); // Assert Assert.AreEqual(pocos.Count - 1, result.Count); }
public void CreateWhereContainsLambdaSuccessfully() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules {MemberName = "Prop", MemberValue = "2a"}; var pocos = Enumerable.Range(1, 10).Select(i => new Poco {Prop = string.Format("La {0}a la", i)}).ToList(); Poco expected = pocos[1]; // Act var mce = lambda.CreateContainsLambda<Poco>(contains.MemberName, contains.MemberValue); var res = pocos.AsQueryable().Where(mce); var res2 = pocos.Where(p => p.Prop.Contains(contains.MemberValue)).ToList(); // Assert Assert.AreEqual(1, res.Count()); Assert.AreEqual(expected.Prop, res.First().Prop); Assert.AreEqual(res2.First().Prop, res.First().Prop); }
public void ThrowBadRequestIfQueriedPropertyIsNotAString() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules { MemberName = "IntProp", MemberValue = "15" }; var pocos = Enumerable.Range(1, 10).Select(i => new Poco { Prop = string.Format("La {0}a la", i) }).ToList(); // Act try { lambda.CreateContainsLambda<Poco>(contains.MemberName, contains.MemberValue); // Assert Assert.Fail("Should throw exception"); } catch (BadRequestException exception) { } catch (Exception e) { Assert.Fail("Should throw bad request exception"); } }
public void CreateWhereContainsLambdaSuccessfully() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules { MemberName = "Prop", MemberValue = "2a" }; var pocos = Enumerable.Range(1, 10).Select(i => new Poco { Prop = string.Format("La {0}a la", i) }).ToList(); Poco expected = pocos[1]; // Act var mce = lambda.CreateContainsLambda <Poco>(contains.MemberName, contains.MemberValue); var res = pocos.AsQueryable().Where(mce); var res2 = pocos.Where(p => p.Prop.Contains(contains.MemberValue)).ToList(); // Assert Assert.AreEqual(1, res.Count()); Assert.AreEqual(expected.Prop, res.First().Prop); Assert.AreEqual(res2.First().Prop, res.First().Prop); }
public EntityFilterShould() { _lambdaCreator = new LambdaCreator(); }
public void FilterWithEqualsSuccessfully() { // Arrange const string propertyName = "IntProp"; var equals = new EqualsFilterRules<int>{MemberName = propertyName, MemberValue = 2}; var entities = Enumerable.Range(1, 2).SelectMany(i => Enumerable.Repeat(new LambdaCreatorShould.Poco { IntProp = i }, 10)).ToList(); var lambdaCreator = new LambdaCreator(); IFilter entityFilter = new EntityFilter(lambdaCreator); // Act List<LambdaCreatorShould.Poco> res = entityFilter.FilterWithEquals(entities, @equals); // Assert Assert.AreEqual(10, res.Count); Assert.IsTrue(res.All(elem => elem.IntProp == 2)); }
public void ParseObjectExpressionsSuccessfully() { var rules = new ContainsFilterRules {MemberName = "value", MemberValue = "10"}; var entities = Enumerable.Range(1, 10).Select(i => new {id = i, value = "lala" + i.ToString(CultureInfo.InvariantCulture)}).ToList(); var lamdaCreator = new LambdaCreator(); var filter = new EntityFilter(lamdaCreator); var res = filter.FilterWithContains(entities, rules); Assert.AreEqual(1, res.Count); Assert.AreEqual(10, res[0].id); }
public void SkipElementOfCollectionIfItsQueriedPropertyIsNull() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules { MemberName = "Prop", MemberValue = "la" }; var pocos = Enumerable.Range(1, 10).Select(i => new Poco { Prop = string.Format("La {0}a la", i) }).ToList(); pocos.Add(new Poco()); // Prop = null. // Act var mce = lambda.CreateContainsLambda<Poco>(contains.MemberName, contains.MemberValue); var result = pocos.AsQueryable().Where(mce).ToList(); // Assert Assert.AreEqual(pocos.Count - 1, result.Count); }
public void PerformCaseInsensitiveSearchSuccessfully() { // Arrange var lambda = new LambdaCreator(); var contains = new ContainsFilterRules { MemberName = "Prop", MemberValue = "10A" }; var pocos = Enumerable.Range(1, 10).Select(i => new Poco { Prop = string.Format("La {0}a la", i), IntProp = i}).ToList(); // Act var mce = lambda.CreateContainsLambda<Poco>(contains.MemberName, contains.MemberValue); var result = pocos.AsQueryable().Where(mce).ToList(); // Assert Assert.AreEqual(10, result.Single().IntProp); }
public void CreateWhereEqualsLambdaSuccessfully() { // Arrange const string propertyName = "IntProp"; const int propertyValue = 2; var entities = Enumerable.Range(1, 2).SelectMany(i => Enumerable.Repeat(new Poco{IntProp = i}, 10)).ToList(); var lambdaCreator = new LambdaCreator(); // Act Expression<Func<Poco, bool>> equalsLambda = lambdaCreator.CreateEqualsLambda<Poco>(propertyName, propertyValue); var result = entities.AsQueryable().Where(equalsLambda).ToList(); // Assert Assert.AreEqual(10, result.Count); Assert.IsTrue(result.All(i => i.IntProp == 2)); }