public User FindByEmail(string email)
        {
            AdHocSpecification<User> specification = new AdHocSpecification<User>(x => x.Email == email);
            User user = this.userRepository.FindSingle(specification);

            return user;
        }
        public void SimpleAdHocSpecificationIsSerializable()
        {
            var testSpecification = new AdHocSpecification <string>(n => n == "it works");

            // serialize and deserialize the spec
            var serializedSpecification   = Serialize(testSpecification);
            var deserializedSpecification = Deserialize <Specification <string> >(serializedSpecification);

            Assert.That(deserializedSpecification.IsSatisfiedBy("it works"), Is.True);
            Assert.That(deserializedSpecification.IsSatisfiedBy("it fails"), Is.False);
        }
Exemple #3
0
        public void Equals_returns_true_when_both_sides_are_equals()
        {
            var s1   = new AdHocSpecification <string>(x => x.Length > 1);
            var s2   = new AdHocSpecification <string>(x => x.Length > 2);
            var spec = s1 | s2;

            Assert.IsInstanceOf <OrSpecification <string> >(spec);
            Assert.IsTrue(spec.Equals(spec));
            Assert.IsTrue(spec.Equals(s1 | s2));
            Assert.IsTrue(spec.Equals(s1 || s2)); // | or || both operators behave as ||
        }
Exemple #4
0
        public void Or_operator_should_work()
        {
            var startWithR = new AdHocSpecification <string>(n => n.StartsWith("R"));
            var endsWithN  = new AdHocSpecification <string>(n => n.EndsWith("n"));

            var result = new SampleRepository().Find(startWithR | endsWithN);

            Assert.Contains("Rodrigo", result);
            Assert.Contains("Elton", result);
            Assert.Contains("Alison", result);
        }
Exemple #5
0
        public void Negate_should_work()
        {
            var startWithJ    = new AdHocSpecification <string>(n => n.StartsWith("J"));
            var specification = new NotSpecification <string>(startWithJ);

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

            Assert.DoesNotContain("Jose", result);
            Assert.DoesNotContain("Julian", result);
            Assert.Contains("Manuel", result);
        }
        public void Or_operator_should_work()
        {
            var startWithJ = new AdHocSpecification <string>(n => n.StartsWith("J"));
            var endsWithN  = new AdHocSpecification <string>(n => n.EndsWith("n"));

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

            CollectionAssert.Contains(result, "Jose");
            CollectionAssert.Contains(result, "Julian");
            CollectionAssert.DoesNotContain(result, "Manuel");
        }
Exemple #7
0
        public void Should_be_serializable()
        {
            var sourceSpec = new AdHocSpecification <string>(n => n == "it fails");
            var spec       = new NotSpecification <string>(sourceSpec);

            var deserializedSpec = Helpers.Helpers.SerializeAndDeserialize(spec);

            Assert.IsType <NotSpecification <string> >(deserializedSpec);
            Assert.True(deserializedSpec.ToExpression().Compile().Invoke("it works"));
            Assert.False(deserializedSpec.ToExpression().Compile().Invoke("it fails"));
        }
Exemple #8
0
        public void Combination_of_boolean_operators_should_work()
        {
            var startWithM = new AdHocSpecification <string>(n => n.StartsWith("A"));
            var endsWithN  = new AdHocSpecification <string>(n => n.EndsWith("n"));
            var containsU  = new AdHocSpecification <string>(n => n.Contains("s"));

            var result = new SampleRepository().Find(startWithM | (!endsWithN & !containsU));

            Assert.Contains("Rodrigo", result);
            Assert.DoesNotContain("Elton", result);
            Assert.Contains("Alison", result);
        }
Exemple #9
0
        public void AddQuery(int CategoryKey, string title)
        {
            var specification = new AdHocSpecification <ArchiveEntity>(x => x.Categories.Any(t => t.CategoryKey == CategoryKey));

            AddItem(new SearchWidgetItem()
            {
                Title        = string.Format(@"{0}", title)
                , GroupTitle = "Категории"
                ,
                Specification = specification
            });
        }
Exemple #10
0
        public void GetHashCode_retuns_same_value_for_equal_specifications()
        {
            var s1    = new AdHocSpecification <string>(x => x.Length > 1);
            var s2    = new AdHocSpecification <string>(x => x.Length > 2);
            var s3    = new AdHocSpecification <string>(x => x.Length > 3);
            var spec1 = s1 & s2 & s3;
            var spec2 = s1 & s2 & s3;

            Assert.IsType <AndSpecification <string> >(spec1);
            Assert.IsType <AndSpecification <string> >(spec2);
            Assert.Equal(spec1.GetHashCode(), spec2.GetHashCode());
        }
