{
private DBPerson dbperson;
[TestFixtureSetUp]
public void Init()
{
dbperson = new DBPerson();
}
[Test]
public void TestInsert()
{
Person person = new Person();
person.Username = "韩迎龙";
person.Password = "citsoft";
person.Age = 21;
dbperson.Insert(person);
int maxid = this.GetMaxId();
person.Id = maxid;
Person person2 = dbperson.GetByID(maxid);
this.Company(person, person2);
//做完测试后记得删除数据库中的数据
dbperson.RemoveID(maxid);
}
[Test]
public void TestUpdate()
{
Person person = new Person();
person.Username = "韩鑫";
person.Password = "hanyinglong";
person.Age = 20;
dbperson.Insert(person);
int maxid = this.GetMaxId();
Person person2 = dbperson.GetByID(maxid);
person2.Username = "韩迎龙";
person2.Password = "citsoft";
person2.Age = 20;
dbperson.Update(person2);
Person person3 = dbperson.GetByID(maxid);
this.Company(person2, person3);
dbperson.RemoveID(maxid); //清空数据库
}
[Test]
public void TestGetByID()
{
Person person = new Person();
person.Username = "韩迎龙";
person.Password = "citsoft";
person.Age = 21;
dbperson.Insert(person);
int maxid = this.GetMaxId();
person.Id = maxid;
Person person2 = dbperson.GetByID(maxid);
this.Company(person, person2);
dbperson.RemoveID(maxid);
}
[Test]
public void TestRemoveByID()
{
Person person = new Person();
person.Username = "韩迎龙";
person.Password = "citsoft";
person.Age = 21;
dbperson.Insert(person);
int maxid = this.GetMaxId();
dbperson.RemoveID(maxid);
Person person2 = dbperson.GetByID(maxid);
Assert.IsNull(person2);
}
private int GetMaxId()
{
string sql = "select max(id) as maxID from person";
SqlConnection conn = connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader reader = command.ExecuteReader();
int maxID = 0;
if (reader.Read())
{
maxID = Convert.ToInt32(reader["maxID"]);
//Console.WriteLine(reader["maxID"]);
}
reader.Close();
conn.Close();
return maxID;
}
private void Company(Person person1, Person person2)
{
Assert.AreEqual(person1.Id, person2.Id);
Assert.AreEqual(person1.Username, person2.Username);
Assert.AreEqual(person1.Password, person2.Password);
Assert.AreEqual(person1.Age, person2.Age);
}
}
}