Bambit.TestUtility.DatabaseTools 1.3.3

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Bambit.TestUtility.DatabaseTools --version 1.3.3
                    
NuGet\Install-Package Bambit.TestUtility.DatabaseTools -Version 1.3.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bambit.TestUtility.DatabaseTools" Version="1.3.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bambit.TestUtility.DatabaseTools" Version="1.3.3" />
                    
Directory.Packages.props
<PackageReference Include="Bambit.TestUtility.DatabaseTools" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bambit.TestUtility.DatabaseTools --version 1.3.3
                    
#r "nuget: Bambit.TestUtility.DatabaseTools, 1.3.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bambit.TestUtility.DatabaseTools@1.3.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bambit.TestUtility.DatabaseTools&version=1.3.3
                    
Install as a Cake Addin
#tool nuget:?package=Bambit.TestUtility.DatabaseTools&version=1.3.3
                    
Install as a Cake Tool

Bambit.TestUtility.DatabaseTools

Provides a set of tools for interfacing with a database for test purposes. Includes class generation from tables and verifying data.

Getting started

Install:

PM> Install-Package Bambit.TestUtility.DatabaseTools

Usage

For the following examples, we are going to use a SqlServer connection, with a database named TestDb We'll use a single table defined as:

create table dbo.[SampleTest](
   [ID] int not null,
   [Name] varchar(255) not null,
   [DateOfBirth] Date not null

)

We also will assume the following appSettings.json config:

{
  "databaseFactory": {
   
    "databaseCatalogRecordMap": {
      "sqlServer": "Bambit.TestUtility.DatabaseTools.SqlServer.SqlServerDatabaseCatalogRecord,  Bambit.TestUtility.DatabaseTools.SqlServer"
    },
    "mappedDatabases": {
      "Test1": {
        "connectionString": "Server=localhost\\MMXXII; Database=IntegrationTests;Trusted_Connection=true",
        "databaseCatalog": "SqlServer"
      }
    },
  },
},

In the above json, the databaseCatalogRecordMap section defines a Dictionary of Type definitions for any type of database needed. There are in separate libraries (such as Bambit.TestUtility.DatabaseTools.SqlServer which implements methods to access a SqlServer database).

The mappedDatabases section contains a dictionary of MappedDatabase values. The key of this dictionary will be used as the ConnectionName in calls to ITestDatabaseFactory methods. The connecitonString is the string that will be used to instantiate the connection, while the databaseCatalog is the lookup value in the databaseCatalogRecordMap

ITestDatabaseFactory

The ITestDatabaseFactory provides access to configured ITestDbConnections and IDatabaseCatalogRecords, along with shortcuts to various methods on a given ITestDbConnection. The concrete implemenation, TestDbConnection, requires a TestDatabaseFactoryOptions injected.

// Using IBuilder and values from a config

IConfiguration configuration=new ConfigurationBuilder()
                .AddJsonFile("appSettings.json")
                .Build();
 TestDatabaseFactoryOptions options = Configuration.GetSection("databaseFactory").Get<TestDatabaseFactoryOptions>()!;
ITestDatabaseFactory factory=new TestDatabaseFactory(options);

TableToClassBuilder

The TableToClassBuilder requires an ITestDatabaseFactory to be injected.


TableToClassBuilder classBuilder = new(factory);
         


From here, you can generate Types or objects:

dynamic dynamicOption = classBuilder.GenerateObjectFromTable("Test1", "dbo", "SampleTest");
// It will have 3 fields:
dynamicOption.ID = 123;
dynamicOption.Name = "Mr. Hyde"
dynamicOption.DateOfBirth =null;

// The returned element is always a DatabaseMappedClass
DatabaseMappedClass mappedClass = classBuilder.GenerateObjectFromTable("Test1", "dbo", "SampleTest");

// This exposes a few methods:

// We can assign the values from a dictionary
Dictionary<string, object?> values = new {
   {"ID", 123},
   {"Name", "Mr. Hyde"},
   {"DateOfBirth", new Date(2000,1,1)},
};

// We can get a property
Assert.AreEqual(mappedClass.Get<int>("ID"), 123);

IDatabaseCatalogRecord

IDatabaseCatalogRecord imeplementations provide methods to generate ITestDbConnection connections, along with some fields that are used in generating DatabaseMappedClass instances. Implementation of this class are left for other packages as they are database specific.

ITestDbConnection

The ITestDbConnection imeplementations are wrappers around specific IDbConnection objects. They expose some helper methods such as:

  • CompareTableToDataset: Compares a collecton of object arrays to values in a database table
  • Persist: Saves a DatabaseMappedClass to the database
  • TrackInfoMessages: Enables tracking (where possible) or messages from the connection (such as print statements)
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Bambit.TestUtility.DatabaseTools:

Package Downloads
Bambit.TestUtility.DatabaseTools.SqlServer

Provides quick random data generation routines to generate or populate random objects.

Bambit.TestUtility.DatabaseTools.SpecFlow

Step library for testing against databases.

Bambit.TestUtility.DatabaseTools.Postgres

Provides implementation of the Bambit.TestUtility.DatabaseTools for a PostgreSql database.

Bambit.TestUtility.DatabaseTools.Reqnroll

Step library for testing against databases.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.5.2 290 2/19/2025
1.5.1 141 2/18/2025
1.5.0 161 2/18/2025
1.4.4 283 1/8/2025
1.4.1 137 12/10/2024
1.4.0 126 12/10/2024
1.3.5 144 12/3/2024
1.3.3 140 11/26/2024
1.3.2 143 11/26/2024
1.3.1 134 11/26/2024
1.3.0 141 11/26/2024
1.2.4 141 11/18/2024
1.2.1 169 8/20/2024
1.2.0 156 8/19/2024
1.1.0 120 7/30/2024
1.0.2-alpha.0.22 66 7/30/2024
1.0.2-alpha.0.16 56 7/29/2024