using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Transform;
using NHibernate.Criterion;
namespace NHibernate.Test.NHSpecificTest.NHXXXX
{
public abstract class BagDeepQueryTest : BugTestCase
{
Blog expected;
protected override System.Collections.IList Mappings
{
get
{
return new string[] { "NHSpecificTest.NHXXXX.MapSetOfSets.hbm.xml" };
}
}
///
/// Create blog with 4 posts
/// 1st post have 0 comments
/// 2nd post have 1 comment
/// 3rd post have 2 comments
/// 4th post have 3 comments
///
protected override void OnSetUp()
{
base.OnSetUp();
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var blog = new Blog()
{
Blogger = "Blogger",
Name = "Blog #1",
Posts = new List()
};
for (int i = 1; i <= 4; i++)
{
var post = new Post()
{
Blog = blog,
CtreationDate = DateTime.Now.Subtract(TimeSpan.FromHours(1)).AddMinutes(i*10),
Text = "bla bla bla",
Comments = new List()
};
blog.Posts.Add(post);
for (int j = 1; j < i; j++)
{
var comment = new Comment()
{
Post = post,
CtreationDate = post.CtreationDate.AddMinutes(j),
Blogger = "Blogger #" + j,
Text = "bla bla bla " + j
};
post.Comments.Add(comment);
}
}
session.SaveOrUpdate(blog);
tx.Commit();
expected = blog;
}
}
protected override void OnTearDown()
{
base.OnTearDown();
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.CreateSQLQuery("delete from Comments").ExecuteUpdate();
session.CreateSQLQuery("delete from Posts").ExecuteUpdate();
session.CreateSQLQuery("delete from Blogs").ExecuteUpdate();
tx.Commit();
}
}
[Test]
public void QueryWithLinq()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
Blog actual = session.Query()
.Where(b => b.Id == expected.Id)
.FetchMany(b => b.Posts)
.ThenFetch(p => p.Comments)
.ToList().First();
Validate(expected, actual);
}
}
[Test]
public void QueryWithQueryOver()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
Post postAlias = null;
Blog actual = session.QueryOver()
.Where(b => b.Id == expected.Id)
.JoinQueryOver(b => b.Posts, () => postAlias, NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.JoinQueryOver(() => postAlias.Comments, NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.TransformUsing(Transformers.DistinctRootEntity)
.SingleOrDefault();
Validate(expected, actual);
}
}
[Test]
public void QueryWithCriteria()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var actual = session.CreateCriteria()
.Add(Restrictions.IdEq(expected.Id))
.SetFetchMode("Posts", FetchMode.Eager)
.SetFetchMode("Posts.Comments", FetchMode.Eager)
.SetResultTransformer(Transformers.DistinctRootEntity)
.UniqueResult();
Validate(expected, actual);
}
}
protected virtual void Validate(Blog expectedBlog, Blog actualBlog)
{
Assert.AreEqual(expectedBlog.Posts.Count, actualBlog.Posts.Count);
for (int i = 0; i < expectedBlog.Posts.Count; i++)
{
var expectedPost = expectedBlog.Posts.ElementAt(i);
var post = actualBlog.Posts.ElementAt(i);
Assert.AreEqual(expectedPost.Id, post.Id);
Assert.AreEqual(expectedPost.Comments.Count, post.Comments.Count);
for (int j = 0; j < expectedPost.Comments.Count; j++)
{
var expectedComment = expectedPost.Comments.ElementAt(j);
var comment = post.Comments.ElementAt(j);
Assert.AreEqual(expectedComment.Id, comment.Id);
}
}
}
}
[TestFixture]
public class BagOfBagsDeepQueryTest : BagDeepQueryTest
{
protected override System.Collections.IList Mappings
{
get
{
return new string[] { "NHSpecificTest.NHXXXX.MapBagOfBags.hbm.xml" };
}
}
}
[TestFixture]
public class BagOfSetsDeepQueryTest : BagDeepQueryTest
{
protected override System.Collections.IList Mappings
{
get
{
return new string[] { "NHSpecificTest.NHXXXX.MapBagOfSets.hbm.xml" };
}
}
}
[TestFixture]
public class SetOfBagsDeepQueryTest : BagDeepQueryTest
{
protected override System.Collections.IList Mappings
{
get
{
return new string[] { "NHSpecificTest.NHXXXX.MapSetOfBags.hbm.xml" };
}
}
}
[TestFixture]
public class SetOfSetsDeepQueryTest : BagDeepQueryTest
{
protected override System.Collections.IList Mappings
{
get
{
return new string[] { "NHSpecificTest.NHXXXX.MapSetOfSets.hbm.xml" };
}
}
}
}