Skip to content

Commit

Permalink
Validate uniqueness on (type, value) columns of contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
smaximov committed Mar 5, 2018
1 parent d58b8ff commit 7ec17d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/blog/accounts/contact.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Blog.Accounts.Contact do
contact
|> cast(attrs, [:type, :value])
|> validate_required([:type, :value])
|> unique_constraint(:value, name: :contacts_type_value_index)
end

end
12 changes: 12 additions & 0 deletions test/blog/accounts/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ defmodule Blog.AccountsTest do
assert {:error, %Ecto.Changeset{}} = Accounts.create_user(attrs)
assert ^contact_count = Repo.aggregate(Accounts.Contact, :count, :id)
end

test "create_user/1 discards duplicate contacts" do
contact = %{type: "phone", value: "+1 000 000 0000"}

assert {:ok, _} = Accounts.create_user(%{contact: contact, name: "name", password: "password"})

assert {:error, changeset} = Accounts.create_user(%{contact: contact, name: "other", password: "password"})
require IEx; IEx.pry


assert "has already been taken" in errors_on(changeset).value
end
end

0 comments on commit 7ec17d3

Please sign in to comment.