public void SportType_CheckDataBeforeSave() { //Create a SportType object with data. aSportType.LoadData(-1, "Weird Sport", 0, "any kind of weird sport", 85,1); int res = aSportType.CheckData(connString); Assert.AreEqual(1, res); //Data are ok because the type of sport isn´t in de BD. //Save the data into BD, and check again. aSportType = aSportType.SaveData(connString); Int64 sportTypeIDUsed = aSportType.SportTypeID; Int64 userIDUsed = aSportType.UserID; res = aSportType.CheckData(connString); Assert.AreEqual(-1, res); //Data are not ok, SportTypeName is in de BD. //Create a SportType object with data: aSportType.LoadData(-1, "Weird Sport2", -10, "any kind of weird sport", 85,1); res = aSportType.CheckData(connString); Assert.AreEqual(-2, res); //Data are not ok, parentSportID does not exist on SportTypes //Create a SportType object with data: aSportType.LoadData(-1, "", 0, "any kind of weird sport", 85,1); res = aSportType.CheckData(connString); Assert.AreEqual(0, res); //Data are not ok, SportTypeName is empty. //Create a SportType object with data: aSportType.LoadData(-1, "Weird Sport2", 0, "any kind of weird sport", -100,1); res = aSportType.CheckData(connString); Assert.AreEqual(-3, res); //Data are not ok, userID not in data base. //Deleting object saved first. aSportType = aSportType.FindSportTypeByID(connString); res = aSportType.Delete(connString); Assert.AreEqual(1, res); }
public void SportType_FindParent() { //Create a SportType object to be the parent string sportTypeNameUsed = "Deporte Padre"; Int64 parentSportTypeIDUsed = 0; string memoUsed = "Deporte padre"; Int64 userIDUsed = 85; SportTypes aSportTypeParent = new SportTypes(); aSportTypeParent.LoadData(-1, sportTypeNameUsed, parentSportTypeIDUsed, memoUsed, userIDUsed,1); //Save into BD obtaining the sportTypeID aSportTypeParent = aSportTypeParent.SaveData(connString); Assert.AreNotEqual(0, aSportTypeParent.SportTypeID); //saving correct data //Create a SportType object to be the child sportTypeNameUsed = "Deporte Hijo"; parentSportTypeIDUsed = aSportTypeParent.SportTypeID; memoUsed = "Deporte Hijo"; userIDUsed = 85; SportTypes aSportTypeChild = new SportTypes(); aSportTypeChild.LoadData(-1, sportTypeNameUsed, parentSportTypeIDUsed, memoUsed, userIDUsed,1); //Save into BD obtaining the sportTypeID aSportTypeChild = aSportTypeChild.SaveData(connString); Assert.AreNotEqual(0, aSportTypeChild.SportTypeID); //saving correct data //Try to find the parent sport type. SportTypes parentSportTypeToFind = new SportTypes(); parentSportTypeToFind = aSportTypeChild.FindParentSportType(connString); Assert.AreNotEqual(-1, parentSportTypeToFind.SportTypeID); //Found it!! Assert.AreEqual(aSportTypeParent.SportTypeID, parentSportTypeToFind.SportTypeID); Assert.AreEqual(aSportTypeParent.SportTypeName, parentSportTypeToFind.SportTypeName); Assert.AreEqual(aSportTypeParent.ParentSportTypeID, parentSportTypeToFind.ParentSportTypeID,1); Assert.AreEqual(aSportTypeParent.Memo, parentSportTypeToFind.Memo); //Deleting object previously saved int res = aSportTypeParent.Delete(connString); Assert.AreEqual(1, res); //Deleting object previously saved res = aSportTypeChild.Delete(connString); Assert.AreEqual(1, res); }
//Update an User object into BD using a connection and a userID, suposses data are checked with checkUserData. public SportTypes UpdateData(string connString) { //Return the user updated. If error return an empty user with userID=-1 SportTypes aSportType = new SportTypes(); using (SqlConnection conn = new SqlConnection(connString)) { //string pass = userPass; string query = null; query = @"UPDATE SportTypes SET SportTypeName=@sportTypeName, ParentSportTypeID=@parentSportTypeID, Memo=@memo, UserID=@userID, DistForPace=@distForPace WHERE SportTypeID=@sportTypeID"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@sportTypeID", SqlDbType.BigInt)); cmd.Parameters["@sportTypeID"].Value = sportTypeID; cmd.Parameters.Add(new SqlParameter("@sportTypeName", SqlDbType.VarChar)); cmd.Parameters["@sportTypeName"].Value = sportTypeName; cmd.Parameters.Add(new SqlParameter("@parentSportTypeID", SqlDbType.BigInt)); cmd.Parameters["@parentSportTypeID"].Value = parentSportTypeID; cmd.Parameters.Add(new SqlParameter("@memo", SqlDbType.VarChar)); cmd.Parameters["@memo"].Value = memo; cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.BigInt)); cmd.Parameters["@userID"].Value = userID; cmd.Parameters.Add(new SqlParameter("@distForPace", SqlDbType.Float)); cmd.Parameters["@distForPace"].Value = distForPace; conn.Open(); int res = cmd.ExecuteNonQuery(); //Find the userID asigned. if (res > 0) { aSportType.LoadData(sportTypeID, sportTypeName, parentSportTypeID, memo, userID, distForPace); } } } return aSportType; }
//Save SportType object into BD returning the ID of the object inserted. 0 in case of error public SportTypes SaveData(string connString) { Int64 res = 0; SportTypes aSportType = new SportTypes(); using (SqlConnection conn = new SqlConnection(connString)) { string query = @"INSERT INTO SportTypes(SportTypeName, ParentSportTypeID, Memo, UserID, DistForPace) VALUES(@sportTypeName, @parentSportTypeID, @memo, @userID, @distForPace); SELECT CAST(SCOPE_IDENTITY() AS bigint)"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@sportTypeName", SqlDbType.VarChar)); cmd.Parameters["@sportTypeName"].Value = sportTypeName; cmd.Parameters.Add(new SqlParameter("@parentSportTypeID", SqlDbType.BigInt)); cmd.Parameters["@parentSportTypeID"].Value = parentSportTypeID; cmd.Parameters.Add(new SqlParameter("@memo", SqlDbType.VarChar)); cmd.Parameters["@memo"].Value = memo; cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.BigInt)); cmd.Parameters["@userID"].Value = userID; cmd.Parameters.Add(new SqlParameter("@distForPace", SqlDbType.Float)); cmd.Parameters["@distForPace"].Value = distForPace; conn.Open(); //res = cmd.ExecuteNonQuery(); res = (Int64)cmd.ExecuteScalar(); if (res > 0) { aSportType.LoadData(res, sportTypeName, parentSportTypeID, memo, userID, distForPace); } } } return aSportType; }
//Find an SportType object by a given name, and return this object. If not find returns an SportType object with ID=-1 public SportTypes FindSportTypeByName(string connString) { SportTypes aSportType = new SportTypes(); using (SqlConnection conn = new SqlConnection(connString)) { string query = "SELECT * from SportTypes WHERE SportTypeName = @sportTypeName AND UserID=@userID"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@sportTypeName", SqlDbType.VarChar)); cmd.Parameters["@SportTypeName"].Value = sportTypeName; cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.BigInt)); cmd.Parameters["@userID"].Value = userID; conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { aSportType.LoadData(reader.GetInt64(0), reader.GetString(1), reader.GetInt64(2), reader.GetString(3), reader.GetInt64(4),reader.GetDouble(5)); } reader.Close(); } } return aSportType; }
public void SportType_Update() { //Create a SportType object string sportTypeNameUsed = "Bicicleta"; Int64 parentSportTypeIDUsed = 0; string memoUsed = "Deportes de bicicleta"; Int64 userIDUsed = 85; SportTypes aSportType = new SportTypes(); aSportType.LoadData(-1, sportTypeNameUsed, parentSportTypeIDUsed, memoUsed, userIDUsed,1); //Save into BD obtaining the sportTypeID aSportType = aSportType.SaveData(connString); Assert.AreNotEqual(0, aSportType.SportTypeID); Assert.AreNotEqual(0, aSportType.SportTypeID); //saving correct data Int64 sportTypeIDUsed = aSportType.SportTypeID; //Try to find and check values aSportType = aSportType.FindSportTypeByName(connString); Assert.AreNotEqual(-1, aSportType.SportTypeID); Assert.AreEqual(sportTypeNameUsed, aSportType.SportTypeName); Assert.AreEqual(parentSportTypeIDUsed, aSportType.ParentSportTypeID); Assert.AreEqual(memoUsed, aSportType.Memo); Assert.AreEqual(userIDUsed, aSportType.UserID); //Update values sportTypeNameUsed = "Bike Updated"; parentSportTypeIDUsed = 0; memoUsed = "Deportes de bicicleta updated"; userIDUsed = 85; aSportType.LoadData(sportTypeIDUsed, sportTypeNameUsed, parentSportTypeIDUsed, memoUsed, userIDUsed,1); aSportType.UpdateData(connString); //Try to find and check values aSportType = aSportType.FindSportTypeByName(connString); Assert.AreNotEqual(-1, aSportType.SportTypeID); Assert.AreEqual(sportTypeNameUsed, aSportType.SportTypeName); Assert.AreEqual(parentSportTypeIDUsed, aSportType.ParentSportTypeID); Assert.AreEqual(memoUsed, aSportType.Memo); Assert.AreEqual(userIDUsed, aSportType.UserID); //Deleting object previously saved int res = aSportType.Delete(connString); Assert.AreEqual(1, res); //Try to find again after deleting aSportType = aSportType.FindSportTypeByID(connString); Assert.AreEqual(-1, aSportType.SportTypeID); }