// Methods public Item QueryItemById(int id) { var itemIdSpec = new ExpressionSpecification<Item>(i => i.Id == id); var items = _itemRepository .Select(itemIdSpec, _includedProperties) .SingleOrDefault(); return items; }
public void OrTestCase4() { var left = new ExpressionSpecification<String>( x => true ); var target = left.Or( x => true ); var actual = target.IsSatisfiedByWithMessages( String.Empty ); Assert.AreEqual( 0, actual.Count() ); }
public void OrTestCase() { var left = new ExpressionSpecification<String>( x => true ); var target = left.Or( x => true ); var actual = target.IsSatisfiedBy( String.Empty ); Assert.IsTrue( actual ); }
public void ExpressionReturnsTrueWhenSpecifactionIsFalse(TestObject testObject, bool result) { var specification1 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty); var notSpecification = new NotSpecification <TestObject>(specification1); ExpressionSpecificationTester.TestSpecification(notSpecification, testObject, result); }
public void XOrTestCase3() { var left = new ExpressionSpecification<String>( x => false ); var target = left.XOr( x => false ); var actual = target.IsSatisfiedBy( String.Empty ); Assert.IsFalse( actual ); }
public void IsSatisfiedByReturnsTrueWhenSpecifactionIsFalse(TestObject testObject, bool result) { var specification1 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty); var notSpecification = new NotSpecification <TestObject>(specification1); notSpecification.IsSatisfiedBy(testObject).ShouldBe(result); }
public void ExpressionReturnsTrueWhenAllSpecifactionsAreTrue(TestObject testObject, bool result) { var specification1 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty); var specification2 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty1); var andSpecification = new AndSpecification <TestObject>(specification1, specification2); ExpressionSpecificationTester.TestSpecification(andSpecification, testObject, result); }
public void AndTest4() { var left = new ExpressionSpecification <String>(x => true); var target = left.And(x => true); var actual = target.IsSatisfiedByWithMessages(String.Empty); Assert.Equal(0, actual.Count()); }
public void IsSatisfiedByWithMessagesTest() { var target = new ExpressionSpecification <String>(x => x.Length > 5); var actual = target.IsSatisfiedByWithMessages("123") .ToList(); Assert.Equal(1, actual.Count); Assert.Null(actual[0]); }
public List <CPFormField> GetFormFieldCol(int formId) { ISpecification <CPFormField> specification; specification = new ExpressionSpecification <CPFormField>(t => t.FormId.Equals(formId)); IList <CPFormField> col = this._CPFormFieldRep.GetByCondition(specification); return(col.ToList()); }
public List <CODepUserRelate> GetDepUserRelate(int depId) { ISpecification <CODepUserRelate> specification; specification = new ExpressionSpecification <CODepUserRelate>(t => t.DepId.Equals(depId)); IList <CODepUserRelate> col = this._CODepUserRelateRep.GetByCondition(specification); return(col.ToList()); }
// Methods public Item QueryItemById(int id) { var itemIdSpec = new ExpressionSpecification <Item>(i => i.Id == id); var items = _itemRepository .Select(itemIdSpec, _includedProperties) .SingleOrDefault(); return(items); }
public void Implicit_To_Func() { var lessThan5 = new ExpressionSpecification <int>(i => i < 5); Func <int, bool> f = lessThan5.Function; f = lessThan5; Assert.That(f(1), Is.True); Assert.That(f(6), Is.False); }
public void XOrTestCase10() { var left = new ExpressionSpecification<String>( x => false, "msgLeft" ); var target = left.XOr( x => true, "msgRight" ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 0, actual.Count() ); }
public void AndTestCase1() { var left = new ExpressionSpecification<String>( x => true ); var target = left.And( x => false ); var actual = target.IsSatisfiedBy( String.Empty ); actual.Should() .BeFalse(); }
public void OrTestNullCheck1() { var left = new ExpressionSpecification <String>(x => true); Func <String, Boolean> expression = null; // ReSharper disable once AssignNullToNotNullAttribute Action test = () => left.Or(expression); test.ShouldThrow <ArgumentNullException>(); }
public List <CPPortalModule> GetModulesByParent(int parentModuleId = -1, int sysId = 1) { ISpecification <CPPortalModule> specification; specification = new ExpressionSpecification <CPPortalModule>(t => t.ParentId.Equals(parentModuleId) && t.IsShow == true && t.SysId == sysId); IList <CPPortalModule> col = this._CPPortalModuleRep.GetByCondition(specification); return(col.OrderBy(t => t.ShowOrder).ToList()); }
public void Implicit_To_Expression() { var lessThan5 = new ExpressionSpecification <int>(i => i < 5); Expression <Func <int, bool> > e = lessThan5.Expression; e = lessThan5; Assert.That(e.Compile().Invoke(1), Is.True); Assert.That(e.Compile().Invoke(6), Is.False); }
public void SatisfiesWithMessagesTestCase() { var specification = new ExpressionSpecification<String>( x => x.Length > 3 ) .And( x => x.StartsWith( "1" ) ); var actual = "1234".SatisfiesWithMessages( specification ) .ToList(); Assert.AreEqual( 0, actual.Count ); }
public void InvokeNull_NotRaiseException() { Expression <Func <object, bool> > expression = candidate => true; var sut = new ExpressionSpecification <object>(expression); var exception = Record.Exception(() => sut.GetExpression().Compile().Invoke(null)); Assert.Null(exception); }
public void MoreThan10_Or_LessThan5() { ISpecification <int> moreThan10 = ExpressionSpecification.For <int>(i => i > 10); ISpecification <int> lessThan5 = new ExpressionSpecification <int>(i => i < 5); ISpecification <int> subject = lessThan5.Or(moreThan10); Assert.That(subject, Must.Not.Be.SatisfiedBy(7)); Assert.That(subject, Must.Be.SatisfiedBy(3).And(13)); }
public List <COUser> GetAllUser() { ISpecification <COUser> specification; specification = new ExpressionSpecification <COUser>(t => t.IsCanLogin.Value.Equals(true) && t.IsOutUser.Value.Equals(false)); IList <COUser> col = this._COUserRep.GetByCondition(specification); return(col.ToList()); }
public void MoreThan5_AndOp_LessThan10() { ExpressionSpecification <int> lessThan10 = ExpressionSpecification.For <int>(i => i < 10); var moreThan5 = new ExpressionSpecification <int>(i => i > 5); ExpressionSpecification <int> subject = lessThan10 && moreThan5; Assert.That(subject, Must.Be.SatisfiedBy(7)); Assert.That(subject, Must.Not.Be.SatisfiedBy(3).Or(13)); }
public List <CPFormChildTable> GetFormChildTable(int formId) { ISpecification <CPFormChildTable> specification; specification = new ExpressionSpecification <CPFormChildTable>(t => t.FormId.Equals(formId)); IList <CPFormChildTable> col = this._CPFormChildTableRep.GetByCondition(specification); return(col.ToList()); }
public void MoreThan10_OrOp_LessThan5() { ExpressionSpecification <int> moreThan10 = ExpressionSpecification.For <int>(i => i > 10); var lessThan5 = new ExpressionSpecification <int>(i => i < 5); ExpressionSpecification <int> subject = lessThan5 || moreThan10; Assert.That(subject, Must.Not.Be.SatisfiedBy(7)); Assert.That(subject, Must.Be.SatisfiedBy(3).Or(13)); }
public void OrTest4() { var left = new ExpressionSpecification <String>(x => true); var target = left.Or(x => true); var actual = target.IsSatisfiedByWithMessages(String.Empty); Assert.Empty(actual); }
public void AndTestCase3() { var target = new ExpressionSpecification<String>( x => false ); var other = new ExpressionSpecification<String>( x => false ); var actual = target.And( other ); var result = actual.IsSatisfiedBy( String.Empty ); Assert.IsFalse( result ); }
public void AndTestCaseNullCheck() { var target = new ExpressionSpecification<String>( x => false ); ExpressionSpecification<String> other = null; Action test = () => target.And( other ); test.ShouldThrow<ArgumentNullException>(); }
public void IsSatisfiedByReturnsTrueWhenAnySpecifactionIsTrue(TestObject testObject, bool expectedResult) { var specification1 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty); var specification2 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty1); var orSpecification = new OrSpecification <TestObject>(specification1, specification2); orSpecification.IsSatisfiedBy(testObject).ShouldBe(expectedResult); }
public void NullCandidate_NoException() { Expression <Func <object, bool> > expression = candidate => true; var sut = new ExpressionSpecification <object>(expression); var exception = Record.Exception(() => sut.IsSatisfiedBy(null, out _)); Assert.Null(exception); }
public void ExpressionReturnsTrueWhenAnySpecifactionIsTrue(TestObject testObject, bool expectedResult) { var specification1 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty); var specification2 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty1); var orSpecification = new OrSpecification <TestObject>(specification1, specification2); ExpressionSpecificationTester.TestSpecification(orSpecification, testObject, expectedResult); }
public void IsSatisfiedByReturnsTrueWhenAllSpecifactionsAreTrue(TestObject testObject, bool result) { var specification1 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty); var specification2 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty1); var andSpecification = new AndSpecification <TestObject>(specification1, specification2); andSpecification.IsSatisfiedBy(testObject).ShouldBe(result); }
public void InvokeTrueExpression_ReturnTrue() { Expression <Func <object, bool> > expression = candidate => true; var sut = new ExpressionSpecification <object>(expression); var result = sut.GetExpression().Compile().Invoke(new object()); Assert.True(result); }
public void OrTest3() { var left = new ExpressionSpecification <String>(x => false); var target = left.Or(x => false); var actual = target.IsSatisfiedBy(String.Empty); Assert.False(actual); }
public void OrTest8() { var left = new ExpressionSpecification <String>(x => true, "msgLeft"); var target = left.Or(x => true, "msgRight"); var actual = target.IsSatisfiedByWithMessages(String.Empty); Assert.Equal(0, actual.Count()); }
public void AndTestNullCheck1() { var left = new ExpressionSpecification <String>(x => true); Func <String, Boolean> expression = null; // ReSharper disable once AssignNullToNotNullAttribute Action test = () => left.And(expression); Assert.Throws <ArgumentNullException>(test); }
public async Task <int> CountPages([FromQuery] CarQuery query) { ISpecification <Car> numberPlateFilter = new ExpressionSpecification <Car> (e => EF.Functions.Like(e.NumberPlate, $"%{query.Search.Trim()}%")); ISpecification <Car> carTypeFilter = new ExpressionSpecification <Car> (e => query.CarTypeId == 0 ? true : e.CarTypeId == query.CarTypeId); ISpecification <Car> carExpSpec = numberPlateFilter.And(carTypeFilter); return(await carRepository.CountPages(query.PageSize, carExpSpec)); }
public void AndTest8() { var left = new ExpressionSpecification <String>(x => true, "msgLeft"); var target = left.And(x => true, "msgRight"); var actual = target.IsSatisfiedByWithMessages(String.Empty); Assert.Empty(actual); }
public void TrueExpression_ReturnTrue() { Expression <Func <object, bool> > expression = candidate => true; var sut = new ExpressionSpecification <object>(expression); var result = sut.IsSatisfiedBy(new object()); Assert.True(result); }
public void XOrTestCase4() { var left = new ExpressionSpecification<String>( x => true ); var target = left.XOr( x => true ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 1, actual.Count ); Assert.AreEqual( "The given object matches both specifications.", actual[0] ); }
public void AndTestCase3() { var left = new ExpressionSpecification<String>( x => false ); var right = new ExpressionSpecification<String>( x => true ); var target = new AndSpecification<String>( left, right ); var actual = target.And( new ExpressionSpecification<String>( x => false ) ); var result = actual.IsSatisfiedBy( String.Empty ); Assert.IsFalse( result ); }
public void SatisfiesWithMessagesTestCase2() { var specification = new ExpressionSpecification<String>( x => x.Length > 3, "msg1" ) .And( x => x.StartsWith( "1" ), "msg2" ); var actual = "234".SatisfiesWithMessages( specification ) .ToList(); Assert.AreEqual( 2, actual.Count ); Assert.AreEqual( 1, actual.Count( x => x == "msg1" ) ); Assert.AreEqual( 1, actual.Count( x => x == "msg2" ) ); }
public void WhereNotTestCase() { var target = new List<Int32> { 1, 10, 100, 1000 }; var specification = new ExpressionSpecification<Int32>( x => x >= 100 ); var actual = target.WhereNot( specification ) .ToList(); Assert.AreEqual( 2, actual.Count ); Assert.AreEqual( 1, actual.Count( x => x == 1 ) ); Assert.AreEqual( 1, actual.Count( x => x == 10 ) ); }
public void Test_And_Specification() { ISpecification<int> rule1 = new ExpressionSpecification<int>(x => x == 1, "rule1 failed"); ISpecification<int> rule2 = new ExpressionSpecification<int>(x => x == 2, "rule2 failed"); ISpecification<int> rule3 = new ExpressionSpecification<int>(x => x == 3, "rule3 failed"); ISpecification<int> rule4 = rule1.Or(rule2).Or(rule3); var result = rule4.ValidateWithMessages(4); Assert.IsTrue(result.Count > 0); }
public void AndTestCase11() { var left = new ExpressionSpecification<String>( x => false, "msgLeft" ); var target = left.And( x => false, "msgRight" ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); actual.Should() .HaveCount( 2 ); actual.Should() .Contain( "msgLeft", "msgRight" ); }
public static void Main() { var someBookstore = new Bookstore("Shakespeare and Company"); var witchesAbroad = new Book("Witches abroad", Genre.Fantasy, 301); someBookstore.Add(witchesAbroad); var hatFullOfSky = new Book("Hat full of sky", Genre.Fantasy, 310); someBookstore.Add(hatFullOfSky); var gameOfThrones = new Book("Game of thrones", Genre.Fantasy, 900); someBookstore.Add(gameOfThrones); var deathOnTheNile = new Book("Death on the Nile", Genre.Crime, 400); someBookstore.Add(deathOnTheNile); var historyOfTheBalkans = new Book("History of the Balkans", Genre.History, 1120); someBookstore.Add(historyOfTheBalkans); Console.WriteLine(someBookstore); var ruleOnlyFantasy = new ExpressionSpecification<Book>(b => b.Genre == Genre.Fantasy); var ruleLargeBooks = new ExpressionSpecification<Book>(b => b.Pages > 1000); var rulePagesBelow500 = new ExpressionSpecification<Book>(b => b.Pages < 500); var ruleFantasyOrLargeBooks = ruleOnlyFantasy.Or(ruleLargeBooks); var allBooks = someBookstore.All(); var largeBooks = allBooks.FindAll(b => ruleLargeBooks.IsSatisfiedBy(b)); Console.WriteLine("Large books:"); foreach (var book in largeBooks) { Console.WriteLine(book); } Console.WriteLine(); var fantasyAndLargeBooks = allBooks.FindAll(b => ruleFantasyOrLargeBooks.IsSatisfiedBy(b)); Console.WriteLine("Fantasy and large books:"); foreach (var book in fantasyAndLargeBooks) { Console.WriteLine(book); } Console.WriteLine(); }
public void Main(string[] args) { var ehSolteiro = new ExpressionSpecification<Pessoa>(p => p.EstadoCivil == EstadoCivil.Solteiro); var ehHomem = new ExpressionSpecification<Pessoa>(p => p.Sexo == Sexo.Masculino); var ehHomemSolteiro = ehSolteiro & ehHomem; var ouHomemOuSolteiro = ehHomem ^ ehSolteiro; var log = new Microsoft.Framework.Logging.LoggerFactory(); log.MinimumLevel = Microsoft.Framework.Logging.LogLevel.Verbose; using (var db = new PessoaDbContext()) { db.Pessoas.Where(ouHomemOuSolteiro) .Select(x => x).ToList().ForEach(x => Console.WriteLine(x)); } Console.WriteLine("Finished OK."); Console.ReadLine(); }
public static void Main(string[] args) { var mobiles = new List<Mobile>() { new Mobile(MobileBrand.Apple, MobileType.Smart), new Mobile(MobileBrand.Samsung, MobileType.Smart), new Mobile(MobileBrand.Samsung, MobileType.Basic) }; var smartSpecification = new ExpressionSpecification<Mobile>(mobile => mobile.Type == MobileType.Smart); var appleSpecification = new ExpressionSpecification<Mobile>(mobile => mobile.Brand == MobileBrand.Apple); // find all smart mobiles; var smartMobiles = mobiles.FindAll(mobile => smartSpecification.IsSatisfiedBy(mobile)); // find all apple smart mobiles; var andSpecification = new AndSpecification<Mobile>(smartSpecification, appleSpecification); var appleSmartMobiles = mobiles.FindAll(mobile => andSpecification.IsSatisfiedBy(mobile)); Console.WriteLine("Hello World!"); Console.ReadKey(); }
public string Login(string username, string password) { if (string.IsNullOrEmpty(username)) { return ""; } var filter = new ExpressionSpecification<Employee>( (e) => e.UserName == username); var employees = repository.Find(filter); if (employees == null || employees.Count() == 0) { throw new ApplicationException("Invalid user name or password."); } var employee = employees.First(); if (employee.Password != password) { throw new ApplicationException("Invalid username or password."); } return "Login successful!"; }
public void IsSatisfiedByTestCase() { var left = new ExpressionSpecification<String>( x => true ); var right = new ExpressionSpecification<String>( x => true ); var target = new AndSpecification<String>( left, right ); var actual = target.IsSatisfiedBy( String.Empty ); Assert.IsTrue( actual ); }
public void XOrTestCaseNullCheck1() { var left = new ExpressionSpecification<String>( x => true ); Func<String, Boolean> expression = null; Action test = () => left.XOr( expression ); test.ShouldThrow<ArgumentNullException>(); }
public void XOrTestCase7() { var left = new ExpressionSpecification<String>( x => false ); var target = left.XOr( x => false ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 2, actual.Count() ); Assert.IsNull( actual[0] ); Assert.IsNull( actual[1] ); }
public IEnumerable<Item> QueryItems() { var allSpec = new ExpressionSpecification<Item>(); var items = _itemRepository.Select(allSpec, _includedProperties); return items; }
public void XOrTestCase() { var left = new ExpressionSpecification<String>( x => true ); var right = new ExpressionSpecification<String>( x => true ); var target = new XOrSpecification<String>( left, right ); var actual = target.XOr( new ExpressionSpecification<String>( x => true ) ); var result = actual.IsSatisfiedBy( String.Empty ); Assert.IsTrue( result ); }
// Methods static List<Item> GetItems() { using (var uow = new TpcUnitOfWork(Context)) { // Insert item... var itemRepository = uow.GetRepository<Item>(); var spec = new ExpressionSpecification<Item>() .Not(i => (i.Header.Title.Contains("H"))) .Or(i => i.Amount >= 300); var include = new PropertyNavigator<Item>() .Include(i => i.Header) .Include(i => i.Entry) .Properties; return itemRepository .Select(spec, include) .ToList(); } }
public void IsSatisfiedByWithMessagesTestCas3() { var left = new ExpressionSpecification<String>( x => false ); var right = new ExpressionSpecification<String>( x => false ); var target = new AndSpecification<String>( left, right ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 2, actual.Count ); Assert.IsNull( actual[0] ); Assert.IsNull( actual[1] ); }
public void IsSatisfiedByWithMessagesTestCase4() { var left = new ExpressionSpecification<String>( x => true, "msgLeft" ); var right = new ExpressionSpecification<String>( x => true, "msgRight" ); var target = new AndSpecification<String>( left, right ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 0, actual.Count ); }
public void XOrTestCaseNullCheck() { var left = new ExpressionSpecification<String>( x => false ); var right = new ExpressionSpecification<String>( x => true ); var target = new AndSpecification<String>( left, right ); ExpressionSpecification<String> other = null; Action test = () => target.XOr( other ); test.ShouldThrow<ArgumentNullException>(); }
public void IsSatisfiedByWithMessagesTestCase7() { var left = new ExpressionSpecification<String>( x => false, "msgLeft" ); var right = new ExpressionSpecification<String>( x => false, "msgRight" ); var target = new XOrSpecification<String>( left, right ); var actaul = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 2, actaul.Count ); Assert.AreEqual( 1, actaul.Count( x => x == "msgLeft" ) ); Assert.AreEqual( 1, actaul.Count( x => x == "msgRight" ) ); }
public IEnumerable<Item> QueryItemsByUserId(int userId) { var itemUserIdSpec = new ExpressionSpecification<Item>(i => i.UserId == userId); var items = _itemRepository.Select(itemUserIdSpec, _includedProperties); return items; }