This repository has been archived on 2020-06-09. You can view files and clone it, but cannot push or open issues or pull requests.
dotnet-nhibernate-test/ConsoleApp1/Repository/PersonRepositoryTest.cs

94 lines
2.5 KiB
C#
Raw Permalink Normal View History

2020-06-08 22:12:01 +00:00
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()
{
2020-06-09 19:10:20 +00:00
static Person[] GeneratePersons(int num = 3)
2020-06-08 22:12:01 +00:00
{
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;
}
2020-06-09 19:10:20 +00:00
var persons = GeneratePersons(10000);
_personRepo.Save(persons);
Assert.AreEqual(10000, _personRepo.RowCount());
2020-06-08 22:12:01 +00:00
}
[TearDown]
public void DeleteDatabaseIfExists()
{
if (File.Exists("test.db"))
File.Delete("test.db");
}
}
}