using System; using System.Collections.Generic; using System.Data; using System.IO; using ConsoleApp1.Model; using ConsoleApp1.ORM; using NHibernate.Tool.hbm2ddl; using NUnit.Framework; namespace ConsoleApp1.Repository { [TestFixture] public class NHibernatePersonRepositoryTest { private PersonRepository _personRepo; [SetUp] public void CreateSchema() { DeleteDatabaseIfExists(); var schemaUpdate = new SchemaUpdate(NHibernateHelper.Configuration); schemaUpdate.Execute(false, true); _personRepo = new PersonRepositoryNHibernate(); } [Test] public void CanSavePerson() { _personRepo.Save(new Person()); Assert.AreEqual(1, _personRepo.RowCount()); } [Test] public void CanGetPerson() { var person = new Person(); _personRepo.Save(person); Assert.AreEqual(1, _personRepo.RowCount()); person = _personRepo.Get(person.Id); Assert.IsNotNull(person); } [Test] public void CanUpdatePerson() { var person = new Person(); _personRepo.Save(person); Assert.AreEqual(1, _personRepo.RowCount()); person = _personRepo.Get(person.Id); person.FirstName = "Test"; _personRepo.Update(person); Assert.AreEqual(1, _personRepo.RowCount()); Assert.AreEqual("Test", _personRepo.Get(person.Id).FirstName); } [Test] public void CanDeletePerson() { var person = new Person(); _personRepo.Save(person); Assert.AreEqual(1, _personRepo.RowCount()); _personRepo.Delete(person); Assert.AreEqual(0, _personRepo.RowCount()); } [Test] public void TestBulkInsert() { static IEnumerable GeneratePersons(int num = 3) { var ret = new Person[num]; for (var ii = 0; ii < num; ii++) ret[ii] = new Person {Id = Guid.NewGuid(), FirstName = "bac", LastName = "fif"}; return ret; } _personRepo.Save(GeneratePersons(10)); } [TearDown] public void DeleteDatabaseIfExists() { if (File.Exists("test.db")) File.Delete("test.db"); } } }