-
Notifications
You must be signed in to change notification settings - Fork 42
Feature: add fake data module #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Namoshek
wants to merge
6
commits into
elsa-workflows:main
Choose a base branch
from
Namoshek:feature/fakedata-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
319f24a
Create new projects for Elsa.FakeData module
Namoshek bb073f8
Add new activities to generate fake data including tests
Namoshek abeacd7
Add README for fake data module
Namoshek c07e1e3
Add fake data module to workbench projects
Namoshek 6d57981
Produce array as output, not ICollection<T>
Namoshek c773710
Apply suggestions from code review
Namoshek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
74 changes: 74 additions & 0 deletions
74
src/modules/fakedata/Elsa.FakeData/Activities/GenerateFakeDataActivity.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using Bogus; | ||
| using Elsa.Extensions; | ||
| using Elsa.Workflows; | ||
| using Elsa.Workflows.Attributes; | ||
| using Elsa.Workflows.Models; | ||
|
|
||
| namespace Elsa.FakeData.Activities; | ||
|
|
||
| /// <summary> | ||
| /// Provides a base implementation for activities that generate collections of fake data | ||
| /// using the <see href="https://github.com/bchavez/Bogus">Bogus</see> library. | ||
| /// </summary> | ||
| public abstract class GenerateFakeDataActivity<T> : CodeActivity<T[]> where T : class | ||
| { | ||
| /// <inheritdoc /> | ||
| protected GenerateFakeDataActivity(string? source = null, int? line = null) : base(source, line) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The number of fake records to generate. | ||
| /// </summary> | ||
| [Input( | ||
| DisplayName = "Number of records", | ||
| Description = "The number of fake records to generate. Default: 10")] | ||
| public Input<int> Count { get; set; } = new(10); | ||
|
|
||
| /// <summary> | ||
| /// The locale to use when generating fake data (e.g. 'en', 'de', 'fr', 'nl'). | ||
| /// See the <see href="https://github.com/bchavez/Bogus#locales">Bogus locale list</see> for all supported values. | ||
| /// </summary> | ||
| [Input( | ||
| DisplayName = "Locale", | ||
| Description = "The locale to use when generating fake data (e.g. 'en', 'de', 'fr', 'nl'). Default: en")] | ||
| public Input<string> Locale { get; set; } = new("en"); | ||
|
|
||
| /// <summary> | ||
| /// A seed value for the random number generator. | ||
| /// Providing a seed will ensure that the same fake data is generated on each run, which can be useful for deterministic testing. | ||
| /// If not provided, a random seed will be used, resulting in different data on each execution. | ||
| /// </summary> | ||
| [Input( | ||
| DisplayName = "Seed", | ||
| Description = "With a seed, each run will produce the identical data (for deterministic tests).")] | ||
| public Input<int?> Seed { get; set; } = new((int?)null); | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override ValueTask ExecuteAsync(ActivityExecutionContext context) | ||
| { | ||
| var count = Count.Get(context); | ||
|
|
||
| if (count < 0) | ||
| throw new ArgumentOutOfRangeException(nameof(Count), count, "The number of records to generate must be zero or greater."); | ||
|
|
||
| var locale = Locale.GetOrDefault(context) ?? "en"; | ||
| var seed = Seed.GetOrDefault(context, () => null); | ||
|
|
||
| var faker = CreateFaker(locale, seed); | ||
| var items = faker.Generate(count); | ||
|
|
||
| context.Set(Result, items.ToArray()); | ||
|
|
||
| return ValueTask.CompletedTask; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a configured <see cref="Faker{T}"/> instance for the given locale. | ||
| /// </summary> | ||
| /// <param name="locale">The locale string (e.g. "en", "de", "fr").</param> | ||
| /// <param name="seed">An optional seed value for deterministic data generation.</param> | ||
| /// <returns>A configured <see cref="Faker{T}"/> instance.</returns> | ||
| protected abstract Faker<T> CreateFaker(string locale, int? seed); | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
src/modules/fakedata/Elsa.FakeData/Activities/GenerateFakeOrders.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using System.Globalization; | ||
| using System.Runtime.CompilerServices; | ||
| using Bogus; | ||
| using Elsa.FakeData.Extensions; | ||
| using Elsa.FakeData.Models; | ||
| using Elsa.Workflows; | ||
| using Elsa.Workflows.Attributes; | ||
|
|
||
| namespace Elsa.FakeData.Activities; | ||
|
|
||
| /// <summary> | ||
| /// Generates a collection of fake <see cref="FakeOrder"/> records. | ||
| /// </summary> | ||
| [Activity( | ||
| Namespace = "Elsa", | ||
| Category = "Fake Data", | ||
| DisplayName = "Generate Orders", | ||
| Description = "Generates a collection of fake order records.", | ||
| Kind = ActivityKind.Task)] | ||
| public class GenerateFakeOrders : GenerateFakeDataActivity<FakeOrder> | ||
| { | ||
| /// <inheritdoc /> | ||
| public GenerateFakeOrders([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Faker<FakeOrder> CreateFaker(string locale, int? seed) | ||
| { | ||
| return new Faker<FakeOrder>(locale) | ||
| .When(seed is not null, f => f.UseSeed(seed!.Value)) | ||
| .RuleFor(o => o.Id, f => f.Random.Guid()) | ||
| .RuleFor(o => o.OrderNumber, f => f.Random.Number(100_000, 1_000_000_000).ToString(CultureInfo.InvariantCulture)) | ||
| .RuleFor(o => o.OrderDate, f => f.Date.Past()) | ||
| .RuleFor(o => o.Status, f => f.PickRandom("Pending", "Processing", "Shipped", "Delivered", "Cancelled")) | ||
| .RuleFor(o => o.CustomerName, f => f.Person.FullName) | ||
| .RuleFor(o => o.CustomerEmail, f => f.Person.Email) | ||
| .RuleFor(o => o.ShippingAddress, f => f.Address.FullAddress()) | ||
| .RuleFor(o => o.SubTotal, f => f.Finance.Amount(10, 500)) | ||
| .RuleFor(o => o.Tax, (_, o) => Math.Round(o.SubTotal * 0.1m, 2)) | ||
| .RuleFor(o => o.Total, (_, o) => o.SubTotal + o.Tax); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/modules/fakedata/Elsa.FakeData/Activities/GenerateFakePersons.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using Bogus; | ||
| using Elsa.FakeData.Extensions; | ||
| using Elsa.FakeData.Models; | ||
| using Elsa.Workflows; | ||
| using Elsa.Workflows.Attributes; | ||
|
|
||
| namespace Elsa.FakeData.Activities; | ||
|
|
||
| /// <summary> | ||
| /// Generates a collection of fake <see cref="FakePerson"/> records. | ||
| /// </summary> | ||
| [Activity( | ||
| Namespace = "Elsa", | ||
| Category = "Fake Data", | ||
| DisplayName = "Generate Persons", | ||
| Description = "Generates a collection of fake person records.", | ||
| Kind = ActivityKind.Task)] | ||
| public class GenerateFakePersons : GenerateFakeDataActivity<FakePerson> | ||
| { | ||
| /// <inheritdoc /> | ||
| public GenerateFakePersons([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Faker<FakePerson> CreateFaker(string locale, int? seed) | ||
| { | ||
| return new Faker<FakePerson>(locale) | ||
| .When(seed is not null, f => f.UseSeed(seed!.Value)) | ||
| .RuleFor(p => p.Id, f => f.Random.Guid()) | ||
| .RuleFor(p => p.FirstName, f => f.Person.FirstName) | ||
| .RuleFor(p => p.LastName, f => f.Person.LastName) | ||
| .RuleFor(p => p.FullName, (f) => f.Person.FullName) | ||
| .RuleFor(p => p.Email, (f, p) => f.Person.Email) | ||
| .RuleFor(p => p.Phone, f => f.Phone.PhoneNumber()) | ||
| .RuleFor(p => p.DateOfBirth, f => f.Date.Past(80, new DateTime(2000, 1, 1))); | ||
| .RuleFor(p => p.Gender, f => f.PickRandom("Male", "Female", "Non-binary")) | ||
| .RuleFor(p => p.Address, f => f.Address.StreetAddress()) | ||
| .RuleFor(p => p.City, f => f.Address.City()) | ||
| .RuleFor(p => p.State, f => f.Address.State()) | ||
| .RuleFor(p => p.ZipCode, f => f.Address.ZipCode()) | ||
| .RuleFor(p => p.Country, f => f.Address.Country()); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/modules/fakedata/Elsa.FakeData/Activities/GenerateFakeProducts.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using Bogus; | ||
| using Elsa.FakeData.Extensions; | ||
| using Elsa.FakeData.Models; | ||
| using Elsa.Workflows; | ||
| using Elsa.Workflows.Attributes; | ||
|
|
||
| namespace Elsa.FakeData.Activities; | ||
|
|
||
| /// <summary> | ||
| /// Generates a collection of fake <see cref="FakeProduct"/> records. | ||
| /// </summary> | ||
| [Activity( | ||
| Namespace = "Elsa", | ||
| Category = "Fake Data", | ||
| DisplayName = "Generate Products", | ||
| Description = "Generates a collection of fake product records.", | ||
| Kind = ActivityKind.Task)] | ||
| public class GenerateFakeProducts : GenerateFakeDataActivity<FakeProduct> | ||
| { | ||
| /// <inheritdoc /> | ||
| public GenerateFakeProducts([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Faker<FakeProduct> CreateFaker(string locale, int? seed) | ||
| { | ||
| return new Faker<FakeProduct>(locale) | ||
| .When(seed is not null, f => f.UseSeed(seed!.Value)) | ||
| .RuleFor(p => p.Id, f => f.Random.Guid()) | ||
| .RuleFor(p => p.Name, f => f.Commerce.ProductName()) | ||
| .RuleFor(p => p.Description, f => f.Commerce.ProductDescription()) | ||
| .RuleFor(p => p.Price, f => f.Finance.Amount(1, 1000)) | ||
| .RuleFor(p => p.Category, f => f.Commerce.Categories(1)[0]) | ||
| .RuleFor(p => p.Department, f => f.Commerce.Department()) | ||
| .RuleFor(p => p.Rating, f => f.Random.Number(1, 5)) | ||
| .RuleFor(p => p.Stock, f => f.Random.Int(0, 1000)); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/modules/fakedata/Elsa.FakeData/Activities/GenerateFakeUsers.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using Bogus; | ||
| using Elsa.FakeData.Extensions; | ||
| using Elsa.FakeData.Models; | ||
| using Elsa.Workflows; | ||
| using Elsa.Workflows.Attributes; | ||
|
|
||
| namespace Elsa.FakeData.Activities; | ||
|
|
||
| /// <summary> | ||
| /// Generates a collection of fake <see cref="FakeUser"/> records. | ||
| /// </summary> | ||
| [Activity( | ||
| Namespace = "Elsa", | ||
| Category = "Fake Data", | ||
| DisplayName = "Generate Users", | ||
| Description = "Generates a collection of fake user records.", | ||
| Kind = ActivityKind.Task)] | ||
| public class GenerateFakeUsers : GenerateFakeDataActivity<FakeUser> | ||
| { | ||
| /// <inheritdoc /> | ||
| public GenerateFakeUsers([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Faker<FakeUser> CreateFaker(string locale, int? seed) | ||
| { | ||
| return new Faker<FakeUser>(locale) | ||
| .When(seed is not null, f => f.UseSeed(seed!.Value)) | ||
| .RuleFor(u => u.Id, f => f.Random.Guid()) | ||
| .RuleFor(u => u.FirstName, f => f.Person.FirstName) | ||
| .RuleFor(u => u.LastName, f => f.Person.LastName) | ||
| .RuleFor(u => u.Username, f => f.Person.UserName) | ||
| .RuleFor(u => u.Email, f => f.Person.Email) | ||
| .RuleFor(u => u.Avatar, f => f.Person.Avatar) | ||
| .RuleFor(u => u.Roles, f => [f.PickRandom("Admin", "User", "Moderator", "Guest")]) | ||
| .RuleFor(u => u.CreatedAt, f => f.Date.Past(5)); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <Description> | ||
| Provides activities to generate fake data for testing and benchmarking purposes. | ||
| </Description> | ||
| <PackageTags>elsa module fakedata testing benchmarking bogus</PackageTags> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Bogus" /> | ||
| <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" /> | ||
|
|
||
| <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' != 'true'"> | ||
| <PackageReference Include="Elsa.Workflows.Management" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' == 'true'"> | ||
| <ProjectReference Include="..\..\..\..\..\elsa-core\src\modules\Elsa.Workflows.Management\Elsa.Workflows.Management.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
19 changes: 19 additions & 0 deletions
19
src/modules/fakedata/Elsa.FakeData/Extensions/ModuleExtensions.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using Elsa.Extensions; | ||
| using Elsa.FakeData.Features; | ||
| using Elsa.Features.Services; | ||
|
|
||
| namespace Elsa.FakeData.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for configuring fake data services. | ||
| /// </summary> | ||
| public static class ModuleExtensions | ||
| { | ||
| /// <summary> | ||
| /// Installs the FakeData module. | ||
| /// </summary> | ||
| public static IModule UseFakeData(this IModule module, Action<FakeDataFeature>? configure = null) | ||
| { | ||
| return module.Use(configure); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/modules/fakedata/Elsa.FakeData/Extensions/ObjectExtensions.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Elsa.FakeData.Extensions; | ||
|
|
||
| internal static class ObjectExtensions | ||
| { | ||
| public static T When<T>(this T obj, bool condition, Func<T, T> action) | ||
| { | ||
| return condition ? action(obj) : obj; | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/modules/fakedata/Elsa.FakeData/Features/FakeDataFeature.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Elsa.Extensions; | ||
| using Elsa.Features.Abstractions; | ||
| using Elsa.Features.Services; | ||
|
|
||
| namespace Elsa.FakeData.Features; | ||
|
|
||
| /// <summary> | ||
| /// A feature that provides activities to generate fake data for testing and benchmarking purposes. | ||
| /// </summary> | ||
| public class FakeDataFeature : FeatureBase | ||
| { | ||
| /// <inheritdoc/> | ||
| public FakeDataFeature(IModule module) : base(module) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Configure() | ||
| { | ||
| Module.AddActivitiesFrom<FakeDataFeature>(); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd" /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.