Exemple #11
0
        public void Or_should_work()
        {
            var startWithE = new AdHocSpecification <string>(n => n.StartsWith("E"));
            var endsWithN  = new AdHocSpecification <string>(n => n.EndsWith("n"));

            var result = new SampleRepository()
                         .Find(new OrSpecification <string>(startWithE, endsWithN));

            Assert.Contains("Elton", result);
            Assert.Contains("Alison", result);
            Assert.DoesNotContain("Rodrigo", result);
        }
        public void Should_be_serializable()
        {
            var sourceSpec1 = new AdHocSpecification <string>(n => n.StartsWith("it"));
            var sourceSpec2 = new AdHocSpecification <string>(n => n.EndsWith("works"));
            var spec        = new OrSpecification <string>(sourceSpec1, sourceSpec2);

            var deserializedSpec = Helpers.Helpers.SerializeAndDeserialize(spec);

            Assert.IsType <OrSpecification <string> >(deserializedSpec);
            Assert.True(deserializedSpec.ToExpression().Compile().Invoke("it works very well"));
            Assert.False(deserializedSpec.ToExpression().Compile().Invoke("fails"));
        }
        public void Should_be_serializable()
        {
            var sourceSpec1 = new AdHocSpecification <string>(n => n.StartsWith("it"));
            var sourceSpec2 = new AdHocSpecification <string>(n => n.EndsWith("works"));
            var spec        = new AndSpecification <string>(sourceSpec1, sourceSpec2);

            var deserializedSpec = Helpers.SerializeAndDeserialize(spec);

            Assert.That(deserializedSpec, Is.InstanceOf <AndSpecification <string> >());
            Assert.That(deserializedSpec.ToExpression().Compile().Invoke("it works"), Is.True);
            Assert.That(deserializedSpec.ToExpression().Compile().Invoke("it fails"), Is.False);
        }
Exemple #14
0
        public void Combination_of_boolean_operators_should_work()
        {
            var startWithM = new AdHocSpecification <string>(n => n.StartsWith("M"));
            var endsWithN  = new AdHocSpecification <string>(n => n.EndsWith("n"));
            var containsU  = new AdHocSpecification <string>(n => n.Contains("u"));

            var result = new SampleRepository().Find(startWithM | (!endsWithN & !containsU));

            CollectionAssert.Contains(result, "Jose");
            CollectionAssert.DoesNotContain(result, "Julian");
            CollectionAssert.Contains(result, "Manuel");
        }
        public void And_should_work()
        {
            var startWithJ   = new AdHocSpecification <string>(n => n.StartsWith("J"));
            var endsWithE    = new AdHocSpecification <string>(n => n.EndsWith("e"));
            var specfication = new AndSpecification <string>(startWithJ, endsWithE);

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

            CollectionAssert.Contains(result, "Jose");
            CollectionAssert.DoesNotContain(result, "Julian");
            CollectionAssert.DoesNotContain(result, "Manuel");
        }
        public void GetHashCode_retuns_same_value_for_equal_specifications()
        {
            var s1    = new AdHocSpecification <string>(x => x.Length > 1);
            var s2    = new AdHocSpecification <string>(x => x.Length > 2);
            var s3    = new AdHocSpecification <string>(x => x.Length > 3);
            var spec1 = s1 | s2 | s3;
            var spec2 = s1 | s2 | s3;

            Assert.IsInstanceOf <OrSpecification <string> >(spec1);
            Assert.IsInstanceOf <OrSpecification <string> >(spec2);
            Assert.AreEqual(spec1.GetHashCode(), spec2.GetHashCode());
        }
Exemple #17
0
        public void Should_be_serializable()
        {
            SerializationSettings.ExpressionSerializerFactory = new FakeSerializerFactory();

            var spec = new AdHocSpecification <string>(n => n == "it works");

            var deserializedSpec = Helpers.SerializeAndDeserialize(spec);

            Assert.That(deserializedSpec, Is.InstanceOf <AdHocSpecification <string> >());
            Assert.That(deserializedSpec.ToExpression().Compile().Invoke("it works"), Is.True);
            Assert.That(deserializedSpec.ToExpression().Compile().Invoke("it fails"), Is.False);
        }
