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

Option to make any generated data unique #479

Open
chvanikoff opened this issue Sep 17, 2022 · 1 comment
Open

Option to make any generated data unique #479

chvanikoff opened this issue Sep 17, 2022 · 1 comment

Comments

@chvanikoff
Copy link

From project to project, I keep bringing my tiny function of making generated data unique:

def make_unique(string) do
  "#{string}#{System.unique_integer([:positive, :monotonic])}"
end

make_unique(Faker.Pokemon.name())

While it's really tiny, I thought what if we had an option to pass extra opts, like unique: true, to any of the Faker modules functions? i.e., Faker.For example, Pokemon.name(unique: true) would generate Pikachu42.

Dvogiatz pushed a commit to Dvogiatz/faker that referenced this issue Oct 19, 2022
Adds a helper function that differentiates a list of strings

Relates to elixirs#479
@adonig
Copy link

adonig commented Aug 25, 2023

When you use Faker with ExMachina you can use sequence/1 and sequence/2 to make something unique:

defmodule MyApp.Factory do
  use ExMachina
 
  def github_repo_factory do
    repo_name = sequence(:github_repo_name, &"repo-#{&1}")
 
    %GitHub.Repo{
      id: 1296269,
      name: repo_name,
      full_name: "octocat/#{repo_name}",
      description: "This your first repo!",
      owner_id: 1,
      owner_url: "https://api.github.com/users/octocat",
      private: false,
      html_url: "https://github.com/octocat/#{repo_name}",
      url: "https://api.github.com/repos/octocat/#{repo_name}"
    }
  end
end

You can then build multiple repos at once:

MyApp.Factory.build_list(3, :github_repo)
[
  %GitHub.Repo{
    name: "octocat/repo-1"
    # ...
  },
  %GitHub.Repo{
    name: "octocat/repo-2"
    # ...
  }
    %GitHub.Repo{
    name: "octocat/repo-3"
    # ...
  }
]

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