/******Test SqlHelper********/
[TestFixture]
public class TestDBPersonSqlHelper
{
private Person personNUnit;
private Person personDB;
[SetUp] //初始化
public void Init()
{
personNUnit = new Person();
personDB = new Person();
}
// 重构出公共函数
private void AssertCompare(Person expectedPerson, Person currentPerson)
{
Assert.AreEqual(expectedPerson.Id, currentPerson.Id);
Assert.AreEqual(expectedPerson.Name, currentPerson.Name);
Assert.AreEqual(expectedPerson.Age, currentPerson.Age);
}
[Test] //测试添加对象
public void TestAddPerson()
{
personNUnit.Name = "Vincent";
personNUnit.Age = 24;
DBPersonSqlHelper.AddPerson(personNUnit);
int id = DBPersonSqlHelper.GetMaxPersonID();
personNUnit.Id = id;
personDB = DBPersonSqlHelper.GetPersonByID(id);
AssertCompare(personNUnit, personDB);
DBPersonSqlHelper.DeletePersonByID(id); //测试完后清除测试的数据
}
[Test] //测试更新对象
public void TestUpdatePerson()
{
personNUnit.Id = 26;
personNUnit.Name = "abc";
personNUnit.Age = 10;
DBPersonSqlHelper.UpdatePerson(personNUnit);
personDB = DBPersonSqlHelper.GetPersonByID(personNUnit.Id);
AssertCompare(personNUnit, personDB);
}
[Test] //测试获取最大的对象ID
public void TestGetMaxID()
{
int i;
i = DBPersonSqlHelper.GetMaxPersonID();
Assert.AreEqual(27, i);
}
[Test] //测试对象是否存在
public void TestPersonIsExistById()
{
int p_id = 25;
bool isExist = true;
isExist = DBPersonSqlHelper.PersonIsExistById(p_id);
Assert.AreEqual(true, isExist);
}
[Test]//测试删除对象
public void TestDeletePerson()
{
int id = 26;
DBPersonSqlHelper.DeletePersonByID(id);
Assert.AreEqual(false, DBPersonSqlHelper.PersonIsExistById(id));
}
[Test] //测试表中是否含有数据
public void TestTableHasRecord()
{
bool hasRecord = true;
hasRecord = DBPersonSqlHelper.TableHasRecord();
Assert.AreEqual(true, hasRecord);
}
[Test]//测试通过ID查询对象
public void TestSearchPersonByID()
{
personNUnit.Id = 21;
personNUnit.Name = "tom";
personNUnit.Age = 16;
personDB = DBPersonSqlHelper.GetPersonByID(personNUnit.Id);
//AssertCompare(personNUnit, personDB);
Assert.AreEqual(null, personDB); // 测试搜索不存在的用户时用这个语句
}
[Test]//测试查询所有对象
public void TestGetManyPersons()
{
List<Person> lstPerson = new List<Person>();
lstPerson = DBPersonSqlHelper.GetManyPersons();
Assert.AreEqual(1, lstPerson.Count);
}
}
/***********Test DB ***********/
[TestFixture]
public class TestDBPerson
{
private Person personNUnit;
private Person personDB;
[SetUp]
public void Init()
{
personNUnit = new Person();
personDB = new Person();
}
private void AssertCompare(Person expectedPerson, Person currentPerson)
{
Assert.AreEqual(expectedPerson.Id, currentPerson.Id);
Assert.AreEqual(expectedPerson.Name, currentPerson.Name);
Assert.AreEqual(expectedPerson.Age, currentPerson.Age);
}
[Test]
public void TestAddPerson()
{
personNUnit.Name = "Vincent";
personNUnit.Age = 24;
DBPerson.AddPerson(personNUnit);
int id = DBPerson.GetMaxPersonID();
personNUnit.Id = id;
personDB = DBPerson.GetPersonByID(id);
AssertCompare(personNUnit, personDB);
DBPerson.DeletePersonByID(id); //测试完后清除测试的数据
}
[Test]
public void TestUpdatePerson()
{
personNUnit.Id = 10;
personNUnit.Name = "Vincent";
personNUnit.Age = 24;
DBPerson.UpdatePerson(personNUnit);
personDB = DBPerson.GetPersonByID(personNUnit.Id);
AssertCompare(personNUnit,personDB);
}
[Test]
public void TestGetMaxID()
{
int i;
i = DBPerson.GetMaxPersonID();
Assert.AreEqual(15,i);
}
[Test,Explicit]
public void TestPersonIsExistById()
{
int p_id = 11;
bool isExist = true;
isExist = DBPerson.PersonIsExistById(p_id);
Assert.AreEqual(false, isExist);
//Console.WriteLine("isExist: "+isExist);
}
[Test]
public void TestDeletePerson()
{
int id = 16;
DBPerson.DeletePersonByID(id);
Assert.AreEqual(false, DBPerson.PersonIsExistById(id));
}
[Test]
public void TestTableHasRecord()
{
bool hasRecord = true;
hasRecord = DBPerson.TableHasRecord();
Assert.AreEqual(true,hasRecord);
}
[Test]
public void TestSearchPersonByID()
{
personNUnit.Id = 17;
personNUnit.Name = "tom";
personNUnit.Age = 16;
personDB = DBPerson.GetPersonByID(personNUnit.Id);
AssertCompare(personNUnit, personDB);
//Assert.AreEqual(null, personDB);
}
}
/***********Test DB Connection ***********/
[TestFixture]
public class TestConnection
{
private SqlConnection conn;
[Test]
public void TestSqlConnection()
{
Assert.AreEqual(ConnectionState.Open, conn.State);
}
[TestFixtureSetUp]
public void Init()
{
conn = Connection.GetSqlConnection();
}
[TestFixtureTearDown]
public void Destroy()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}