elixir - Why is params_for not going through the changeset? -
why params_for not going through changeset model main question. params_for generates 4 digits , 2 letters postal_code (could lowercase or uppercase). if params_for going trough changeset, uppercase afterwards. why test fail?
error message: expected truthy, got nil
i've company model, changeset:
def changeset(company, params \\ %{}) company ... |> maybe_sanitize_postal_code(:postal_code) ... end defp maybe_sanitize_postal_code(changeset, attr) case get_change(changeset, attr) nil -> changeset postal_code -> put_change(changeset, attr, sanitize_postal_code(postal_code)) end end defp sanitize_postal_code(postal_code) postal_code |> string.replace(~r/\s+/, "") |> string.upcase() end
i have test:
test "create company postal code", %{conn: conn, user: user} params = params_for(:company) conn = post conn, company_path(conn, :create), company: params assert json_response(conn, 201)["company"]["id"] company = repo.get_by(company, params) assert company assert [user] == repo.all ecto.assoc(company, :users) ... end
params_for docs: https://hexdocs.pm/ex_machina/exmachina.ecto.html#c:params_for/2
if params
contains lowercased postal_code
, gets upcased changeset. however, repo.get_by(company, params)
still looks lowercase postal code, returning nil
Comments
Post a Comment