public void Users_UpdateTestWithoutPass() { //Creates an User with data. Users aUser = new Users(-1, "Nombre", "Apellido", Convert.ToDateTime("12/04/1982"), "NickToDel", "12345678", "12345678", "*****@*****.**"); //Try to save first aUser = aUser.saveUserData(connString); Assert.AreNotEqual(-1, aUser.userID); //1=save data without any problem. //Try to find it and take the userID //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 Users aUserToFind = aUser.findUserByUserName(connString, aUser.userName); Assert.AreEqual(aUser.userName, aUserToFind.userName); Int64 expectedUserID = aUser.userID; //Try to Update previous saved user. aUser.userSecondName = "Second Name Updated"; aUser = aUser.updateUserData(connString,false); Assert.AreNotEqual(-1, aUser.userID); //1=update data without any problem. //Try to find it //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 aUserToFind = aUser.findUserByUserID(connString,aUser.userID); Assert.AreEqual(aUser.userName, aUserToFind.userName); //Verify the update. Assert.AreEqual(aUser.userSecondName,aUserToFind.userSecondName); //Try to Delete previous saved user. int res = Users.deleteUserByUserID(connString, aUser.userID); Assert.AreEqual(1, res); //Deleted with success //Try to find it aUser = aUser.findUserByUserID(connString, aUser.userID); Assert.AreEqual(-1, aUser.userID); //user not found because was deleted }
//Update an User object into BD using a connection and a userID, suposses data are checked with checkUserData. public Users updateUserData(string connString, Boolean savePassword) { //Return the user updated. If error return an empty user with userID=-1 Users aUser = new Users(); using (SqlConnection conn = new SqlConnection(connString)) { //string pass = userPass; string query = null; if (!savePassword) { query = @"UPDATE Users SET UserFirstName=@userFirstName, UserSecondName=@userSecondName, UserBdate=@userBDate, UserName=@userName , UserMail=@userMail WHERE UserID=@userID"; } else { query = String.Format(@"UPDATE Users SET UserFirstName=@userFirstName, UserSecondName=@userSecondName, UserBdate=@userBDate, UserName=@userName, UserPass=PwdEncrypt('{0}'), UserMail=@userMail WHERE UserID=@userID", userPass); } using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.BigInt)); cmd.Parameters["@userID"].Value = userID; cmd.Parameters.Add(new SqlParameter("@userFirstName", SqlDbType.VarChar)); cmd.Parameters["@userFirstName"].Value = userFirstName; cmd.Parameters.Add(new SqlParameter("@userSecondName", SqlDbType.VarChar)); cmd.Parameters["@userSecondName"].Value = userSecondName; cmd.Parameters.Add(new SqlParameter("@userBDate", SqlDbType.Date)); cmd.Parameters["@userBDate"].Value = userBDate; cmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar)); cmd.Parameters["@userName"].Value = userName; cmd.Parameters.Add(new SqlParameter("@userMail", SqlDbType.VarChar)); cmd.Parameters["@userMail"].Value = userMail; conn.Open(); int res = cmd.ExecuteNonQuery(); //Find the userID asigned. if (res > 0) { aUser = aUser.findUserByUserID(connString, userID); } } } return aUser; }
public void Users_TestFindUserByID() { //Creates an User with data. int userID = 78; Users aUser = new Users(-1, "", "", DateTime.Now, "", "", "", ""); //findUserByUserID returns a user if find, if not find return a user wit UserID=-1 aUser = aUser.findUserByUserID (connString, userID); Assert.AreNotEqual(-1, aUser.userID); Assert.AreEqual(userID, aUser.userID); }