Exemple #18
0
        public void And_should_work()
        {
            var startWithR   = new AdHocSpecification <string>(n => n.StartsWith("R"));
            var endsWithO    = new AdHocSpecification <string>(n => n.EndsWith("o"));
            var specfication = new AndSpecification <string>(startWithR, endsWithO);

            var result = new SampleRepository().Find(specfication).ToArray();

            Assert.All(result, item => Assert.Contains("Rodrigo", item));
            Assert.All(result, item => Assert.DoesNotContain("Elton", item));
            Assert.All(result, item => Assert.DoesNotContain("Alison", item));
        }
        public void Or_should_work()
        {
            var startWithJ = new AdHocSpecification <string>(n => n.StartsWith("J"));
            var endsWithN  = new AdHocSpecification <string>(n => n.EndsWith("n"));

            var result = new SampleRepository()
                         .Find(new OrSpecification <string>(startWithJ, endsWithN));

            Assert.Contains("Jose", result);
            Assert.Contains("Julian", result);
            Assert.DoesNotContain("Manuel", result);
        }
        public void AddQuery(int?minGrade, int?maxGrade)
        {
            var specification = new AdHocSpecification <ArchiveEntity>(x => x.Grade >= minGrade && x.Grade <= maxGrade);

            AddItem(new SearchWidgetItem()
            {
                Title = string.Format(@"{0}-{1}", minGrade, maxGrade)
                ,
                GroupTitle = "Оценка"
                ,
                Specification = specification
            });
        }
Exemple #21
0
        public void Equals_return_false_when_the_negated_spec_are_not_equals()
        {
            var sourceSpec1 = new AdHocSpecification <string>(x => x.Length > 1);
            var sourceSpec2 = new AdHocSpecification <string>(x => x.Length > 2);
            var spec        = !sourceSpec1;

            Assert.IsType <NotSpecification <string> >(spec);
            Assert.False(spec.Equals(null));
            Assert.False(spec.Equals(10));
            Assert.False(spec.Equals(sourceSpec1));
            Assert.False(spec.Equals(sourceSpec2));
            Assert.False(spec.Equals(!sourceSpec2));
        }
Exemple #22
0
        public void AdHocSpecificationTestAdHocSpecification()
        {
            // arrange
            var spec = new AdHocSpecification <Sample>(x => x.FirstName.StartsWith("J"));

            // act
            var result = TestData.List.Where(spec.IsSatisfied()).OrderBy(c => c.FirstName);

            // assert
            Assert.That(result.Count(), Is.EqualTo(2));
            Assert.That(result.First().FirstName, Is.EqualTo("Jose"));
            Assert.That(result.Last().FirstName, Is.EqualTo("Julian"));
        }
Exemple #23
0
        public void CombinedSpecificationIsSerializable()
        {
            var testSpecification1 = new AdHocSpecification <string>(n => n.StartsWith("it works"));
            var testSpecification2 = new AdHocSpecification <string>(n => n.EndsWith("very well"));
            var testSpecification  = new AndSpecification <string>(testSpecification1, testSpecification2);

            // serialize and deserialize the spec
            var serializedSpecification   = Serialize(testSpecification);
            var deserializedSpecification = Deserialize <Specification <string> >(serializedSpecification);

            Assert.That(deserializedSpecification.IsSatisfiedBy("it works very well"), Is.True);
            Assert.That(deserializedSpecification.IsSatisfiedBy("it works very well if you do it right"), Is.False);
        }
        public void AddQuery(string TagTitle)
        {
            var specification = new AdHocSpecification <ArchiveEntity>(x => x.Tags.Any(t => t.TagTitle == TagTitle));

            AddItem(new SearchWidgetItem()
            {
                Title = string.Format(@"{0}", TagTitle)
                ,
                GroupTitle = "Метки"
                ,
                Specification = specification
            });
        }
