Skip to content
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

Define char quantity in random names #63

Closed
NicolaPicoli opened this issue Mar 13, 2017 · 6 comments
Closed

Define char quantity in random names #63

NicolaPicoli opened this issue Mar 13, 2017 · 6 comments

Comments

@NicolaPicoli
Copy link

It would be very useful if we could insert a parameter to define a maximum and minimum quantity of characters in any object generated.
It would help many in running unit tests!

Thank you very much!
Nicola Picoli

@bchavez
Copy link
Owner

bchavez commented Mar 13, 2017

Hi @NicolaPicoli ,

Can you give me a few examples of what you're trying to do?

Thanks,
🌲 🔮 PINES - Fate

@NicolaPicoli
Copy link
Author

Hi @bchavez!

Some entities have character size validations. For example, FirstName must be a minimum of 3 characters and a maximum of 30 characters.

I would like when creating a random "FirstName" could include the minimum and maximum number of characters, for example:

var result = new Faker<Customer>()
    .RuleFor(p => p.Name, v => v.Company.CompanyName(0).Length([MIN], [MAX]))

Tks!

@bchavez
Copy link
Owner

bchavez commented Mar 14, 2017

Hi @NicolaPicoli ,

Thanks for your example. Could you try this:

public static class ExtensionsForString
{
    private static Randomizer r = new Randomizer();

    public static string ClampLength(this string str, int? min = null, int? max = null)
    {
        if( max != null && str.Length > max )
        {
            return str.Substring(0, max.Value);
        }
        if( min != null && min > str.Length )
        {
            var missingChars = min - str.Length;
            var fillerChars = r.Replace("".PadRight(missingChars.Value, '?'));
            return str + fillerChars;
        }
        return str;
    }
}
company.CompanyName(0).ClampLength(12, 15).Dump();

Produces:

"Mitchell Inc"
"Schultz LLCA"
"Mueller IncR"
"Kreiger and Son"
"Welch IncSPC"
"Bashirian LLC"
"Veum IncVVIP"
"Medhurst Inc"
"Paucek Group"
"Hoppe IncPOF"
"Haley IncRUE"
"Waters Group"

I think this is the best we can do. After considering your situation I don't think there's an easy way to do this inside Bogus where we avoid the random filler characters.

The first problem arises where we don't have enough characters to meet the minimum length. What do we do?

  • We could query the faker database again with a request to something between min and max length. But we can't efficiently without a JSONPath size() filter. Newtonsoft, IIRC, doesn't support a size() JSONPath filter in its query select token. We'd have to read the entire list and keep finding words that would meet the requirement. :(

  • Add filler characters like we did above (see ExtensionsForString).

If you have other ideas let me know but I don't think we should add this to Bogus since it's not an ideal solution. Some people might not be okay with the random filler characters.

💥 🔥 "Set it ablaze like a candle wick... Light it up, light it up..."

@bchavez
Copy link
Owner

bchavez commented Mar 26, 2017

Hi @NicolaPicoli ,

I've moved ClampLength string extension to Bogus.Extensions to help work with strings and fake data. This extension method will be available in the next v12 release.

Thanks,
Brian

😎 ☁️ "I just wanna stay in the sun where I find... I know it's hard sometimes"

@NicolaPicoli
Copy link
Author

Very Nice...

Thanks @bchavez !

@aline-almeida
Copy link

Nevertheless, I think having a max value would be useful. For example for testing layouts where we want to try different lengths. I would like to have this on full names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants