Akov.DataGenerator
1.4.0
See the version list below for details.
dotnet add package Akov.DataGenerator --version 1.4.0
NuGet\Install-Package Akov.DataGenerator -Version 1.4.0
<PackageReference Include="Akov.DataGenerator" Version="1.4.0" />
<PackageVersion Include="Akov.DataGenerator" Version="1.4.0" />
<PackageReference Include="Akov.DataGenerator" />
paket add Akov.DataGenerator --version 1.4.0
#r "nuget: Akov.DataGenerator, 1.4.0"
#:package Akov.DataGenerator@1.4.0
#addin nuget:?package=Akov.DataGenerator&version=1.4.0
#tool nuget:?package=Akov.DataGenerator&version=1.4.0
DataGenerator
Generates data randomly. The input must contain the definition for objects and properties, so the DataProcessor will know the expectations. Either a special json, which will be described below, or any class using the Akov.DataGenerator.Attributes namespace may be considered to be the input. The output will be in the json format. Fluent support as an alternative to attributes added since version 1.4.0.
Important:
- Versions 1.0 and 1.1 are not supported and not recommended to use.
- Version 1.2 arrays of primitive types are not supported. Fixed in 1.3.
- Version 1.3 mapping of type
TontoDataSchemebased on attributes fromAkov.DataGenerator.Attributesadded. - Version 1.3.1 min array size support added, range validation for
DgLength,DgSpacesCount,DgFailureadded on attributes level. - Version 1.4.0 fluent support added.
Author's blog
Data Generator - a quick introduction.
Custom Data Generator - creating a custom generator based on the library.
Calculated Properties with Data Generator - calculated properties and files as custom data sources.
Data Generator Attributes - using the program approach.
Integration Testing with Data Generator - shows how to mock and test solutions with the generated data.
Fluent support profile example - an alternative to using attributes.
Assemblies description
Akov.DataGenerator the library itself. The nuget package is available via the link above.
Akov.DataGenerator.Demo demonstrates how to mock an http client with generated data, and then use it in tests.
DataScheme
root points to the entry definition.
definitions determines objects.
Definition
name stands for the object name.
properties the object properties.
Properties
type the property type.
Primitive types:
stringthe string.setone of the list of values.guidthe guid.boolTrue or False.intthe integer.doublethe double.datetimethe datatime.
Complex types:
objectthe object from definition.arraythe array.filethe file of values that will be transformed to thesetprimitive type.calcthe calculated property. Having it means that a custom generator, specifying the property behaviour, and derived from theCalcGeneratorBaseshould be added to theGeneratorFactory.
pattern
- for
stringdefines all possible characters, e.g. "abcdefghABCFEDGH". Spaces will be added additionally. - for
setdefines all possible values separated by comma by default. - for
double,guid,datetimespecifies the output format, e.g. "0.00", "yyyy-MM-dd" etc. - for
objectpoints to the definition name. - for
arraypoints to the definition name if it represents the object type, and to a primitive type otherwise. - for
filespecifies the path to an existing file. The data are separated by comma by default.
subpattern is a primitive type pattern used only within arrays of primitive types.
"name": "prices",
"type": "array", // <- should be array
"pattern": "double" , // <- the pattern keyword specifies the type of array
"subpattern": "0.00", // <- the pattern for the double primitive type
sequenceSeparator the separator for the set and file.
minLength the minimum output data length for string, the min size for array.
maxLength the maximum output data length for string, the max size for array.
minSpaceCount the minimum count of spaces in the string.
maxSpaceCount the maximum count of spaces in the string.
minValue the minimum value for int, double and datetime.
maxValue the maximum value for int, double and datetime.
failure means inconsistent data appears with the specified probability.
nullablethe probability that null appears.customthe probability that the invalid value appears.rangethe probability that the value will be out of range. Forstringthat means that the string length will be out of the specified interval.
customFailure specifies the value that will appear for the custom failure case.
Example of an input json
Akov.DataGenerator.Attributes
Determine how data should be generated based on an existing class in code.
DgCalc considered the property to be calculated.
DgCustomFailure specifies the value that will appear for the custom failure case.
DgFailure specifies probabilities of the different failure types.
DgIgnore ignores the property while data generation.
DgLength specifies the length of the string. The Max value is also used for the array size.
DgName specifies the name for the property while generation. Makes sense if it should be different from the class property name.
DgPattern specifies the pattern. Unlike the generation process from a json, there is no need to have an additional subpattern attribute for primitive arrays. As the type of array will be determined by the class property type.
DgRange specifies the range of values for the property.
DgSequenceSeparator specifies the output separator if the property type is enum, and the input one if the pattern points to a file of values.
DgSource specifies the file of values for the property. Not supported for objects, arrays and enums.
DgSpaceCount specifies the count of spaces in the string.
Fluent profiles
A profile should derive from DgProfileBase.
ForType() - registers a type in the profile. All the subtypes should be registered as well.
Ignore(Expression) - excludes a property from data generation.
Property(Expression) - points to a property for which the generation rules should be setup. If a property is not ignored and skipped in the profile, then the defaults will be applied to it.
PropertyBuilder - contains the list of methods which work similar to the attribute approach.
Code examples
A custom generator for a calculated property
public class StudentCalcGenerator : CalcGeneratorBase
{
protected override object CreateImpl(CalcPropertyObject propertyObject)
{
if (string.Equals(propertyObject.Property.Name, "fullname", StringComparison.OrdinalIgnoreCase))
{
var val1 = propertyObject.Values
.Single(v => String.Equals(v.Name, "firstname", StringComparison.OrdinalIgnoreCase));
var val2 = propertyObject.Values
.Single(v => String.Equals(v.Name, "lastname", StringComparison.OrdinalIgnoreCase));
return $"{val1.Value} {val2.Value}";
}
throw new NotSupportedException($"Unexpected calculated property {propertyObject.Property.Name}");
}
protected override object CreateRangeFailureImpl(CalcPropertyObject propertyObject)
{
throw new NotSupportedException($"Range failure not supported for {propertyObject.Property.Name}");
}
}
Extending the existing generator factory
public class StudentGeneratorFactory : GeneratorFactory
{
public override Dictionary<string, GeneratorBase> GetGeneratorDictionary()
{
Dictionary<string, GeneratorBase> generators = base.GetGeneratorDictionary();
generators.Add(TemplateType.Calc, new StudentCalcGenerator());
return generators;
}
}
var dg = new DG(new StudentGeneratorFactory());
A class representing the data generation
public class DgStudent
{
[DgFailure(NullProbability = 0.2)]
public Guid Id { get; set; }
[DgSource("firstnames.txt")]
[DgFailure(NullProbability = 0.1)]
public string? FirstName { get; set; }
[DgSource("lastnames.txt")]
[DgFailure(NullProbability = 0.1)]
public string? LastName { get; set; }
[DgCalc] //supposed to be calculated
public string? FullName { get; set; }
[DgName("test_variant")] //Variant represents enum here
public Variant Variant { get; set; }
[DgName("test_answers")]
[DgRange(Min = 1, Max = 5)]
[DgLength(Max = 5)]
public int[]? TestAnswers { get; set; }
[DgName("encoded_solution")]
[DgPattern("abcdefghijklmnopqrstuvwxyz0123456789")]
[DgLength(Min = 15, Max = 50)]
[DgSpacesCount(Min = 1, Max = 3)]
[DgFailure(
NullProbability = 0.1,
CustomProbability = 0.1,
OutOfRangeProbability = 0.05)]
[DgCustomFailure("####-####-####")]
public string? EncodedSolution { get; set; }
[DgName("last_updated")]
[DgPattern("dd/MM/yy")]
[DgRange(Min = "20/10/19", Max = "01/01/20")]
[DgFailure(
NullProbability = 0.2,
CustomProbability = 0.2,
OutOfRangeProbability = 0.1)]
public DateTime? LastUpdated { get; set; }
//DgSubject represents a similar class
public DgSubject? Subject { get; set; }
//Collections are equal to arrays in the data generation process
public List<DgSubject>? Subjects { get; set; }
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- Newtonsoft.Json (>= 12.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 2.2.0 | 210 | 12/4/2025 | |
| 2.1.4 | 671 | 3/11/2025 | |
| 2.1.3 | 327 | 3/10/2025 | |
| 2.1.2 | 293 | 3/10/2025 | |
| 2.1.1 | 339 | 3/5/2025 | |
| 2.1.0 | 327 | 3/5/2025 | |
| 2.0.0 | 317 | 3/5/2025 | |
| 2.0.0-alpha.1 | 217 | 3/5/2025 | |
| 1.11.0 | 232 | 2/27/2025 | |
| 1.10.0 | 593 | 11/14/2023 | |
| 1.9.1 | 868 | 1/11/2023 | |
| 1.9.0 | 838 | 1/8/2023 | |
| 1.8.2 | 817 | 12/18/2022 | |
| 1.8.1 | 810 | 12/14/2022 | |
| 1.8.0 | 848 | 12/7/2022 | |
| 1.7.1 | 826 | 12/5/2022 | |
| 1.7.0 | 848 | 12/5/2022 | |
| 1.6.4 | 869 | 11/30/2022 | |
| 1.6.2 | 865 | 11/23/2022 | |
| 1.6.1 | 911 | 11/23/2022 | |
| 1.6.0 | 853 | 11/21/2022 | |
| 1.5.2 | 868 | 11/20/2022 | |
| 1.5.1 | 835 | 11/19/2022 | |
| 1.5.0 | 862 | 11/18/2022 | |
| 1.4.1 | 865 | 11/16/2022 | |
| 1.4.0 | 956 | 7/31/2022 | |
| 1.3.1 | 1,120 | 9/24/2020 | |
| 1.3.0 | 1,071 | 9/7/2020 | |
| 1.2.0 | 1,326 | 8/31/2020 |