public void Create_ShouldThrowException_GivenDuplicateSessionCode() { // Insert the duplicate Session data row in the database. int sessionID = SessionTestTable.InsertPlaceholder(sessionCode: "6dk61ufcuzp3f7vs"); // Build the Session data row. SessionDataRow sessionDataRow = new SessionDataRow(); sessionDataRow.SessionCode = "6dk61ufcuzp3f7vs"; sessionDataRow.Name = "Session Alpha"; sessionDataRow.StartDate = new DateTime(2001, 1, 1); // Build the database connection. using (DatabaseConnection databaseConnection = new DatabaseConnection(TestDatabase.ConnectionString)) { // Open the database connection. databaseConnection.Open().Wait(); try { // Create the Session data row. SessionDataAccessComponent sessionDataAccessComponent = new SessionDataAccessComponent(); sessionDataAccessComponent.Create(databaseConnection, sessionDataRow).Wait(); // Validate an exception was thrown. Assert.Fail(); } catch (AggregateException ex) { // Validate an SQL exception was thrown. Assert.IsInstanceOfType(ex.InnerExceptions[0], typeof(SqlException)); } } }
public void Create_ShouldSucceed() { // Build the Session data row. SessionDataRow sessionDataRow = new SessionDataRow(); sessionDataRow.SessionCode = "6dk61ufcuzp3f7vs"; sessionDataRow.Name = "Session Alpha"; sessionDataRow.StartDate = new DateTime(2001, 1, 1); // Build the database connection. using (DatabaseConnection databaseConnection = new DatabaseConnection(TestDatabase.ConnectionString)) { // Open the database connection. databaseConnection.Open().Wait(); // Create the Session data row. SessionDataAccessComponent sessionDataAccessComponent = new SessionDataAccessComponent(); sessionDataAccessComponent.Create(databaseConnection, sessionDataRow).Wait(); } // Validate the SessionID was generated. Assert.AreNotEqual(0, sessionDataRow.SessionID); // Validate the Session data row was inserted in the database. SessionTestTable.AssertPresence( sessionDataRow.SessionID, "6dk61ufcuzp3f7vs", "Session Alpha", new DateTime(2001, 1, 1)); }
/// <summary> /// Sets the parameter values on the specified SQL command. /// </summary> private void SetSqlCommandParameterValues(SqlCommand sqlCommand, SessionDataRow sessionDataRow, bool setPrimaryKeyValue) { // Set the primary key if requested. if (setPrimaryKeyValue) { sqlCommand.Parameters.Add("@sessionID", SqlDbType.Int).Value = sessionDataRow.SessionID; } // Set the other parameters. sqlCommand.Parameters.Add("@sessionCode", SqlDbType.NVarChar, 50).Value = sessionDataRow.SessionCode; sqlCommand.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = sessionDataRow.Name; sqlCommand.Parameters.Add("@startDate", SqlDbType.Date).Value = sessionDataRow.StartDate; }
/// <summary> /// Gets the values from the specified SQL data reader. /// </summary> private SessionDataRow GetSqlDataReaderValues(SqlDataReader sqlDataReader) { // Build the Session data row. SessionDataRow sessionDataRow = new SessionDataRow(); // Read the values. sessionDataRow.SessionID = (int)sqlDataReader["SessionID"]; sessionDataRow.SessionCode = (string)sqlDataReader["SessionCode"]; sessionDataRow.Name = (string)sqlDataReader["Name"]; sessionDataRow.StartDate = (DateTime)sqlDataReader["StartDate"]; // Return the Session data row. return sessionDataRow; }
/// <summary> /// Creates the specified Session data row. /// </summary> public async Task Create(IDatabaseConnection databaseConnection, SessionDataRow sessionDataRow) { // Build the SQL command. using (SqlCommand sqlCommand = new SqlCommand("INSERT INTO [Session] VALUES (@sessionCode, @name, @startDate); SELECT CAST(SCOPE_IDENTITY() AS INT);")) { // Use the specified database connection. SqlConnection sqlConnection = (databaseConnection as DatabaseConnection).SqlConnection; sqlCommand.Connection = sqlConnection; // Set the SQL command parameter values. this.SetSqlCommandParameterValues(sqlCommand, sessionDataRow, setPrimaryKeyValue: false); // Execute the SQL command. int sessionID = (int)await sqlCommand.ExecuteScalarAsync(); // Assign the generated SessionID. sessionDataRow.SessionID = sessionID; } }
/// <summary> /// Deletes the specified Session data row. /// </summary> public async Task Delete(IDatabaseConnection databaseConnection, SessionDataRow sessionDataRow) { // Build the SQL command. using (SqlCommand sqlCommand = new SqlCommand("DELETE FROM [Session] WHERE [SessionID] = @sessionID;")) { // Use the specified database connection. SqlConnection sqlConnection = (databaseConnection as DatabaseConnection).SqlConnection; sqlCommand.Connection = sqlConnection; // Set the SQL command parameter values. sqlCommand.Parameters.Add("@sessionID", SqlDbType.Int).Value = sessionDataRow.SessionID; // Execute the SQL command. await sqlCommand.ExecuteNonQueryAsync(); } }
/// <summary> /// Updates the specified Session data row. /// </summary> public async Task Update(IDatabaseConnection databaseConnection, SessionDataRow sessionDataRow) { // Build the SQL command. using (SqlCommand sqlCommand = new SqlCommand("UPDATE [Session] SET [SessionCode] = @sessionCode, [Name] = @name, [StartDate] = @startDate WHERE [SessionID] = @sessionID;")) { // Use the specified database connection. SqlConnection sqlConnection = (databaseConnection as DatabaseConnection).SqlConnection; sqlCommand.Connection = sqlConnection; // Set the SQL command parameter values. this.SetSqlCommandParameterValues(sqlCommand, sessionDataRow, setPrimaryKeyValue: true); // Execute the SQL command. await sqlCommand.ExecuteNonQueryAsync(); } }
public void Update_ShouldSucceed() { // Insert the Session data row in the database. int sessionID = SessionTestTable.InsertWithValues( "6dk61ufcuzp3f7vs", "Session Alpha", new DateTime(2001, 1, 1)); // Build the Session data row. SessionDataRow sessionDataRow = new SessionDataRow(); sessionDataRow.SessionID = sessionID; sessionDataRow.SessionCode = "n3p4y556gt9f17hw"; sessionDataRow.Name = "Session Bravo"; sessionDataRow.StartDate = new DateTime(2002, 2, 2); // Build the database connection. using (DatabaseConnection databaseConnection = new DatabaseConnection(TestDatabase.ConnectionString)) { // Open the database connection. databaseConnection.Open().Wait(); // Update the Session data row. SessionDataAccessComponent sessionDataAccessComponent = new SessionDataAccessComponent(); sessionDataAccessComponent.Update(databaseConnection, sessionDataRow).Wait(); } // Validate the Session data row was updated in the database. SessionTestTable.AssertPresence( sessionID, "n3p4y556gt9f17hw", "Session Bravo", new DateTime(2002, 2, 2)); }