-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from dwyl/unique-constraints
Unique Constraints
- Loading branch information
Showing
10 changed files
with
172 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
priv/repo/test_app/migrations/20181211161000_unique_index.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Alog.Repo.Migrations.UniqueIndex do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:unique) do | ||
add(:name, :string) | ||
add(:entry_id, :string) | ||
add(:deleted, :boolean, default: false) | ||
|
||
timestamps() | ||
end | ||
|
||
create(unique_index(:unique, :name)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule AlogTest.ConstraintTest do | ||
use Alog.TestApp.DataCase | ||
|
||
alias Alog.TestApp.{User, Helpers} | ||
|
||
describe "apply_constraints/1:" do | ||
test "returns error if not unique on insert" do | ||
{:ok, user_1} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
|
||
assert {:error, user_2} = | ||
%User{} | ||
|> User.changeset( | ||
Helpers.user_2_params() | ||
|> Map.merge(%{username: user_1.username}) | ||
) | ||
|> User.insert() | ||
|
||
assert user_2.errors == [username: {"has already been taken", []}] | ||
end | ||
|
||
test "returns error if not unique on update" do | ||
{:ok, user_1} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
{:ok, user_2} = %User{} |> User.changeset(Helpers.user_2_params()) |> User.insert() | ||
|
||
assert {:error, user_2} = | ||
user_2 | ||
|> User.changeset(%{username: user_1.username}) | ||
|> User.update() | ||
|
||
assert user_2.errors == [username: {"has already been taken", []}] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Alog.TestApp.Unique do | ||
use Ecto.Schema | ||
use Alog | ||
import Ecto.Changeset | ||
|
||
schema "unique" do | ||
field(:name, :string) | ||
field(:entry_id, :string) | ||
field(:deleted, :boolean, default: false) | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(unique, attrs) do | ||
unique | ||
|> cast(attrs, [:name]) | ||
|> validate_required([:name]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters