namespace HRProject.Test
{
using NUnit.Framework;
using NHibernate;
using HRProject.Model;
using HRProject.Setup;
using HRProject.Utils;
using NHibernate.Linq;
[TestFixture]
public class RepositoryTest
{
[SetUp]
public void SetUp()
{
ISession session = SessionHelper.GetSession();
using (ITransaction tx = session.BeginTransaction())
{
foreach (Cashier c in session.Query<Cashier>().ToList<Cashier>())
{
session.Delete(c);
}
tx.Commit();
}
using (ITransaction tx = session.BeginTransaction())
{
Cashier c1 = new Cashier(){Description = "D.Rathnayaka"};
Cashier c2 = new Cashier(){Description = "K.Kumara"};
Cashier c3 = new Cashier(){Description = "R.Gunapala"};
session.Save(c1);
session.Save(c2);
session.Save(c3);
tx.Commit();
}
}
[Test]
public void TestLoadUp()
{
ISession session = SessionHelper.GetSession();
using (ITransaction tx = session.BeginTransaction())
{
Repository<Cashier> repo = new Repository<Cashier>();
Assert.AreEqual(3, repo.Items.Count);
Assert.AreEqual(1, (from Cashier c in repo.Items
where c.Description == "D.Rathnayaka"
select true).Count());
tx.Commit();
}
}
[Test]
public void TestUpdate()
{
ISession session = SessionHelper.GetSession();
using (ITransaction tx = session.BeginTransaction())
{
Repository<Cashier> repo = new Repository<Cashier>();
Cashier cas = (from Cashier c in repo.Items
where c.Description == "D.Rathnayaka"
select c).Single();
cas.Description = "K.R";
session.Flush();
tx.Commit();
}
using (ITransaction tx = session.BeginTransaction())
{
Repository<Cashier> repo = new Repository<Cashier>();
//Fails here//
Assert.IsNotNull((from Cashier c in repo.Items
where c.Description == "K.R"
select c).Single());
}
}
}
}
Может кто-нибудь сказать мне, почему этот тест не проходит? БД тоже не обновляется. Класс хранилища ниже:
namespace HRProject.Utils
{
using NHibernate;
using HRProject.Setup;
using NHibernate.Linq;
class Repository<T> where T : class
{
public IList<T> Items {get; set;}
public Repository()
{
Items = new List<T>();
ISession session = SessionHelper.GetSession();
using (ITransaction tx = session.BeginTransaction())
{
List<T> list = session.Query<T>().ToList();
foreach (T obj in list)
{
Items.Add((T)session.Merge(obj));
}
tx.Commit();
}
}
public void AddRepositoryObject(T obj)
{
}
}
}