Skip to content

Commit

Permalink
Merge pull request #17 from IowaComputerGurus/feature/net7
Browse files Browse the repository at this point in the history
Feature/net7
  • Loading branch information
mitchelsellers committed Mar 9, 2023
2 parents 1c68f7c + 6c12b35 commit 64a960e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 111 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

![](https://img.shields.io/nuget/v/icg.netcore.utilities.unittesting.svg) ![](https://img.shields.io/nuget/dt/icg.netcore.utilities.unittesting.svg)

[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.unittesting&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=IowaComputerGurus_netcore.utilities.unittesting)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.unittesting&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=IowaComputerGurus_netcore.utilities.unittesting)
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.unittesting&metric=bugs)](https://sonarcloud.io/summary/new_code?id=IowaComputerGurus_netcore.utilities.unittesting)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.unittesting&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=IowaComputerGurus_netcore.utilities.unittesting)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.unittesting&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=IowaComputerGurus_netcore.utilities.unittesting)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.unittesting&metric=coverage)](https://sonarcloud.io/summary/new_code?id=IowaComputerGurus_netcore.utilities.unittesting)

This library provides helpful items to speed the development of unit tests across all .NET Core project types. We will update this library regularly with helpful base classes/implementations.

## Using ICG.NetCore.Utilities.UnitTesting
Expand Down
20 changes: 14 additions & 6 deletions src/NetCore.Utilities.UnitTesting/AbstractDataServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using Microsoft.EntityFrameworkCore;

namespace ICG.NetCore.Utilities.UnitTesting
namespace ICG.NetCore.Utilities.UnitTesting;

/// <summary>
/// An abstract class that can be utilized as a base for unit testing where you need to instantiate a Memory DB Context
/// </summary>
public abstract class AbstractDataServiceTest
{
public abstract class AbstractDataServiceTest
/// <summary>
/// Creates a set of DB Context Options for in memory usage with the specified name
/// </summary>
/// <typeparam name="T">The DbContext Type</typeparam>
/// <param name="dbName">The targeted name for the in-memory database</param>
/// <returns></returns>
public DbContextOptions<T> BuildMemoryDbOptions<T>(string dbName) where T : DbContext
{
public DbContextOptions<T> BuildMemoryDbOptions<T>(string dbName) where T : DbContext
{
return new DbContextOptionsBuilder<T>().UseInMemoryDatabase(dbName).Options;
}
return new DbContextOptionsBuilder<T>().UseInMemoryDatabase(dbName).Options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
<None Include="icgAppIcon.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Respawn" Version="5.0.1" />
<PackageReference Include="Xunit" Version="2.4.1" />
<PackageReference Include="Xunit" Version="2.4.2" />
</ItemGroup>

</Project>
187 changes: 93 additions & 94 deletions src/NetCore.Utilities.UnitTesting/SampleDataGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,107 +1,106 @@
using System;

namespace ICG.NetCore.Utilities.UnitTesting
namespace ICG.NetCore.Utilities.UnitTesting;

/// <summary>
/// A process that generates sample data for testing purposes
/// </summary>
public interface ISampleDataGenerator
{
/// <summary>
/// Creates a random string with a combination of upper case, lower case, or numbers, including spaces
/// </summary>
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
string RandomAlphaNumericString(int length = 10);

/// <summary>
/// Creates a random string with a combination of upper case and lower case letters with spaces.
/// </summary>
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
string RandomAlphaString(int length = 10);

/// <summary>
/// Creates a random date value
/// </summary>
/// <param name="minDate">Optional minimum date, if omitted defaults to 1/1/1900</param>
/// <param name="maxDate">Optional maximum date, if omitted defaults to today</param>
/// <returns>A random date value</returns>
DateTime RandomDate(DateTime? minDate = null, DateTime? maxDate = null);

/// <summary>
/// Creates a random integer value
/// </summary>
/// <param name="min">Optional minimum integer, if omitted defaults to 0</param>
/// <param name="max">Optional maximum integer, if omitted defaults to <see cref="int.MaxValue" /></param>
/// <returns>The random int value</returns>
int RandomInt(int min = 0, int max = int.MaxValue);
}

/// <summary>
/// A process that generates sample data for testing purposes
/// </summary>
public class SampleDataGenerator : ISampleDataGenerator
{
private readonly Random _random = new();

/// <summary>
/// A process that generates sample data for testing purposes
/// Creates a random string with a combination of upper case, lower case, or numbers, including spaces
/// </summary>
public interface ISampleDataGenerator
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
public string RandomAlphaNumericString(int length = 10)
{
/// <summary>
/// Creates a random string with a combination of upper case, lower case, or numbers, including spaces
/// </summary>
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
string RandomAlphaNumericString(int length = 10);

/// <summary>
/// Creates a random string with a combination of upper case and lower case letters with spaces.
/// </summary>
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
string RandomAlphaString(int length = 10);

/// <summary>
/// Creates a random date value
/// </summary>
/// <param name="minDate">Optional minimum date, if omitted defaults to 1/1/1900</param>
/// <param name="maxDate">Optional maximum date, if omitted defaults to today</param>
/// <returns>A random date value</returns>
DateTime RandomDate(DateTime? minDate = null, DateTime? maxDate = null);

/// <summary>
/// Creates a random integer value
/// </summary>
/// <param name="min">Optional minimum integer, if omitted defaults to 0</param>
/// <param name="max">Optional maximum integer, if omitted defaults to <see cref="int.MaxValue" /></param>
/// <returns>The random int value</returns>
int RandomInt(int min = 0, int max = int.MaxValue);
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var stringChars = new char[length];

for (var i = 0; i < stringChars.Length; i++) stringChars[i] = chars[_random.Next(chars.Length)];

return new string(stringChars);
}

/// <summary>
/// Creates a random string with a combination of upper case and lower case letters with spaces.
/// </summary>
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
public string RandomAlphaString(int length = 10)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
var stringChars = new char[length];

for (var i = 0; i < stringChars.Length; i++) stringChars[i] = chars[_random.Next(chars.Length)];

return new string(stringChars);
}

/// <summary>
/// Creates a random date value
/// </summary>
/// <param name="minDate">Optional minimum date, if omitted defaults to 1/1/1900</param>
/// <param name="maxDate">Optional maximum date, if omitted defaults to today</param>
/// <returns>A random date value</returns>
public DateTime RandomDate(DateTime? minDate = null, DateTime? maxDate = null)
{
var start = new DateTime(1900, 1, 1);
if (minDate.HasValue) start = minDate.Value;

var end = DateTime.Today;
if (maxDate.HasValue) end = maxDate.Value;

var range = (end - start).Days;
return start.AddDays(_random.Next(range));
}

/// <summary>
/// A process that generates sample data for testing purposes
/// Creates a random integer value
/// </summary>
public class SampleDataGenerator : ISampleDataGenerator
/// <param name="min">Optional minimum integer, if omitted defaults to 0</param>
/// <param name="max">Optional maximum integer, if omitted defaults to <see cref="int.MaxValue" /></param>
/// <returns>The random int value</returns>
public int RandomInt(int min = 0, int max = int.MaxValue)
{
private readonly Random _random = new Random();

/// <summary>
/// Creates a random string with a combination of upper case, lower case, or numbers, including spaces
/// </summary>
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
public string RandomAlphaNumericString(int length = 10)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var stringChars = new char[length];

for (var i = 0; i < stringChars.Length; i++) stringChars[i] = chars[_random.Next(chars.Length)];

return new string(stringChars);
}

/// <summary>
/// Creates a random string with a combination of upper case and lower case letters with spaces.
/// </summary>
/// <param name="length">Optional length, defaults to 10</param>
/// <returns>The random string of requested length</returns>
public string RandomAlphaString(int length = 10)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
var stringChars = new char[length];

for (var i = 0; i < stringChars.Length; i++) stringChars[i] = chars[_random.Next(chars.Length)];

return new string(stringChars);
}

/// <summary>
/// Creates a random date value
/// </summary>
/// <param name="minDate">Optional minimum date, if omitted defaults to 1/1/1900</param>
/// <param name="maxDate">Optional maximum date, if omitted defaults to today</param>
/// <returns>A random date value</returns>
public DateTime RandomDate(DateTime? minDate = null, DateTime? maxDate = null)
{
var start = new DateTime(1900, 1, 1);
if (minDate.HasValue) start = minDate.Value;

var end = DateTime.Today;
if (maxDate.HasValue) end = maxDate.Value;

var range = (end - start).Days;
return start.AddDays(_random.Next(range));
}

/// <summary>
/// Creates a random integer value
/// </summary>
/// <param name="min">Optional minimum integer, if omitted defaults to 0</param>
/// <param name="max">Optional maximum integer, if omitted defaults to <see cref="int.MaxValue" /></param>
/// <returns>The random int value</returns>
public int RandomInt(int min = 0, int max = int.MaxValue)
{
return _random.Next(min, max);
}
return _random.Next(min, max);
}
}

0 comments on commit 64a960e

Please sign in to comment.