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

UniqueIndex seems to generate repeating values #48

Closed
mdmoura opened this issue Dec 21, 2016 · 2 comments
Closed

UniqueIndex seems to generate repeating values #48

mdmoura opened this issue Dec 21, 2016 · 2 comments

Comments

@mdmoura
Copy link

mdmoura commented Dec 21, 2016

Hello,

I am getting a strange behavior with UniqueIndex. I have the following:

        Faker<Client> clients = new Faker<Client>()
          .RuleFor(x => x.Description, y => y.Lorem.Paragraphs(1))

        users.AddRange(
          new Faker<User>()
            .RuleFor(x => x.Name, y => new String[] { "John", "Mary", "Mike", "Tom" }[y.UniqueIndex % 4])
            .RuleFor(x => x.Email, (y, x) => { return $"{x.Name}@xyz.com".Replace(" ", "").ToLower(); })
            .RuleFor(x => x.Client, y => y.Random.Bool() ? clients.Generate(1).First() : null)
            .RuleFor(x => x.UserName, (y, x) => x.Email)
            .Generate(4).ToList());

I am getting four users with repeating names like: John, Mary, John, Mary ...

If I remove the line to generate the Client, e.g:

            .RuleFor(x => x.Client, y => y.Random.Bool() ? clients.Generate(1).First() : null)

Then it seems I get the four different names ... Any idea why?

Thank You

@bchavez
Copy link
Owner

bchavez commented Dec 22, 2016

You have a Generate(1) that is randomly called call within a rule controlled y.Random.Bool() ? clients.Generate(1).

This is what causes the UniqueIndex to appear to skip values from the outer scope Generate(4). So, the first of Generate(4), may or may not call Generate(1) for a client. If it does call Generate(1) for a client it will cause the UniqueIndex to increment again.

UniqueIndex is not intended to be used or referenced as sequential incrementing value, but more as a globally unique integer that spans the entirety of an app domain and faker instance creations.

If you want to control your own counter (or in this case move sequential across the array of values), you'll have to define and increment your own counter.

Thanks,
Brian

🍂 🍃 Distance - Falling (ft Alys Be)

@bchavez bchavez closed this as completed Dec 22, 2016
@mdmoura
Copy link
Author

mdmoura commented Dec 23, 2016

Hi,

Wouldn't be possible to add to Bogus an, for example, UniqueLocalIndex (or any other name) that would be incremented only in each generation block?

Or maybe a better way to integrate user created indexes in Fluent code of Bogus? Something like:

   int index = 0;
   var users = 
      new Faker<User>()
        .UseIndex(index, 0) // 0 is the initial value. Could be another one.
        .RuleFor(x => x.Name, y => new String[] { "John", "Mary", "Mike", "Tom" }[index % 4])
        .RuleFor(x => x.Email, (y, x) => { return $"{x.Name}@xyz.com".Replace(" ", "").ToLower(); })
        .RuleFor(x => x.Client, y => y.Random.Bool() ? clients.Generate(1).First() : null)
        .RuleFor(x => x.UserName, (y, x) => x.Email)
        .UpdateIndex(index++) // index++ or index = index + 2, etc. index is updated in each cycle. 
        .Generate(4).ToList());

Using Bogus for quite sometime having a local index well integrated is really helpful.
This is very useful when generating data in more complex database models.

Thank You,
Miguel

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

2 participants