public void TestEvaluateNullPropertyExpression()
		{
			Person person = new Person() { Name = null };
			ICriterion criterion = ExpressionProcessor.ProcessExpression<Person>(p => p.Name == person.Name);
			SimpleExpression simpleExpression = (SimpleExpression)criterion;
			Assert.AreEqual(null, simpleExpression.Value);
		}
		public void FilterNullComponent()
		{
			using (ISession s = OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				var p1 = new Person() { Detail = new PersonDetail() { MaidenName = "test", Anniversary = new DateTime(2007, 06, 05) } };
				var p2 = new Person() { Detail = null };

				s.Save(p1);
				s.Save(p2);

				var nullDetails =
					s.QueryOver<Person>()
						.Where(p => p.Detail == null)
						.List();

				Assert.That(nullDetails.Count, Is.EqualTo(1));
				Assert.That(nullDetails[0].Id, Is.EqualTo(p2.Id));
			}
		}
예제 #3
0
		public void StatelessSession()
		{
			using (var ss = sessions.OpenStatelessSession())
			{
				using (var tx = ss.BeginTransaction())
				{
					var person = new Person() { Name = "test1" };
					ss.Insert(person);

					var statelessPerson1 =
						ss.QueryOver<Person>()
							.List()
							[0];

					Assert.That(statelessPerson1.Id, Is.EqualTo(person.Id));

					var statelessPerson2 =
						QueryOver.Of<Person>()
							.GetExecutableQueryOver(ss)
							.List()
							[0];

					Assert.That(statelessPerson2.Id, Is.EqualTo(person.Id));
				}
			}
		}
