-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from IowaComputerGurus/feature/net7
Feature/net7
- Loading branch information
Showing
4 changed files
with
111 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 14 additions & 6 deletions
20
src/NetCore.Utilities.UnitTesting/AbstractDataServiceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 93 additions & 94 deletions
187
src/NetCore.Utilities.UnitTesting/SampleDataGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |