{"id":13509153,"url":"https://github.com/batate/blacksmith","last_synced_at":"2025-10-21T15:12:56.361Z","repository":{"id":23695052,"uuid":"27067006","full_name":"batate/blacksmith","owner":"batate","description":"Data generation framework for Elixir","archived":false,"fork":false,"pushed_at":"2023-01-10T23:02:14.000Z","size":55,"stargazers_count":196,"open_issues_count":5,"forks_count":11,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-29T00:42:13.328Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"heroku/heroku.rb","license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/batate.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-24T08:22:27.000Z","updated_at":"2024-10-02T08:40:49.000Z","dependencies_parsed_at":"2023-01-14T01:30:13.654Z","dependency_job_id":null,"html_url":"https://github.com/batate/blacksmith","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batate%2Fblacksmith","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batate%2Fblacksmith/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batate%2Fblacksmith/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/batate%2Fblacksmith/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/batate","download_url":"https://codeload.github.com/batate/blacksmith/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222552615,"owners_count":17002123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:01:03.701Z","updated_at":"2025-10-21T15:12:51.326Z","avatar_url":"https://github.com/batate.png","language":"Elixir","funding_links":[],"categories":["Testing"],"sub_categories":[],"readme":"Blacksmith\n==========\n\nData generation framework for Elixir.\n\nIn testing, sometimes it's useful to create records in the form of maps. Blacksmith makes it easy.\n\nFirst, install Blacksmith:\n\nIn your mix.exs file, add the blacksmith dependency:\n\n~~~elixir\ndef deps do\n  [{:blacksmith, \"~\u003e 0.1\"}]\nend\n~~~\n\nYou will also have to add `:blacksmith` to your application list:\n\n~~~elixir\ndef application do\n  [applications: applications(Mix.env)]\nend\n\ndefp applications(:test), do: applications(:all) ++ [:blacksmith]\ndefp applications(_all),  do: [:logger]\n~~~\n\nNext, tell Blacksmith how to save one record, or many records:\n\n~~~elixir\ndefmodule Blacksmith.Config do\n  def save(model) do\n    MyRepo |\u003e save(model)\n  end\n\n  def save_all(list_of_models) do\n    MyRepo |\u003e save_all(list_of_models)\n  end\nend\n~~~\n\nNext, perhaps in test_helper for convenience or somewhere in lib for speed, register each of your new models with Forge. Use [Faker](https://github.com/igas/faker) for fake values, and sequences to maintain unique values:\n\n~~~elixir\ndefmodule Forge do\n  use Blacksmith\n  register :user,\n    name: Faker.Name.first_name,\n    email: Sequence.next(:email, \u0026\"test#{\u00261}@example.com\"),\n    description: Faker.Lorem.sentence,\n    roles: [],\n    always_the_same: \"string\"\n\n  # this will create a user with roles set to [:admin]\n  register :admin,\n    [prototype: :user],\n    roles: [\"admin\"]\nend\n~~~\n\nNow you can create a user, generating all of the default values:\n\n~~~elixir\n  user = Forge.user\n~~~\n\nor a saved user, with the name attribute overridden, and a new attribute of favorite_language:\n\n~~~elixir\n  user = Forge.saved_user\n           name: \"Will Override\",\n           favorite_language: \"Elixir\"\n~~~\n\nor a list of 5 users\n\n~~~elixir\n  user = Forge.user_list 5\n~~~\n\nor a saved list of 5 admins\n\n~~~elixir\n  admin = Forge.saved_admin_list repo, 5\n~~~\n\nCreate a list using a few common data elements:\n\n~~~elixir\n  Forge.having survey_id: some_survey.id, author: Forge.user do\n    question = Forge.question   # will share the same survey id and user from above\n  end\n~~~\n\nNext release: allow nesting of having blocks.\n\n## Using with Ecto\n\nBlacksmith can be used easily with a database persistence library such as Ecto.\n\n~~~elixir\ndefmodule User do\n  use Ecto.Model\n\n  schema \"users\" do\n    field :name, :string\n    field :email, :string\n  end\nend\n~~~\n\nThe `@save_one_function` and `@save_all_function` attributes are used to delegate to your persistence layer. We delegate to `Blacksmith.Config` defined below. You'll also notice that we directly create a struct in `register :user`, that's because Ecto works with models built on structs instead of plain maps.\n\n~~~elixir\ndefmodule Forge do\n  use Blacksmith\n\n  @save_one_function \u0026Blacksmith.Config.save/1\n  @save_all_function \u0026Blacksmith.Config.save_all/1\n\n  register :user, %User{\n    name: \"John Henry\",\n    email: Sequence.next(:email, \u0026\"jh#{\u00261}@example.com\")\n  }\nend\n~~~\n\n`Blacksmith.Config` defines the callback functions that delegate to the Ecto repository for persistence.\n\n~~~elixir\ndefmodule Blacksmith.Config do\n  def save(map) do\n    MyRepo.insert(map)\n  end\n\n  def save_all(list) do\n    Enum.map(list, \u0026MyRepo.insert/1)\n  end\nend\n~~~\n\n`Forge.saved_user` will generate a `User` model that have been inserted in the database backed by `MyRepo`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbatate%2Fblacksmith","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbatate%2Fblacksmith","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbatate%2Fblacksmith/lists"}