public void Create_ShouldSucceed() { // Insert the master data rows in the database. int courseScheduleID = CourseScheduleTestTable.InsertPlaceholder(); // Build the CourseGroup data row. CourseGroupDataRow courseGroupDataRow = new CourseGroupDataRow(); courseGroupDataRow.CourseGroupCode = "5s1cgndj6e5x0uvz"; courseGroupDataRow.CourseScheduleID = courseScheduleID; courseGroupDataRow.PlacesCount = 1; // Build the database connection. using (DatabaseConnection databaseConnection = new DatabaseConnection(TestDatabase.ConnectionString)) { // Open the database connection. databaseConnection.Open().Wait(); // Create the CourseGroup data row. CourseGroupDataAccessComponent courseGroupDataAccessComponent = new CourseGroupDataAccessComponent(); courseGroupDataAccessComponent.Create(databaseConnection, courseGroupDataRow).Wait(); } // Validate the CourseGroupID was generated. Assert.AreNotEqual(0, courseGroupDataRow.CourseGroupID); // Validate the CourseGroup data row was inserted in the database. CourseGroupTestTable.AssertPresence( courseGroupDataRow.CourseGroupID, "5s1cgndj6e5x0uvz", courseScheduleID, 1); }
/// <summary> /// Sets the parameter values on the specified SQL command. /// </summary> private void SetSqlCommandParameterValues(SqlCommand sqlCommand, CourseGroupDataRow courseGroupDataRow, bool setPrimaryKeyValue) { // Set the primary key if requested. if (setPrimaryKeyValue) { sqlCommand.Parameters.Add("@courseGroupID", SqlDbType.Int).Value = courseGroupDataRow.CourseGroupID; } // Set the other parameters. sqlCommand.Parameters.Add("@courseGroupCode", SqlDbType.NVarChar, 50).Value = courseGroupDataRow.CourseGroupCode; sqlCommand.Parameters.Add("@courseScheduleID", SqlDbType.Int).Value = courseGroupDataRow.CourseScheduleID; sqlCommand.Parameters.Add("@placesCount", SqlDbType.Int).Value = courseGroupDataRow.PlacesCount; }
/// <summary> /// Gets the values from the specified SQL data reader. /// </summary> private CourseGroupDataRow GetSqlDataReaderValues(SqlDataReader sqlDataReader) { // Build the CourseGroup data row. CourseGroupDataRow courseGroupDataRow = new CourseGroupDataRow(); // Read the values. courseGroupDataRow.CourseGroupID = (int)sqlDataReader["CourseGroupID"]; courseGroupDataRow.CourseGroupCode = (string)sqlDataReader["CourseGroupCode"]; courseGroupDataRow.CourseScheduleID = (int)sqlDataReader["CourseScheduleID"]; courseGroupDataRow.PlacesCount = (int)sqlDataReader["PlacesCount"]; // Return the CourseGroup data row. return courseGroupDataRow; }
/// <summary> /// Creates the specified CourseGroup data row. /// </summary> public async Task Create(IDatabaseConnection databaseConnection, CourseGroupDataRow courseGroupDataRow) { // Build the SQL command. using (SqlCommand sqlCommand = new SqlCommand("INSERT INTO [CourseGroup] VALUES (@courseGroupCode, @courseScheduleID, @placesCount); 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, courseGroupDataRow, setPrimaryKeyValue: false); // Execute the SQL command. int courseGroupID = (int)await sqlCommand.ExecuteScalarAsync(); // Assign the generated CourseGroupID. courseGroupDataRow.CourseGroupID = courseGroupID; } }
public void Create_ShouldThrowException_GivenDuplicateCourseGroupCode() { // Insert the master data rows in the database. int courseScheduleID = CourseScheduleTestTable.InsertPlaceholder(); // Insert the duplicate CourseGroup data row in the database. int courseGroupID = CourseGroupTestTable.InsertPlaceholder(courseGroupCode: "5s1cgndj6e5x0uvz"); // Build the CourseGroup data row. CourseGroupDataRow courseGroupDataRow = new CourseGroupDataRow(); courseGroupDataRow.CourseGroupCode = "5s1cgndj6e5x0uvz"; courseGroupDataRow.CourseScheduleID = courseScheduleID; courseGroupDataRow.PlacesCount = 1; // Build the database connection. using (DatabaseConnection databaseConnection = new DatabaseConnection(TestDatabase.ConnectionString)) { // Open the database connection. databaseConnection.Open().Wait(); try { // Create the CourseGroup data row. CourseGroupDataAccessComponent courseGroupDataAccessComponent = new CourseGroupDataAccessComponent(); courseGroupDataAccessComponent.Create(databaseConnection, courseGroupDataRow).Wait(); // Validate an exception was thrown. Assert.Fail(); } catch (AggregateException ex) { // Validate an SQL exception was thrown. Assert.IsInstanceOfType(ex.InnerExceptions[0], typeof(SqlException)); } } }
/// <summary> /// Deletes the specified CourseGroup data row. /// </summary> public async Task Delete(IDatabaseConnection databaseConnection, CourseGroupDataRow courseGroupDataRow) { // Build the SQL command. using (SqlCommand sqlCommand = new SqlCommand("DELETE FROM [CourseGroup] WHERE [CourseGroupID] = @courseGroupID;")) { // Use the specified database connection. SqlConnection sqlConnection = (databaseConnection as DatabaseConnection).SqlConnection; sqlCommand.Connection = sqlConnection; // Set the SQL command parameter values. sqlCommand.Parameters.Add("@courseGroupID", SqlDbType.Int).Value = courseGroupDataRow.CourseGroupID; // Execute the SQL command. await sqlCommand.ExecuteNonQueryAsync(); } }
/// <summary> /// Updates the specified CourseGroup data row. /// </summary> public async Task Update(IDatabaseConnection databaseConnection, CourseGroupDataRow courseGroupDataRow) { // Build the SQL command. using (SqlCommand sqlCommand = new SqlCommand("UPDATE [CourseGroup] SET [CourseGroupCode] = @courseGroupCode, [CourseScheduleID] = @courseScheduleID, [PlacesCount] = @placesCount WHERE [CourseGroupID] = @courseGroupID;")) { // Use the specified database connection. SqlConnection sqlConnection = (databaseConnection as DatabaseConnection).SqlConnection; sqlCommand.Connection = sqlConnection; // Set the SQL command parameter values. this.SetSqlCommandParameterValues(sqlCommand, courseGroupDataRow, setPrimaryKeyValue: true); // Execute the SQL command. await sqlCommand.ExecuteNonQueryAsync(); } }
public void Update_ShouldThrowException_GivenInvalidCourseScheduleID() { // Insert the first master data rows in the database. int firstCourseScheduleID = CourseScheduleTestTable.InsertPlaceholder(); // Insert the second master data rows in the database. int secondCourseScheduleID = CourseScheduleTestTable.InsertPlaceholder(); // Insert the CourseGroup data row in the database. int courseGroupID = CourseGroupTestTable.InsertWithValues( "5s1cgndj6e5x0uvz", firstCourseScheduleID, 1); // Build the CourseGroup data row. CourseGroupDataRow courseGroupDataRow = new CourseGroupDataRow(); courseGroupDataRow.CourseGroupID = courseGroupID; courseGroupDataRow.CourseGroupCode = "78zcn25ynkaz50ef"; courseGroupDataRow.CourseScheduleID = -1; courseGroupDataRow.PlacesCount = 2; // Build the database connection. using (DatabaseConnection databaseConnection = new DatabaseConnection(TestDatabase.ConnectionString)) { // Open the database connection. databaseConnection.Open().Wait(); try { // Update the CourseGroup data row. CourseGroupDataAccessComponent courseGroupDataAccessComponent = new CourseGroupDataAccessComponent(); courseGroupDataAccessComponent.Update(databaseConnection, courseGroupDataRow).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 Update_ShouldSucceed() { // Insert the first master data rows in the database. int firstCourseScheduleID = CourseScheduleTestTable.InsertPlaceholder(); // Insert the second master data rows in the database. int secondCourseScheduleID = CourseScheduleTestTable.InsertPlaceholder(); // Insert the CourseGroup data row in the database. int courseGroupID = CourseGroupTestTable.InsertWithValues( "5s1cgndj6e5x0uvz", firstCourseScheduleID, 1); // Build the CourseGroup data row. CourseGroupDataRow courseGroupDataRow = new CourseGroupDataRow(); courseGroupDataRow.CourseGroupID = courseGroupID; courseGroupDataRow.CourseGroupCode = "78zcn25ynkaz50ef"; courseGroupDataRow.CourseScheduleID = secondCourseScheduleID; courseGroupDataRow.PlacesCount = 2; // Build the database connection. using (DatabaseConnection databaseConnection = new DatabaseConnection(TestDatabase.ConnectionString)) { // Open the database connection. databaseConnection.Open().Wait(); // Update the CourseGroup data row. CourseGroupDataAccessComponent courseGroupDataAccessComponent = new CourseGroupDataAccessComponent(); courseGroupDataAccessComponent.Update(databaseConnection, courseGroupDataRow).Wait(); } // Validate the CourseGroup data row was updated in the database. CourseGroupTestTable.AssertPresence( courseGroupID, "78zcn25ynkaz50ef", secondCourseScheduleID, 2); }