Exemple #25
0
        public void Equals_returns_false_when_both_sides_are_not_equals()
        {
            var s1   = new AdHocSpecification <string>(x => x.Length > 1);
            var s2   = new AdHocSpecification <string>(x => x.Length > 2);
            var s3   = new AdHocSpecification <string>(x => x.Length > 3);
            var spec = s1 & s2;

            Assert.IsType <AndSpecification <string> >(spec);
            Assert.False(spec.Equals(null));
            Assert.False(spec.Equals(s1));
            Assert.False(spec.Equals(s2));
            Assert.False(spec.Equals(s2 & s1)); // AndAlso is not commutable
            Assert.False(spec.Equals(s1 & s3));
            Assert.False(spec.Equals(s3 & s2));
        }
        public User Logon(string email, string password)
        {
            AdHocSpecification<User> specification = new AdHocSpecification<User>(x => x.Email == email);
            User user = this.userRepository.FindSingle(specification);

            if(user == null)
            {
                return null;
            }

            bool loggedIn = user.Login(this.cryptoService, password);
            this.userRepository.Save(user);

            return loggedIn ? user : null;
        }
Exemple #27
0
        public void AddQuery(int minFilesize, int maxFilesize)
        {
            var spMin         = new AdHocSpecification <ArchiveEntity>(x => x.FileSize >= minFilesize);
            var spMax         = new AdHocSpecification <ArchiveEntity>(x => x.FileSize <= maxFilesize);
            var specification = new AndSpecification <ArchiveEntity>(spMin, spMax);

            AddItem(new SearchWidgetItem()
            {
                Title = string.Format("Размер между {0} и {1}", minFilesize, maxFilesize)
                ,
                GroupTitle = "Размер файла"
                ,
                Specification = specification
            });
        }
        public void Equals_returns_false_when_both_sides_are_not_equals()
        {
            var s1   = new AdHocSpecification <string>(x => x.Length > 1);
            var s2   = new AdHocSpecification <string>(x => x.Length > 2);
            var s3   = new AdHocSpecification <string>(x => x.Length > 3);
            var spec = s1 | s2;

            Assert.IsInstanceOf <OrSpecification <string> >(spec);
            Assert.IsFalse(spec.Equals(10));
            Assert.IsFalse(spec.Equals(s1));
            Assert.IsFalse(spec.Equals(s2));
            Assert.IsFalse(spec.Equals(s2 | s1)); // OrElse is not commutable
            Assert.IsFalse(spec.Equals(s1 | s3));
            Assert.IsFalse(spec.Equals(s3 | s2));
            Assert.IsFalse(spec.Equals(Helpers.NullSpecification));
        }
Exemple #29
0
        public void NavigationPropertySpecification()
        {
            using var db = new SampleDbContext();
            var hasOrdersSpec = new AdHocSpecification <Customer>(x => x.Orders.Any());

            var customers = db.Customers.Where(hasOrdersSpec).Select(x => x.Name).ToList();

            Assert.That(
                customers,
                Is.EquivalentTo(new[]
            {
                "Single-order Customer",
                "Double-order Customer",
                "Triple-order Customer"
            })
                );
        }
        public void NegateSpecificationTestNegateSpecificationViaOperator()
        {
            // arrange
            var specFirstName = new AdHocSpecification <Sample>(x => x.FirstName.StartsWith("J"));
            var spec          = !specFirstName;

            // act
            var resultFirstName = TestData.List.Where(specFirstName.IsSatisfied()).OrderBy(c => c.FirstName);
            var result          = TestData.List.Where(spec.IsSatisfied()).OrderBy(c => c.FirstName);

            // assert
            Assert.That(resultFirstName.Count(), Is.EqualTo(2));
            Assert.That(resultFirstName.First().FirstName, Is.EqualTo("Jose"));
            Assert.That(resultFirstName.Last().FirstName, Is.EqualTo("Julian"));
            Assert.That(result.Count(), Is.EqualTo(1));
            Assert.That(result.First().FirstName, Is.EqualTo("Manuel"));
            Assert.That(result.First().LastName, Is.EqualTo("Rivera"));
        }
        public void AddQuery(string searchString)
        {
            //   var str1 = new AdHocSpecification<ArchiveEntity>(x => x.Title.Contains(searchString));
            //   var str2 = new AdHocSpecification<ArchiveEntity>(x => x.Description.Contains(searchString));
            var str1 = new AdHocSpecification <ArchiveEntity>(x => x.Title.Contains("searchString"));
            var str2 = new AdHocSpecification <ArchiveEntity>(x => x.Description.Contains("searchString"));



            //var specification =new OrSpecification<ArchiveEntity>(str1, str2);

            var specification = new AdHocSpecification <ArchiveEntity>(x => x.Title.Contains(searchString));

            AddItem(new SearchWidgetItem()
            {
                Title = string.Format(@"'{0}'", searchString)
                ,
                GroupTitle = "Содержание"
                ,
                Specification = specification
            });
        }