DataSuit
DataSuit copied to clipboard
netstandard2.0 random data generator
Overview
Data Suit is a random data generator. It generates data for primitive data types and POCO classes. At the beginning, it was an experimental project for several purposes. Later, I changed it into a formal format. It is designed with SOLID principles. Mapping fields is also supported via fluent API.
Basis of the API is shown below. For more detailed examples, you can see at Samples
Content
Build and nuget
.NET Standard library of data suit.
| Build Status (travis-ci) | DataSuit Nuget | DataSuit.AspNetCore Nuget |
|---|---|---|
Usage
Suit Class
Suit class is necessary for every operation.
Suit suit = new Suit();
DataSuit comes with built-in data. If you want to enable it, you have to call:
suit.Load();
You can see the built-in data fields on BuiltIn.md file.
POCO example
For example POCO class data generation
var personGenerator = suit.GeneratorOf<Person>();
var listOfPersons = personGenerator.Generate(count: 10);
It generates 10 person classes with respect to properties of it.
var person = personGenerator.Generate();
It generates a Person class
Primitive example
var primitiveGenerator = suit.GeneratorOfPrimitives();
var names = primitiveGenerator.String("FirstName", count: 5);
foreach (var name in names)
Console.WriteLine($"Name:{name}");
It generates 5 names as string list
Customizing
DataSuit API supports customizing very well. A fluent API design welcomes us here.
ISettings settings = new Settings();
Suit suit = new Suit(settings);
var barList = new List<string>() { "Foo", "Bar", "Baz" };
suit.Build<Foo>()
.Collection(i => i.Bar, barList, ProviderType.Random)
.Range(i => i.Range, 10, 40)
.Set(i => i.Static, "DataSuit");
var fooGenerator = suit.GeneratorOf<Foo>();
var data = fooGenerator.Generate(count: 4);
foreach (var item in data)
Console.WriteLine($"{item.Bar} {item.Range} {item.Static}");
Result of the foo classes
Bar 19 DataSuit
Foo 23 DataSuit
Baz 11 DataSuit
Baz 33 DataSuit
Mapping
Collection
suit.Build<T>()
.Collection(i => i.Field, list, ProviderType.Sequential)
Static Variable
suit.Build<T>()
.Set(i => i.Field, Variable)
Range
It requires integer or double range values
suit.Build<T>()
.Range(i => i.FieldInteger, 10, 20)
.Range(i => i.FieldDouble, 10.5, 20.3)
Dummy
It gives lorem ipsum text data with given length
suit.Build<T>()
.Dummy(i => i.Field, 300)
Incremental
It generates integer or long values by sequential order. Such as IDs.
suit.Build<T>()
.Incremental(i => i.Id)
Func
It does run a function for every MoveNext event.
suit.Build<T>()
.Func(i => i.Id, () => Guid.NewGuid().ToString())
Guid
suit.Build<T>()
.Guid(i => i.Id)
Enum
It will give select enums as random. It uses ICollectionProvider with Random provider type.
suit.Build<T>()
.Enum(i => i.EnumData)
Also, you can use Build method without generic type.
Non-generic type
suit.Build()
.Range("Salary", 3000, 5000)
.Incremental("id");
Import/Export
Following code, export settings of the current suit as JSON string.
suit.Export();
Note that: FuncProvider can't be exported. Therefore, you have to re-define the Func providers when you are importing them to a suit.
Following code, import settings with the given JSON string.
suit.Import(data);
You can see an example setting JSON file from here
AspNetCore
You can get the package DataSuit.AspNetCore from nuget.
Just you need to add DataSuit to Startup.cs on ConfigureServices method.
services.AddDataSuit();
Also, you can customize the API via
services.AddDataSuit(i =>
{
i.DefaultData = false; // disable built-in data
i.Build()
.Range("Salary", 3000, 5000)
.Incremental("id");
i.Ready();
});
At the controller, you can inject IGenerator<T>
private readonly IGenerator<PersonViewModel> _personGenerator;
public HomeController(IGenerator<Models.PersonViewModel> personGenerator)
{
_personGenerator = personGenerator;
}
public IActionResult GetPersons()
{
return Json(_personGenerator.Generate(count: 5));
}
TestSetup
This attribute speeding up the process of unit testing.
[TestSetup(typeof(TestSetupExample))]
public void Example()
{
var suit = DataSuitRunner.GetSuit();
var data = suit.GeneratorOfPrimitives().String("Singer");
Assert.Equal("Eminem", data);
}
TestSetupExample class is a setup class for this unit test. It is derived from IAttributeSuit interface.
TestSetupExample.cs
public class TestSetupExample : ISetupSuit
{
public TestSetupExample()
{
Suit = new Suit();
Suit.Build()
.Set("Singer", "Eminem");
Suit.EnsureNoPendingProviders();
}
public Suit Suit { get; }
}