fluffynuts / PeanutButter

Tasty, versatile, nutritious; goes with many things in .net.
BSD 3-Clause "New" or "Revised" License
179 stars 32 forks source link

Allow specifying values which should not be generated within GetRandomInt and GetRandomLong #96

Closed caybokotze closed 1 year ago

caybokotze commented 1 year ago

This is a POC for something I think is useful for some use cases I have come across. You will have to let me know what you think about it.

I sometimes find myself writing unit tests where I want to test that a foreign key exception does get thrown for an invalid value. In those cases, it would be useful to say "give me a random number that is not equal to the primary key value of the record I just inserted." That way I get a guaranteed failure every time (which is a valid test). What happens at the moment is every now and then I get tests that fail because the random number I generated is equal to the primary key (by chance) which was inserted, which is not what I want.

This might not be the best way to go about this implementation. Perhaps not having the params long[] and only the Func overloads would be less confusing and hence more difficult to make use of the overload by mistake.

Let me know what you think.

caybokotze commented 1 year ago

I did notice some of the RandomGenerators.Tests fail, however, I do not see how those failures are related to any changes I have made. All the tests for GetRandomInt and GetRandomLong do pass including the ones that existed before.

caybokotze commented 1 year ago
image

Here is an example of what I would like to achieve. Instead of setting a minimum limit which I assume won't be reached, I would just like to say except business.id

fluffynuts commented 1 year ago

@caybokotze there is GetAnother which (I think) does what you want, on all basic types (:

var id = GetRandomInt();
var another = GetAnother(id);
var yetAnother = GetAnother<int>(new[] { id, another} );

There are overloads which can be given a generator function too, so if you were, eg looking for another first name:

var name = GetRandomFirstName();
var otherName = GetAnother<string>(new[] { name }, GetRandomFirstName)

(writing by hand here, so hopefully this compiles :D )

If this still doesn't do what you want, let me know (:

caybokotze commented 1 year ago

Good lord, you thought of everything.

Thanks for the feedback, that will do the trick.