-
-
Notifications
You must be signed in to change notification settings - Fork 490
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
How to register custom DataSet
s for scoped/local seed propagation given by UseSeed
#405
Comments
Hi @ChristoWolf, thank you for creating an issue.
I have a hunch about what you are trying to do, but I can't be totally sure from the description above ... could you please post some sample code? I want to make sure I'm helping with the right answer to your question. Also, sorry for the late reply, my workload is high right now and I don't get out of work until late :( Thank you, |
@ChristoWolf, I think this is what you're looking for; a way to reset the seed deterministically at the The void Main()
{
var f = new Faker<User>()
.AttachFood()
.UseSeed(1111)
.RuleFor(x => x.Name, f => f.Name.FirstName())
.RuleFor(x => x.Candy, f => f.Food().Candy());
f.Generate().Dump();
f.UseSeed(1111);
f.Generate().Dump();
}
public class User
{
public string Name;
public string Candy;
}
public static class ExtensionsForFood
{
public static Faker<T> AttachFood<T>(this Faker<T> fakerT) where T : class
{
var fakerInternal = fakerT as IFakerTInternal;
var faker = fakerInternal.FakerHub;
ContextHelper.GetOrSet(faker, () => new Food());
return fakerT;
}
public static Food Food(this Faker faker)
{
return ContextHelper.GetOrSet(faker, () => new Food());
}
}
public class Food : Bogus.DataSet
{
private static readonly string[] Candies =
{
"Hard candy", "Taffy", "Chocolate bar", "Stick candy",
"Jelly bean", "Mint", "Cotton candy", "Lollipop"
};
public string Candy()
{
return this.Random.ArrayElement(Candies);
}
private static readonly string[] Drinks = { "Soda", "Water", "Beer", "Wine", "Coffee", "Lemonade", "Milk" };
public string Drink()
{
return this.Random.ArrayElement(Drinks);
}
} Let me know if that helps. |
Hi @bchavez! First of all, thanks a ton for the incredibly fast feedback! The solution that you provided is exactly what I was looking for, thanks! Would it make sense for you if this example was added to the Thanks again! |
Yeah, I think that makes sense. We should probably add the extra code to the |
If you need help with that, I can also add this to the example, just let me know. Thanks again for the great help and of course all the stuff that you are providing with Bogus, @bchavez! |
@bchavez: Would it make sense to add such a (non-extension) generic method public AttachCustomDataSet<TDataSet>() where TDataSet : DataSet, new()
{
// Generic implementation
} to |
Hi @ChristoWolf, that's a great way to generalize the solution. Maybe, we could add it, but I would need to really think about it. Generally, I don't change the public APIs of I like to make sure our public APIs that 21M downloads see are generally consistent and well-thought-out before making any changes. Usually, my thought process is:
I just have to really think about it before moving on to a public API change. But I do like your generic solution for resolving the original issue you had :) 😎 |
Great feature proposal thought process, makes a lot of sense of course! Thanks again, and feel free to let me know if I can help, @bchavez! |
I just created my own dataset and discovered too that the seed is not respected in the dataset, even with that Attachfood approach provided. public class ColorDataSet : DataSet
{
public string Hex(bool includeAlpha = false)
{
var r = Random.Int(0, 255);
var g = Random.Int(0, 255);
var b = Random.Int(0, 255);
var color = $"#{r:X2}{g:X2}{b:X2}";
if (includeAlpha)
{
var a = Random.Int(0, 255);
return color + r.ToString("X2");
}
return color;
}
}
public static ColorDataSet Color(this Faker faker)
{
return ContextHelper.GetOrSet(faker, () => new ColorDataSet());
}
public static Faker<T> AttachColor<T>(this Faker<T> faker) where T : class
{
var fakerInternal = faker as IFakerTInternal;
var hub = fakerInternal.FakerHub;
ContextHelper.GetOrSet(hub, () => new ColorDataSet());
return faker;
} This results in teh hex color being generated with different results even when the Faker is created with UseSeed. |
@garcipat: Can you please show your code where you actually attach the data set? |
Oh my, I added the attach again and now it seams to work propertly... I got it like that: var faker = new Faker<T>()
.AttackColor()
.UseSeed(123456)
.RulesFor(x => x.Color, f => f.Color().Hex()); I'm not sure now if I maybe added the Attach after the UseSeed and becasue of that it didn't apply. Thank you for pointing to the probably error. |
Yep, the order is important, happy that it works for you now! |
Version Information
What locale are you using with Bogus?
en
What's the problem?
Hi!
I am not really sure how to make a custom
DataSet
use aFaker<T>
instance's configured scoped/local seed (configured via itsUseSeed(int)
option, added during this PR due to this issue) for generation.If I understand correctly, this is because its
FakerHub
(of course; or only if I am missing something) cannot (seed-) notify the customDataSet
.E.g. if we have a
CustomDataSet
(similarly to how it is suggested in this example), then using thethis.Random
for generating does not give seeded values, because of the randomizer never being set.What possible solutions have you considered?
As I understand it, it should be possible for
Faker
derivates, becausethis.Notifier
would be accessible, thus one could addin there.
But this does not work for
Faker<T>
, because theNotifier
of itsFakerHub
is not accessible.I have also considered explicitly passing the
Randomizer
instance to the custom dataset, but that would disable the notifier mechanism there, if I understand correctly.Do you have sample code to show what you're trying to do?
Not sure how to do this in a short amount of time in LinqPad.
But simply using the
Food
dataset from aFaker<T>
instance with configured constant local seed yields different results for each test/app execution because the randomizer is never explicitly set, thus a newRandomizer
instance without the seed is created and attached to it.The text was updated successfully, but these errors were encountered: