From 7d2a3cafccd2ad4c458dd6cf0d7a38ea4c923207 Mon Sep 17 00:00:00 2001 From: Gilles TOURREAU Date: Tue, 27 Aug 2024 13:23:53 +0200 Subject: [PATCH] Add the XML comments. --- ...yFrameworkDatabaseInitializerExtensions.cs | 17 +++++++-- ...Databases.SqlServer.EntityFramework.csproj | 1 + .../Comparer/SqlDatabaseComparisonResults.cs | 7 ++++ .../Comparer/SqlDatabaseObjectDifference.cs | 15 ++++++++ .../SqlDatabaseObjectDifferenceType.cs | 12 ++++++ .../Comparer/SqlDatabaseObjectDifferences.cs | 10 +++++ .../Comparer/SqlServerDatabaseComparer.cs | 13 +++++-- .../SqlServerDatabaseExtensions.cs | 38 ++++++++++++++++--- .../SqlServerDatabaseInitializer.cs | 24 ++++++++++-- .../UnitTests.Databases.SqlServer.csproj | 1 + 10 files changed, 121 insertions(+), 17 deletions(-) diff --git a/src/UnitTests.Databases.SqlServer.EntityFramework/EntityFrameworkDatabaseInitializerExtensions.cs b/src/UnitTests.Databases.SqlServer.EntityFramework/EntityFrameworkDatabaseInitializerExtensions.cs index dfa846e..4254ce2 100644 --- a/src/UnitTests.Databases.SqlServer.EntityFramework/EntityFrameworkDatabaseInitializerExtensions.cs +++ b/src/UnitTests.Databases.SqlServer.EntityFramework/EntityFrameworkDatabaseInitializerExtensions.cs @@ -9,10 +9,19 @@ namespace PosInformatique.UnitTests.Databases.SqlServer using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; + /// + /// Contains extensions methods on the to initialize + /// database using Entity Framework . + /// public static class EntityFrameworkDatabaseInitializerExtensions { - public static SqlServerDatabase Initialize(this SqlServerDatabaseInitializer initializer, TContext context) - where TContext : DbContext + /// + /// Initialize a SQL Server database from a specified in the argument. + /// + /// which the initialization will be perform on. + /// Instance of the which represents the database schema to initialize. + /// An instance of the which allows to perform query to initialize the data. + public static SqlServerDatabase Initialize(this SqlServerDatabaseInitializer initializer, DbContext context) { var connectionString = context.Database.GetConnectionString(); @@ -21,7 +30,7 @@ public static SqlServerDatabase Initialize(this SqlServerDatabaseIniti var server = new SqlServer(connectionString!); var database = server.GetDatabase(connectionStringBuilder.InitialCatalog); - if (!initializer.IsDeployed) + if (!initializer.IsInitialized) { server.DeleteDatabase(connectionStringBuilder.InitialCatalog); @@ -34,7 +43,7 @@ IF SUSER_ID ('{connectionStringBuilder.UserID}') IS NULL CREATE LOGIN [{connectionStringBuilder.UserID}] WITH PASSWORD = '{connectionStringBuilder.Password}'"); } - initializer.IsDeployed = true; + initializer.IsInitialized = true; } database.ClearAllData(); diff --git a/src/UnitTests.Databases.SqlServer.EntityFramework/UnitTests.Databases.SqlServer.EntityFramework.csproj b/src/UnitTests.Databases.SqlServer.EntityFramework/UnitTests.Databases.SqlServer.EntityFramework.csproj index 89a20ca..d9d0f2f 100644 --- a/src/UnitTests.Databases.SqlServer.EntityFramework/UnitTests.Databases.SqlServer.EntityFramework.csproj +++ b/src/UnitTests.Databases.SqlServer.EntityFramework/UnitTests.Databases.SqlServer.EntityFramework.csproj @@ -2,6 +2,7 @@ net8.0 + True diff --git a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseComparisonResults.cs b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseComparisonResults.cs index f27c55c..7b0a46d 100644 --- a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseComparisonResults.cs +++ b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseComparisonResults.cs @@ -9,6 +9,9 @@ namespace PosInformatique.UnitTests.Databases.SqlServer using System.Collections.ObjectModel; using System.Text; + /// + /// Represents the differences between 2 databases. + /// public class SqlDatabaseComparisonResults { internal SqlDatabaseComparisonResults(IList objects) @@ -16,8 +19,12 @@ internal SqlDatabaseComparisonResults(IList object this.Objects = new ReadOnlyCollection(objects); } + /// + /// Gets the list of the database objects differences found. + /// public ReadOnlyCollection Objects { get; } + /// public override string ToString() { var stringBuilder = new StringBuilder(); diff --git a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifference.cs b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifference.cs index 7242d24..aa730c7 100644 --- a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifference.cs +++ b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifference.cs @@ -9,6 +9,9 @@ namespace PosInformatique.UnitTests.Databases.SqlServer using System.Data; using System.Text; + /// + /// Represents a difference between 2 database objects. + /// public class SqlDatabaseObjectDifference { private readonly object[] keyValue; @@ -21,12 +24,24 @@ internal SqlDatabaseObjectDifference(DataRow? source, DataRow? target, SqlDataba this.keyValue = keyValue; } + /// + /// Gets schema information of the source database. If it is mean + /// that the object exists in the target database but does not exists in the source database. + /// public DataRow? Source { get; } + /// + /// Gets schema information of the target database. If it is mean + /// that the object exists in the source database but does not exists in the target database. + /// public DataRow? Target { get; } + /// + /// Gets the of the difference between the and the . + /// public SqlDatabaseObjectDifferenceType Type { get; } + /// public override string ToString() { var keyValueString = string.Join(".", this.keyValue); diff --git a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferenceType.cs b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferenceType.cs index 52dd02a..8609d08 100644 --- a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferenceType.cs +++ b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferenceType.cs @@ -6,12 +6,24 @@ namespace PosInformatique.UnitTests.Databases.SqlServer { + /// + /// Indicate the difference type between and . + /// public enum SqlDatabaseObjectDifferenceType { + /// + /// Indicates that an object exists in the source database but does not exists in the target database. + /// MissingInTarget, + /// + /// Indicates that an object exists in the target database but does not exists in the source database. + /// MissingInSource, + /// + /// Indicates that the object exists in the two databases but have some different properties. + /// Different, } } diff --git a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferences.cs b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferences.cs index 558bedd..b0d50b7 100644 --- a/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferences.cs +++ b/src/UnitTests.Databases.SqlServer/Comparer/SqlDatabaseObjectDifferences.cs @@ -8,6 +8,9 @@ namespace PosInformatique.UnitTests.Databases.SqlServer { using System.Collections.ObjectModel; + /// + /// Represents a list of the difference for database object type. + /// public sealed class SqlDatabaseObjectDifferences { internal SqlDatabaseObjectDifferences(string type, IList differences) @@ -16,10 +19,17 @@ internal SqlDatabaseObjectDifferences(string type, IList + /// Gets the list of the object differences between the two databases. + /// public ReadOnlyCollection Differences { get; } + /// + /// Gets the type of the object. + /// public string Type { get; } + /// public override string ToString() { return this.Type; diff --git a/src/UnitTests.Databases.SqlServer/Comparer/SqlServerDatabaseComparer.cs b/src/UnitTests.Databases.SqlServer/Comparer/SqlServerDatabaseComparer.cs index 003869a..01bf7ba 100644 --- a/src/UnitTests.Databases.SqlServer/Comparer/SqlServerDatabaseComparer.cs +++ b/src/UnitTests.Databases.SqlServer/Comparer/SqlServerDatabaseComparer.cs @@ -6,6 +6,9 @@ namespace PosInformatique.UnitTests.Databases.SqlServer { + /// + /// Allows to compare schema difference between 2 databases. + /// public class SqlServerDatabaseComparer { private static readonly SqlServerObjectComparer[] Comparers = @@ -22,10 +25,12 @@ public class SqlServerDatabaseComparer new SqlServerViewsComparer(), ]; - public SqlServerDatabaseComparer() - { - } - + /// + /// Compares two database and returns the differences found. + /// + /// First database to compare with . + /// Second database to compare with . + /// The difference between the two databases. public SqlDatabaseComparisonResults Compare(SqlServerDatabase source, SqlServerDatabase target) { var differences = new List(); diff --git a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs index ebe4c20..a587808 100644 --- a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs +++ b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs @@ -8,8 +8,10 @@ namespace PosInformatique.UnitTests.Databases.SqlServer { using System.Globalization; using System.Text; - using Microsoft.Data.SqlClient; + /// + /// Contains extensions methods on the class. + /// public static class SqlServerDatabaseExtensions { private const string Tab = " "; @@ -24,11 +26,33 @@ public static class SqlServerDatabaseExtensions typeof(decimal?), ]; + /// + /// Insert data into the table specified by the argument. The row + /// to insert are represents by objects (or anonymous objects) which the property name must match the + /// the column name. + /// + /// Type of the object which contains the data to insert in the table. + /// SQL Server database which contains the table where the data will be inserted. + /// SQL table where the data will be inserted. + /// Set of object which represents the row to insert. Each object must have property which are mapped to the column to insert. + /// The number of the rows inserted. public static int InsertInto(this SqlServerDatabase database, string tableName, params T[] objects) { return InsertInto(database, tableName, false, objects); } + /// + /// Insert data into the table specified by the argument. The row + /// to insert are represents by objects (or anonymous objects) which the property name must match the + /// the column name. + /// + /// Type of the object which contains the data to insert in the table. + /// SQL Server database which contains the table where the data will be inserted. + /// SQL table where the data will be inserted. + /// to disable auto incrementation of the IDENTITY column. In this case, the object must contains explicitely the value of the IDENTITY + /// column to insert. + /// Set of object which represents the row to insert. Each object must have property which are mapped to the column to insert. + /// The number of the rows inserted. public static int InsertInto(this SqlServerDatabase database, string tableName, bool disableIdentityInsert, params T[] objects) { var builder = new SqlInsertStatementBuilder(tableName); @@ -70,6 +94,10 @@ Type t when Array.Exists(AuthorizedNonStringTypes, at => at == t) => builder.Add return database.ExecuteNonQuery(statement); } + /// + /// Clear all in the database. + /// + /// SQL Server database which the data have to be deleted. public static void ClearAllData(this SqlServerDatabase database) { database.ExecuteNonQuery("EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'"); @@ -113,15 +141,15 @@ private sealed class SqlInsertStatementBuilder public SqlInsertStatementBuilder(string tableName) { this.tableName = tableName; - this.columns = new(); - this.currentRecord = new(); - this.records = new(); + this.columns = []; + this.currentRecord = []; + this.records = []; } public SqlInsertStatementBuilder NewRecord() { this.records.Add(this.currentRecord); - this.currentRecord = new(); + this.currentRecord = []; return this; } diff --git a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs index 5c9999d..b56b72c 100644 --- a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs +++ b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs @@ -8,10 +8,26 @@ namespace PosInformatique.UnitTests.Databases.SqlServer { using Microsoft.Data.SqlClient; + /// + /// Initializer used to initialize the database for the unit tests. + /// Call the method to initialize a database from + /// a DACPAC file. + /// + /// The database will be created the call of the method. For the next calls + /// the database is preserved but all the data are deleted. public class SqlServerDatabaseInitializer { - public bool IsDeployed { get; set; } - + /// + /// Gets or sets a value indicating whether if the database has been initialized. + /// + public bool IsInitialized { get; set; } + + /// + /// Initialize a SQL Server database from a DACPAC file. + /// + /// Full path of the DACPAC file. + /// Connection string to the SQL Server with administrator rights. + /// An instance of the which allows to perform query to initialize the data. public SqlServerDatabase Initialize(string packageName, string connectionString) { var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString); @@ -20,7 +36,7 @@ public SqlServerDatabase Initialize(string packageName, string connectionString) SqlServerDatabase database; - if (!this.IsDeployed) + if (!this.IsInitialized) { if (!string.IsNullOrWhiteSpace(connectionStringBuilder.UserID)) { @@ -31,7 +47,7 @@ IF SUSER_ID ('{connectionStringBuilder.UserID}') IS NULL database = server.DeployDacPackage(packageName, connectionStringBuilder.InitialCatalog); - this.IsDeployed = true; + this.IsInitialized = true; } else { diff --git a/src/UnitTests.Databases.SqlServer/UnitTests.Databases.SqlServer.csproj b/src/UnitTests.Databases.SqlServer/UnitTests.Databases.SqlServer.csproj index 1cbd8fa..b8017f1 100644 --- a/src/UnitTests.Databases.SqlServer/UnitTests.Databases.SqlServer.csproj +++ b/src/UnitTests.Databases.SqlServer/UnitTests.Databases.SqlServer.csproj @@ -2,6 +2,7 @@ net8.0 + True