예제 #4
0
		public void IsType()
		{
			using (ISession s = OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				var father1 = new Person() { Name = "Father 1" };
				var father2 = new CustomPerson() { Name = "Father 2" };

				var person1 = new Person() { Name = "Person 1", Father = father2 };
				var person2 = new CustomPerson() { Name = "Person 2", Father = father1 };

				s.Save(father1);
				s.Save(father2);

				s.Save(person1);
				s.Save(person2);

				t.Commit();
			}

			using (ISession s = OpenSession())
			{
				var actual =
					s.QueryOver<Person>()
						.Where(p => p is CustomPerson)
						.And(p => p.Father != null)
						.List();

				Assert.That(actual.Count, Is.EqualTo(1));
				Assert.That(actual[0].Name, Is.EqualTo("Person 2"));
			}

			using (ISession s = OpenSession())
			{
				var actual =
					s.QueryOver<Person>()
						.Where(p => p.GetType() == typeof(CustomPerson))
						.And(p => p.Father != null)
						.List();

				Assert.That(actual.Count, Is.EqualTo(1));
				Assert.That(actual[0].Name, Is.EqualTo("Person 2"));
			}

			using (ISession s = OpenSession())
			{
				Person f = null;
				var actual =
					s.QueryOver<Person>()
						.JoinAlias(p => p.Father, () => f)
						.Where(() => f is CustomPerson)
						.List();

				Assert.That(actual.Count, Is.EqualTo(1));
				Assert.That(actual[0].Name, Is.EqualTo("Person 1"));
			}

			using (ISession s = OpenSession())
			{
				Person f = null;
				var actual =
					s.QueryOver<Person>()
						.JoinAlias(p => p.Father, () => f)
						.Where(() => f.GetType() == typeof(CustomPerson))
						.List();

				Assert.That(actual.Count, Is.EqualTo(1));
				Assert.That(actual[0].Name, Is.EqualTo("Person 1"));
			}
		}
		public void NullRestriction()
		{
			ICriteria expected =
				CreateTestCriteria(typeof(Person), "personAlias")
					.Add(Restrictions.IsNull("Name"))
					.Add(Restrictions.IsNull("Name"))
					.Add(Restrictions.IsNull("Name"))
					.Add(Restrictions.IsNull("Father"))
					.Add(Restrictions.IsNull("Father"))
					.Add(Restrictions.IsNull("NullableGender"))
					.Add(Restrictions.IsNull("NullableAge"))
					.Add(Restrictions.IsNull("NullableIsParent"))
					.Add(Restrictions.Not(Restrictions.IsNull("Name")))
					.Add(Restrictions.Not(Restrictions.IsNull("Name")))
					.Add(Restrictions.Not(Restrictions.IsNull("Name")))
					.Add(Restrictions.Not(Restrictions.IsNull("Father")))
					.Add(Restrictions.Not(Restrictions.IsNull("Father")))
					.Add(Restrictions.Not(Restrictions.IsNull("NullableGender")))
					.Add(Restrictions.Not(Restrictions.IsNull("NullableAge")))
					.Add(Restrictions.Not(Restrictions.IsNull("NullableIsParent")))
					.Add(Restrictions.IsNull("personAlias.Name"));

			Person personAlias = null;
			CustomPerson nullPerson = null;
			Person.StaticName = null;
			Person emptyPerson = new Person() { Name = null };
			var actual =
				CreateTestQueryOver<Person>(() => personAlias)
					.Where(p => p.Name == null)
					.Where(p => p.Name == Person.StaticName)
					.Where(p => p.Name == emptyPerson.Name)
					.Where(p => p.Father == null)
					.Where(p => p.Father == nullPerson)
					.Where(p => p.NullableGender == null)
					.Where(p => p.NullableAge == null)
					.Where(p => p.NullableIsParent == null)
					.Where(p => p.Name != null)
					.Where(p => p.Name != Person.StaticName)
					.Where(p => p.Name != emptyPerson.Name)
					.Where(p => p.Father != null)
					.Where(p => p.Father != nullPerson)
					.Where(p => p.NullableGender != null)
					.Where(p => p.NullableAge != null)
					.Where(p => p.NullableIsParent != null)
					.Where(() => personAlias.Name == null);

			AssertCriteriaAreEqual(expected, actual);
		}
		public void TestEvaluateMemberExpression()
		{
			Person testPerson = new Person();
			testPerson.Name = "testName";
			ICriterion criterion = ExpressionProcessor.ProcessExpression<Person>(p => p.Name == testPerson.Name);
			SimpleExpression simpleExpression = (SimpleExpression)criterion;
			Assert.AreEqual("testName", simpleExpression.Value);
		}
        public async Task IsTypeAsync()
        {
            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    var father1 = new Person()
                    {
                        Name = "Father 1"
                    };
                    var father2 = new CustomPerson()
                    {
                        Name = "Father 2"
                    };

                    var person1 = new Person()
                    {
                        Name = "Person 1", Father = father2
                    };
                    var person2 = new CustomPerson()
                    {
                        Name = "Person 2", Father = father1
                    };

                    await(s.SaveAsync(father1));
                    await(s.SaveAsync(father2));

                    await(s.SaveAsync(person1));
                    await(s.SaveAsync(person2));

                    await(t.CommitAsync());
                }

            using (ISession s = OpenSession())
            {
                var actual =
                    await(s.QueryOver <Person>()
                          .Where(p => p is CustomPerson)
                          .And(p => p.Father != null)
                          .ListAsync());

                Assert.That(actual.Count, Is.EqualTo(1));
                Assert.That(actual[0].Name, Is.EqualTo("Person 2"));
            }

            using (ISession s = OpenSession())
            {
                var actual =
                    await(s.QueryOver <Person>()
                          .Where(p => p.GetType() == typeof(CustomPerson))
                          .And(p => p.Father != null)
                          .ListAsync());

                Assert.That(actual.Count, Is.EqualTo(1));
                Assert.That(actual[0].Name, Is.EqualTo("Person 2"));
            }

            using (ISession s = OpenSession())
            {
                Person f      = null;
                var    actual =
                    await(s.QueryOver <Person>()
                          .JoinAlias(p => p.Father, () => f)
                          .Where(() => f is CustomPerson)
                          .ListAsync());

                Assert.That(actual.Count, Is.EqualTo(1));
                Assert.That(actual[0].Name, Is.EqualTo("Person 1"));
            }

            using (ISession s = OpenSession())
            {
                Person f      = null;
                var    actual =
                    await(s.QueryOver <Person>()
                          .JoinAlias(p => p.Father, () => f)
                          .Where(() => f.GetType() == typeof(CustomPerson))
                          .ListAsync());

                Assert.That(actual.Count, Is.EqualTo(1));
                Assert.That(actual[0].Name, Is.EqualTo("Person 1"));
            }
        }
        public async Task OnClauseAsync()
        {
            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    await(s.SaveAsync(new Person()
                    {
                        Name = "John"
                    }
                                      .AddChild(new Child()
                    {
                        Nickname = "John"
                    })
                                      .AddChild(new Child()
                    {
                        Nickname = "Judy"
                    })));

                    await(s.SaveAsync(new Person()
                    {
                        Name = "Jean"
                    }));
                    await(s.SaveAsync(new Child()
                    {
                        Nickname = "James"
                    }));

                    await(t.CommitAsync());
                }

            using (ISession s = OpenSession())
            {
                Child  childAlias  = null;
                Person parentAlias = null;
                var    children    =
                    await(s.QueryOver(() => childAlias)
                          .Left.JoinQueryOver(c => c.Parent, () => parentAlias, p => p.Name == childAlias.Nickname)
                          .WhereRestrictionOn(p => p.Name).IsNotNull
                          .ListAsync());

                Assert.That(children, Has.Count.EqualTo(1));
            }

            using (ISession s = OpenSession())
            {
                Child  childAlias  = null;
                Person parentAlias = null;
                var    parentNames = await(s.QueryOver(() => childAlias)
                                           .Left.JoinAlias(c => c.Parent, () => parentAlias, p => p.Name == childAlias.Nickname)
                                           .Select(c => parentAlias.Name)
                                           .ListAsync <string>());

                Assert.That(parentNames.Count(n => !string.IsNullOrEmpty(n)), Is.EqualTo(1));
            }

            using (ISession s = OpenSession())
            {
                Person personAlias = null;
                Child  childAlias  = null;
                var    people      =
                    await(s.QueryOver <Person>(() => personAlias)
                          .Left.JoinQueryOver(p => p.Children, () => childAlias, c => c.Nickname == personAlias.Name)
                          .WhereRestrictionOn(c => c.Nickname).IsNotNull
                          .ListAsync());

                Assert.That(people, Has.Count.EqualTo(1));
            }

            using (ISession s = OpenSession())
            {
                Person personAlias = null;
                Child  childAlias  = null;
                var    childNames  =
                    await(s.QueryOver <Person>(() => personAlias)
                          .Left.JoinAlias(p => p.Children, () => childAlias, c => c.Nickname == personAlias.Name)
                          .Select(p => childAlias.Nickname)
                          .ListAsync <string>());

                Assert.That(childNames.Count(n => !string.IsNullOrEmpty(n)), Is.EqualTo(1));
            }
        }
		public void TestEvaluateNullPropertyExpression()
		{
			Person person = new Person() { Name = null };
			ICriterion criterion = ExpressionProcessor.ProcessExpression<Person>(p => p.Name == person.Name);
			Assert.That(criterion, Is.InstanceOf<NullExpression>());
		}